xml-crypto-next 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,230 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.C14nCanonicalizationWithComments = exports.C14nCanonicalization = void 0;
4
+ const utils = require("./utils");
5
+ const isDomNode = require("@xmldom/is-dom-node");
6
+ class C14nCanonicalization {
7
+ constructor() {
8
+ this.includeComments = false;
9
+ this.includeComments = false;
10
+ }
11
+ attrCompare(a, b) {
12
+ if (!a.namespaceURI && b.namespaceURI) {
13
+ return -1;
14
+ }
15
+ if (!b.namespaceURI && a.namespaceURI) {
16
+ return 1;
17
+ }
18
+ const left = a.namespaceURI + a.localName;
19
+ const right = b.namespaceURI + b.localName;
20
+ if (left === right) {
21
+ return 0;
22
+ }
23
+ else if (left < right) {
24
+ return -1;
25
+ }
26
+ else {
27
+ return 1;
28
+ }
29
+ }
30
+ nsCompare(a, b) {
31
+ const attr1 = a.prefix;
32
+ const attr2 = b.prefix;
33
+ if (attr1 === attr2) {
34
+ return 0;
35
+ }
36
+ return attr1.localeCompare(attr2);
37
+ }
38
+ renderAttrs(node) {
39
+ let i;
40
+ let attr;
41
+ const attrListToRender = [];
42
+ if (isDomNode.isCommentNode(node)) {
43
+ return this.renderComment(node);
44
+ }
45
+ if (node.attributes) {
46
+ for (i = 0; i < node.attributes.length; ++i) {
47
+ attr = node.attributes[i];
48
+ //ignore namespace definition attributes
49
+ if (attr.name.indexOf("xmlns") === 0) {
50
+ continue;
51
+ }
52
+ attrListToRender.push(attr);
53
+ }
54
+ }
55
+ attrListToRender.sort(this.attrCompare);
56
+ const res = attrListToRender.map((attr) => {
57
+ return ` ${attr.name}="${utils.encodeSpecialCharactersInAttribute(attr.value)}"`;
58
+ });
59
+ return res.join("");
60
+ }
61
+ /**
62
+ * Create the string of all namespace declarations that should appear on this element
63
+ *
64
+ * @param node The node we now render
65
+ * @param prefixesInScope The prefixes defined on this node parents which are a part of the output set
66
+ * @param defaultNs The current default namespace
67
+ * @param defaultNsForPrefix
68
+ * @param ancestorNamespaces Import ancestor namespaces if it is specified
69
+ * @api private
70
+ */
71
+ renderNs(node, prefixesInScope, defaultNs, defaultNsForPrefix, ancestorNamespaces) {
72
+ let i;
73
+ let attr;
74
+ const res = [];
75
+ let newDefaultNs = defaultNs;
76
+ const nsListToRender = [];
77
+ const currNs = node.namespaceURI || "";
78
+ //handle the namespace of the node itself
79
+ if (node.prefix) {
80
+ if (prefixesInScope.indexOf(node.prefix) === -1) {
81
+ nsListToRender.push({
82
+ prefix: node.prefix,
83
+ namespaceURI: node.namespaceURI || defaultNsForPrefix[node.prefix],
84
+ });
85
+ prefixesInScope.push(node.prefix);
86
+ }
87
+ }
88
+ else if (defaultNs !== currNs) {
89
+ //new default ns
90
+ newDefaultNs = node.namespaceURI || "";
91
+ res.push(' xmlns="', newDefaultNs, '"');
92
+ }
93
+ //handle the attributes namespace
94
+ if (node.attributes) {
95
+ for (i = 0; i < node.attributes.length; ++i) {
96
+ attr = node.attributes[i];
97
+ //handle all prefixed attributes that are included in the prefix list and where
98
+ //the prefix is not defined already. New prefixes can only be defined by `xmlns:`.
99
+ if (attr.prefix === "xmlns" && prefixesInScope.indexOf(attr.localName) === -1) {
100
+ nsListToRender.push({ prefix: attr.localName, namespaceURI: attr.value });
101
+ prefixesInScope.push(attr.localName);
102
+ }
103
+ //handle all prefixed attributes that are not xmlns definitions and where
104
+ //the prefix is not defined already
105
+ if (attr.prefix &&
106
+ prefixesInScope.indexOf(attr.prefix) === -1 &&
107
+ attr.prefix !== "xmlns" &&
108
+ attr.prefix !== "xml") {
109
+ nsListToRender.push({ prefix: attr.prefix, namespaceURI: attr.namespaceURI });
110
+ prefixesInScope.push(attr.prefix);
111
+ }
112
+ }
113
+ }
114
+ if (utils.isArrayHasLength(ancestorNamespaces)) {
115
+ // Remove namespaces which are already present in nsListToRender
116
+ for (const ancestorNamespace of ancestorNamespaces) {
117
+ let alreadyListed = false;
118
+ for (const nsToRender of nsListToRender) {
119
+ if (nsToRender.prefix === ancestorNamespace.prefix &&
120
+ nsToRender.namespaceURI === ancestorNamespace.namespaceURI) {
121
+ alreadyListed = true;
122
+ }
123
+ }
124
+ if (!alreadyListed) {
125
+ nsListToRender.push(ancestorNamespace);
126
+ }
127
+ }
128
+ }
129
+ nsListToRender.sort(this.nsCompare);
130
+ //render namespaces
131
+ res.push(...nsListToRender.map((attr) => {
132
+ if (attr.prefix) {
133
+ return ` xmlns:${attr.prefix}="${attr.namespaceURI}"`;
134
+ }
135
+ return ` xmlns="${attr.namespaceURI}"`;
136
+ }));
137
+ return { rendered: res.join(""), newDefaultNs };
138
+ }
139
+ /**
140
+ * @param node Node
141
+ */
142
+ processInner(node, prefixesInScope, defaultNs, defaultNsForPrefix, ancestorNamespaces) {
143
+ if (isDomNode.isCommentNode(node)) {
144
+ return this.renderComment(node);
145
+ }
146
+ if (node.data) {
147
+ return utils.encodeSpecialCharactersInText(node.data);
148
+ }
149
+ if (isDomNode.isElementNode(node)) {
150
+ let i;
151
+ let pfxCopy;
152
+ const ns = this.renderNs(node, prefixesInScope, defaultNs, defaultNsForPrefix, ancestorNamespaces);
153
+ const res = ["<", node.tagName, ns.rendered, this.renderAttrs(node), ">"];
154
+ for (i = 0; i < node.childNodes.length; ++i) {
155
+ pfxCopy = prefixesInScope.slice(0);
156
+ res.push(this.processInner(node.childNodes[i], pfxCopy, ns.newDefaultNs, defaultNsForPrefix, []));
157
+ }
158
+ res.push("</", node.tagName, ">");
159
+ return res.join("");
160
+ }
161
+ throw new Error(`Unable to canonicalize node type: ${node.nodeType}`);
162
+ }
163
+ // Thanks to deoxxa/xml-c14n for comment renderer
164
+ renderComment(node) {
165
+ if (!this.includeComments) {
166
+ return "";
167
+ }
168
+ const isOutsideDocument = node.ownerDocument === node.parentNode;
169
+ let isBeforeDocument = false;
170
+ let isAfterDocument = false;
171
+ if (isOutsideDocument) {
172
+ let nextNode = node;
173
+ let previousNode = node;
174
+ while (nextNode !== null) {
175
+ if (nextNode === node.ownerDocument.documentElement) {
176
+ isBeforeDocument = true;
177
+ break;
178
+ }
179
+ nextNode = nextNode.nextSibling;
180
+ }
181
+ while (previousNode !== null) {
182
+ if (previousNode === node.ownerDocument.documentElement) {
183
+ isAfterDocument = true;
184
+ break;
185
+ }
186
+ previousNode = previousNode.previousSibling;
187
+ }
188
+ }
189
+ const afterDocument = isAfterDocument ? "\n" : "";
190
+ const beforeDocument = isBeforeDocument ? "\n" : "";
191
+ const encodedText = utils.encodeSpecialCharactersInText(node.data);
192
+ return `${afterDocument}<!--${encodedText}-->${beforeDocument}`;
193
+ }
194
+ /**
195
+ * Perform canonicalization of the given node
196
+ *
197
+ * @param node
198
+ * @api public
199
+ */
200
+ process(node, options) {
201
+ options = options || {};
202
+ const defaultNs = options.defaultNs || "";
203
+ const defaultNsForPrefix = options.defaultNsForPrefix || {};
204
+ const ancestorNamespaces = options.ancestorNamespaces || [];
205
+ const prefixesInScope = [];
206
+ for (let i = 0; i < ancestorNamespaces.length; i++) {
207
+ prefixesInScope.push(ancestorNamespaces[i].prefix);
208
+ }
209
+ const res = this.processInner(node, prefixesInScope, defaultNs, defaultNsForPrefix, ancestorNamespaces);
210
+ return res;
211
+ }
212
+ getAlgorithmName() {
213
+ return "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
214
+ }
215
+ }
216
+ exports.C14nCanonicalization = C14nCanonicalization;
217
+ /**
218
+ * Add c14n#WithComments here (very simple subclass)
219
+ */
220
+ class C14nCanonicalizationWithComments extends C14nCanonicalization {
221
+ constructor() {
222
+ super();
223
+ this.includeComments = true;
224
+ }
225
+ getAlgorithmName() {
226
+ return "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments";
227
+ }
228
+ }
229
+ exports.C14nCanonicalizationWithComments = C14nCanonicalizationWithComments;
230
+ //# sourceMappingURL=c14n-canonicalization.js.map
@@ -0,0 +1 @@
1
+ {"version":3,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CAAC;QAEnE,OAAO,GAAG,aAAa,OAAO,WAAW,MAAM,cAAc,EAAE,CAAC;IAClE,CAAC;IAED;;;;;OAKG;IACH,OAAO,CAAC,IAAU,EAAE,OAAgE;QAClF,OAAO,GAAG,OAAO,IAAI,EAAE,CAAC;QACxB,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,EAAE,CAAC;QAC1C,MAAM,kBAAkB,GAAG,OAAO,CAAC,kBAAkB,IAAI,EAAE,CAAC;QAC5D,MAAM,kBAAkB,GAAG,OAAO,CAAC,kBAAkB,IAAI,EAAE,CAAC;QAE5D,MAAM,eAAe,GAAa,EAAE,CAAC;QACrC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,kBAAkB,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACnD,eAAe,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;QACrD,CAAC;QAED,MAAM,GAAG,GAAG,IAAI,CAAC,YAAY,CAC3B,IAAI,EACJ,eAAe,EACf,SAAS,EACT,kBAAkB,EAClB,kBAAkB,CACnB,CAAC;QACF,OAAO,GAAG,CAAC;IACb,CAAC;IAED,gBAAgB;QACd,OAAO,iDAAiD,CAAC;IAC3D,CAAC;CACF;AA/QD,oDA+QC;AAED;;GAEG;AACH,MAAa,gCAAiC,SAAQ,oBAAoB;IACxE;QACE,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC;IAC9B,CAAC;IAED,gBAAgB;QACd,OAAO,8DAA8D,CAAC;IACxE,CAAC;CACF;AATD,4EASC","sourcesContent":["import type {\r\n CanonicalizationOrTransformationAlgorithm,\r\n CanonicalizationOrTransformationAlgorithmProcessOptions,\r\n NamespacePrefix,\r\n RenderedNamespace,\r\n} from \"./types\";\r\nimport * as utils from \"./utils\";\r\nimport * as isDomNode from \"@xmldom/is-dom-node\";\r\n\r\nexport class C14nCanonicalization implements CanonicalizationOrTransformationAlgorithm {\r\n protected includeComments = false;\r\n\r\n constructor() {\r\n this.includeComments = false;\r\n }\r\n\r\n attrCompare(a, b) {\r\n if (!a.namespaceURI && b.namespaceURI) {\r\n return -1;\r\n }\r\n if (!b.namespaceURI && a.namespaceURI) {\r\n return 1;\r\n }\r\n\r\n const left = a.namespaceURI + a.localName;\r\n const right = b.namespaceURI + b.localName;\r\n\r\n if (left === right) {\r\n return 0;\r\n } else if (left < right) {\r\n return -1;\r\n } else {\r\n return 1;\r\n }\r\n }\r\n\r\n nsCompare(a, b) {\r\n const attr1 = a.prefix;\r\n const attr2 = b.prefix;\r\n if (attr1 === attr2) {\r\n return 0;\r\n }\r\n return attr1.localeCompare(attr2);\r\n }\r\n\r\n renderAttrs(node) {\r\n let i;\r\n let attr;\r\n const attrListToRender: Attr[] = [];\r\n\r\n if (isDomNode.isCommentNode(node)) {\r\n return this.renderComment(node);\r\n }\r\n\r\n if (node.attributes) {\r\n for (i = 0; i < node.attributes.length; ++i) {\r\n attr = node.attributes[i];\r\n //ignore namespace definition attributes\r\n if (attr.name.indexOf(\"xmlns\") === 0) {\r\n continue;\r\n }\r\n attrListToRender.push(attr);\r\n }\r\n }\r\n\r\n attrListToRender.sort(this.attrCompare);\r\n\r\n const res = attrListToRender.map((attr) => {\r\n return ` ${attr.name}=\"${utils.encodeSpecialCharactersInAttribute(attr.value)}\"`;\r\n });\r\n\r\n return res.join(\"\");\r\n }\r\n\r\n /**\r\n * Create the string of all namespace declarations that should appear on this element\r\n *\r\n * @param node The node we now render\r\n * @param prefixesInScope The prefixes defined on this node parents which are a part of the output set\r\n * @param defaultNs The current default namespace\r\n * @param defaultNsForPrefix\r\n * @param ancestorNamespaces Import ancestor namespaces if it is specified\r\n * @api private\r\n */\r\n renderNs(\r\n node: Element,\r\n prefixesInScope: string[],\r\n defaultNs: string,\r\n defaultNsForPrefix: string,\r\n ancestorNamespaces: NamespacePrefix[],\r\n ): RenderedNamespace {\r\n let i;\r\n let attr;\r\n const res: string[] = [];\r\n let newDefaultNs = defaultNs;\r\n const nsListToRender: { prefix: string; namespaceURI: string }[] = [];\r\n const currNs = node.namespaceURI || \"\";\r\n\r\n //handle the namespace of the node itself\r\n if (node.prefix) {\r\n if (prefixesInScope.indexOf(node.prefix) === -1) {\r\n nsListToRender.push({\r\n prefix: node.prefix,\r\n namespaceURI: node.namespaceURI || defaultNsForPrefix[node.prefix],\r\n });\r\n prefixesInScope.push(node.prefix);\r\n }\r\n } else if (defaultNs !== currNs) {\r\n //new default ns\r\n newDefaultNs = node.namespaceURI || \"\";\r\n res.push(' xmlns=\"', newDefaultNs, '\"');\r\n }\r\n\r\n //handle the attributes namespace\r\n if (node.attributes) {\r\n for (i = 0; i < node.attributes.length; ++i) {\r\n attr = node.attributes[i];\r\n\r\n //handle all prefixed attributes that are included in the prefix list and where\r\n //the prefix is not defined already. New prefixes can only be defined by `xmlns:`.\r\n if (attr.prefix === \"xmlns\" && prefixesInScope.indexOf(attr.localName) === -1) {\r\n nsListToRender.push({ prefix: attr.localName, namespaceURI: attr.value });\r\n prefixesInScope.push(attr.localName);\r\n }\r\n\r\n //handle all prefixed attributes that are not xmlns definitions and where\r\n //the prefix is not defined already\r\n if (\r\n attr.prefix &&\r\n prefixesInScope.indexOf(attr.prefix) === -1 &&\r\n attr.prefix !== \"xmlns\" &&\r\n attr.prefix !== \"xml\"\r\n ) {\r\n nsListToRender.push({ prefix: attr.prefix, namespaceURI: attr.namespaceURI });\r\n prefixesInScope.push(attr.prefix);\r\n }\r\n }\r\n }\r\n\r\n if (utils.isArrayHasLength(ancestorNamespaces)) {\r\n // Remove namespaces which are already present in nsListToRender\r\n for (const ancestorNamespace of ancestorNamespaces) {\r\n let alreadyListed = false;\r\n for (const nsToRender of nsListToRender) {\r\n if (\r\n nsToRender.prefix === ancestorNamespace.prefix &&\r\n nsToRender.namespaceURI === ancestorNamespace.namespaceURI\r\n ) {\r\n alreadyListed = true;\r\n }\r\n }\r\n\r\n if (!alreadyListed) {\r\n nsListToRender.push(ancestorNamespace);\r\n }\r\n }\r\n }\r\n\r\n nsListToRender.sort(this.nsCompare);\r\n\r\n //render namespaces\r\n res.push(\r\n ...nsListToRender.map((attr) => {\r\n if (attr.prefix) {\r\n return ` xmlns:${attr.prefix}=\"${attr.namespaceURI}\"`;\r\n }\r\n return ` xmlns=\"${attr.namespaceURI}\"`;\r\n }),\r\n );\r\n\r\n return { rendered: res.join(\"\"), newDefaultNs };\r\n }\r\n\r\n /**\r\n * @param node Node\r\n */\r\n processInner(node, prefixesInScope, defaultNs, defaultNsForPrefix, ancestorNamespaces) {\r\n if (isDomNode.isCommentNode(node)) {\r\n return this.renderComment(node);\r\n }\r\n if (node.data) {\r\n return utils.encodeSpecialCharactersInText(node.data);\r\n }\r\n\r\n if (isDomNode.isElementNode(node)) {\r\n let i;\r\n let pfxCopy;\r\n const ns = this.renderNs(\r\n node,\r\n prefixesInScope,\r\n defaultNs,\r\n defaultNsForPrefix,\r\n ancestorNamespaces,\r\n );\r\n const res = [\"<\", node.tagName, ns.rendered, this.renderAttrs(node), \">\"];\r\n\r\n for (i = 0; i < node.childNodes.length; ++i) {\r\n pfxCopy = prefixesInScope.slice(0);\r\n res.push(\r\n this.processInner(node.childNodes[i], pfxCopy, ns.newDefaultNs, defaultNsForPrefix, []),\r\n );\r\n }\r\n\r\n res.push(\"</\", node.tagName, \">\");\r\n return res.join(\"\");\r\n }\r\n\r\n throw new Error(`Unable to canonicalize node type: ${node.nodeType}`);\r\n }\r\n\r\n // Thanks to deoxxa/xml-c14n for comment renderer\r\n renderComment(node: Comment) {\r\n if (!this.includeComments) {\r\n return \"\";\r\n }\r\n\r\n const isOutsideDocument = node.ownerDocument === node.parentNode;\r\n let isBeforeDocument = false;\r\n let isAfterDocument = false;\r\n\r\n if (isOutsideDocument) {\r\n let nextNode: ChildNode | null = node;\r\n let previousNode: ChildNode | null = node;\r\n\r\n while (nextNode !== null) {\r\n if (nextNode === node.ownerDocument.documentElement) {\r\n isBeforeDocument = true;\r\n break;\r\n }\r\n\r\n nextNode = nextNode.nextSibling;\r\n }\r\n\r\n while (previousNode !== null) {\r\n if (previousNode === node.ownerDocument.documentElement) {\r\n isAfterDocument = true;\r\n break;\r\n }\r\n\r\n previousNode = previousNode.previousSibling;\r\n }\r\n }\r\n\r\n const afterDocument = isAfterDocument ? \"\\n\" : \"\";\r\n const beforeDocument = isBeforeDocument ? \"\\n\" : \"\";\r\n const encodedText = utils.encodeSpecialCharactersInText(node.data);\r\n\r\n return `${afterDocument}<!--${encodedText}-->${beforeDocument}`;\r\n }\r\n\r\n /**\r\n * Perform canonicalization of the given node\r\n *\r\n * @param node\r\n * @api public\r\n */\r\n process(node: Node, options: CanonicalizationOrTransformationAlgorithmProcessOptions): string {\r\n options = options || {};\r\n const defaultNs = options.defaultNs || \"\";\r\n const defaultNsForPrefix = options.defaultNsForPrefix || {};\r\n const ancestorNamespaces = options.ancestorNamespaces || [];\r\n\r\n const prefixesInScope: string[] = [];\r\n for (let i = 0; i < ancestorNamespaces.length; i++) {\r\n prefixesInScope.push(ancestorNamespaces[i].prefix);\r\n }\r\n\r\n const res = this.processInner(\r\n node,\r\n prefixesInScope,\r\n defaultNs,\r\n defaultNsForPrefix,\r\n ancestorNamespaces,\r\n );\r\n return res;\r\n }\r\n\r\n getAlgorithmName() {\r\n return \"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\";\r\n }\r\n}\r\n\r\n/**\r\n * Add c14n#WithComments here (very simple subclass)\r\n */\r\nexport class C14nCanonicalizationWithComments extends C14nCanonicalization {\r\n constructor() {\r\n super();\r\n this.includeComments = true;\r\n }\r\n\r\n getAlgorithmName() {\r\n return \"http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments\";\r\n }\r\n}\r\n"]}
@@ -0,0 +1,7 @@
1
+ import type { CanonicalizationOrTransformationAlgorithm, CanonicalizationOrTransformationAlgorithmProcessOptions, CanonicalizationOrTransformAlgorithmType } from "./types";
2
+ export declare class EnvelopedSignature implements CanonicalizationOrTransformationAlgorithm {
3
+ protected includeComments: boolean;
4
+ constructor();
5
+ process(node: Node, options: CanonicalizationOrTransformationAlgorithmProcessOptions): Node;
6
+ getAlgorithmName(): CanonicalizationOrTransformAlgorithmType;
7
+ }
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EnvelopedSignature = void 0;
4
+ const xpath = require("xpath");
5
+ const isDomNode = require("@xmldom/is-dom-node");
6
+ class EnvelopedSignature {
7
+ constructor() {
8
+ this.includeComments = false;
9
+ this.includeComments = false;
10
+ }
11
+ process(node, options) {
12
+ if (null == options.signatureNode) {
13
+ const signature = xpath.select1("./*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']", node);
14
+ if (isDomNode.isNodeLike(signature) && signature.parentNode) {
15
+ signature.parentNode.removeChild(signature);
16
+ }
17
+ return node;
18
+ }
19
+ const signatureNode = options.signatureNode;
20
+ const expectedSignatureValue = xpath.select1(".//*[local-name(.)='SignatureValue']/text()", signatureNode);
21
+ if (isDomNode.isTextNode(expectedSignatureValue)) {
22
+ const expectedSignatureValueData = expectedSignatureValue.data;
23
+ const signatures = xpath.select(".//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']", node);
24
+ for (const nodeSignature of Array.isArray(signatures) ? signatures : []) {
25
+ const signatureValue = xpath.select1(".//*[local-name(.)='SignatureValue']/text()", nodeSignature);
26
+ if (isDomNode.isTextNode(signatureValue)) {
27
+ const signatureValueData = signatureValue.data;
28
+ if (expectedSignatureValueData === signatureValueData) {
29
+ if (nodeSignature.parentNode) {
30
+ nodeSignature.parentNode.removeChild(nodeSignature);
31
+ }
32
+ }
33
+ }
34
+ }
35
+ }
36
+ return node;
37
+ }
38
+ getAlgorithmName() {
39
+ return "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
40
+ }
41
+ }
42
+ exports.EnvelopedSignature = EnvelopedSignature;
43
+ //# sourceMappingURL=enveloped-signature.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enveloped-signature.js","sourceRoot":"","sources":["../src/enveloped-signature.ts"],"names":[],"mappings":";;;AAAA,+BAA+B;AAC/B,iDAAiD;AAQjD,MAAa,kBAAkB;IAG7B;QAFU,oBAAe,GAAG,KAAK,CAAC;QAGhC,IAAI,CAAC,eAAe,GAAG,KAAK,CAAC;IAC/B,CAAC;IAED,OAAO,CAAC,IAAU,EAAE,OAAgE;QAClF,IAAI,IAAI,IAAI,OAAO,CAAC,aAAa,EAAE,CAAC;YAClC,MAAM,SAAS,GAAG,KAAK,CAAC,OAAO,CAC7B,0FAA0F,EAC1F,IAAI,CACL,CAAC;YACF,IAAI,SAAS,CAAC,UAAU,CAAC,SAAS,CAAC,IAAI,SAAS,CAAC,UAAU,EAAE,CAAC;gBAC5D,SAAS,CAAC,UAAU,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;YAC9C,CAAC;YACD,OAAO,IAAI,CAAC;QACd,CAAC;QACD,MAAM,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;QAC5C,MAAM,sBAAsB,GAAG,KAAK,CAAC,OAAO,CAC1C,6CAA6C,EAC7C,aAAa,CACd,CAAC;QACF,IAAI,SAAS,CAAC,UAAU,CAAC,sBAAsB,CAAC,EAAE,CAAC;YACjD,MAAM,0BAA0B,GAAG,sBAAsB,CAAC,IAAI,CAAC;YAE/D,MAAM,UAAU,GAAG,KAAK,CAAC,MAAM,CAC7B,2FAA2F,EAC3F,IAAI,CACL,CAAC;YACF,KAAK,MAAM,aAAa,IAAI,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;gBACxE,MAAM,cAAc,GAAG,KAAK,CAAC,OAAO,CAClC,6CAA6C,EAC7C,aAAa,CACd,CAAC;gBACF,IAAI,SAAS,CAAC,UAAU,CAAC,cAAc,CAAC,EAAE,CAAC;oBACzC,MAAM,kBAAkB,GAAG,cAAc,CAAC,IAAI,CAAC;oBAC/C,IAAI,0BAA0B,KAAK,kBAAkB,EAAE,CAAC;wBACtD,IAAI,aAAa,CAAC,UAAU,EAAE,CAAC;4BAC7B,aAAa,CAAC,UAAU,CAAC,WAAW,CAAC,aAAa,CAAC,CAAC;wBACtD,CAAC;oBACH,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,gBAAgB;QACd,OAAO,uDAAuD,CAAC;IACjE,CAAC;CACF;AAnDD,gDAmDC","sourcesContent":["import * as xpath from \"xpath\";\r\nimport * as isDomNode from \"@xmldom/is-dom-node\";\r\n\r\nimport type {\r\n CanonicalizationOrTransformationAlgorithm,\r\n CanonicalizationOrTransformationAlgorithmProcessOptions,\r\n CanonicalizationOrTransformAlgorithmType,\r\n} from \"./types\";\r\n\r\nexport class EnvelopedSignature implements CanonicalizationOrTransformationAlgorithm {\r\n protected includeComments = false;\r\n\r\n constructor() {\r\n this.includeComments = false;\r\n }\r\n\r\n process(node: Node, options: CanonicalizationOrTransformationAlgorithmProcessOptions): Node {\r\n if (null == options.signatureNode) {\r\n const signature = xpath.select1(\r\n \"./*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']\",\r\n node,\r\n );\r\n if (isDomNode.isNodeLike(signature) && signature.parentNode) {\r\n signature.parentNode.removeChild(signature);\r\n }\r\n return node;\r\n }\r\n const signatureNode = options.signatureNode;\r\n const expectedSignatureValue = xpath.select1(\r\n \".//*[local-name(.)='SignatureValue']/text()\",\r\n signatureNode,\r\n );\r\n if (isDomNode.isTextNode(expectedSignatureValue)) {\r\n const expectedSignatureValueData = expectedSignatureValue.data;\r\n\r\n const signatures = xpath.select(\r\n \".//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']\",\r\n node,\r\n );\r\n for (const nodeSignature of Array.isArray(signatures) ? signatures : []) {\r\n const signatureValue = xpath.select1(\r\n \".//*[local-name(.)='SignatureValue']/text()\",\r\n nodeSignature,\r\n );\r\n if (isDomNode.isTextNode(signatureValue)) {\r\n const signatureValueData = signatureValue.data;\r\n if (expectedSignatureValueData === signatureValueData) {\r\n if (nodeSignature.parentNode) {\r\n nodeSignature.parentNode.removeChild(nodeSignature);\r\n }\r\n }\r\n }\r\n }\r\n }\r\n return node;\r\n }\r\n\r\n getAlgorithmName(): CanonicalizationOrTransformAlgorithmType {\r\n return \"http://www.w3.org/2000/09/xmldsig#enveloped-signature\";\r\n }\r\n}\r\n"]}
@@ -0,0 +1,38 @@
1
+ import type { CanonicalizationOrTransformationAlgorithm, CanonicalizationOrTransformationAlgorithmProcessOptions } from "./types";
2
+ export declare class ExclusiveCanonicalization implements CanonicalizationOrTransformationAlgorithm {
3
+ protected includeComments: boolean;
4
+ constructor();
5
+ attrCompare(a: any, b: any): 1 | 0 | -1;
6
+ nsCompare(a: any, b: any): any;
7
+ renderAttrs(node: any): string;
8
+ /**
9
+ * Create the string of all namespace declarations that should appear on this element
10
+ *
11
+ * @param {Node} node. The node we now render
12
+ * @param {Array} prefixesInScope. The prefixes defined on this node
13
+ * parents which are a part of the output set
14
+ * @param {String} defaultNs. The current default namespace
15
+ * @return {String}
16
+ * @api private
17
+ */
18
+ renderNs(node: any, prefixesInScope: any, defaultNs: any, defaultNsForPrefix: any, inclusiveNamespacesPrefixList: string[]): {
19
+ rendered: string;
20
+ newDefaultNs: any;
21
+ };
22
+ /**
23
+ * @param node Node
24
+ */
25
+ processInner(node: any, prefixesInScope: any, defaultNs: any, defaultNsForPrefix: any, inclusiveNamespacesPrefixList: string[]): string;
26
+ renderComment(node: Comment): string;
27
+ /**
28
+ * Perform canonicalization of the given element node
29
+ *
30
+ * @api public
31
+ */
32
+ process(elem: Element, options: CanonicalizationOrTransformationAlgorithmProcessOptions): string;
33
+ getAlgorithmName(): string;
34
+ }
35
+ export declare class ExclusiveCanonicalizationWithComments extends ExclusiveCanonicalization {
36
+ constructor();
37
+ getAlgorithmName(): string;
38
+ }
@@ -0,0 +1,246 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ExclusiveCanonicalizationWithComments = exports.ExclusiveCanonicalization = void 0;
4
+ const utils = require("./utils");
5
+ const isDomNode = require("@xmldom/is-dom-node");
6
+ function isPrefixInScope(prefixesInScope, prefix, namespaceURI) {
7
+ let ret = false;
8
+ prefixesInScope.forEach(function (pf) {
9
+ if (pf.prefix === prefix && pf.namespaceURI === namespaceURI) {
10
+ ret = true;
11
+ }
12
+ });
13
+ return ret;
14
+ }
15
+ class ExclusiveCanonicalization {
16
+ constructor() {
17
+ this.includeComments = false;
18
+ this.includeComments = false;
19
+ }
20
+ attrCompare(a, b) {
21
+ if (!a.namespaceURI && b.namespaceURI) {
22
+ return -1;
23
+ }
24
+ if (!b.namespaceURI && a.namespaceURI) {
25
+ return 1;
26
+ }
27
+ const left = a.namespaceURI + a.localName;
28
+ const right = b.namespaceURI + b.localName;
29
+ if (left === right) {
30
+ return 0;
31
+ }
32
+ else if (left < right) {
33
+ return -1;
34
+ }
35
+ else {
36
+ return 1;
37
+ }
38
+ }
39
+ nsCompare(a, b) {
40
+ const attr1 = a.prefix;
41
+ const attr2 = b.prefix;
42
+ if (attr1 === attr2) {
43
+ return 0;
44
+ }
45
+ return attr1.localeCompare(attr2);
46
+ }
47
+ renderAttrs(node) {
48
+ let i;
49
+ let attr;
50
+ const res = [];
51
+ const attrListToRender = [];
52
+ if (isDomNode.isCommentNode(node)) {
53
+ return this.renderComment(node);
54
+ }
55
+ if (node.attributes) {
56
+ for (i = 0; i < node.attributes.length; ++i) {
57
+ attr = node.attributes[i];
58
+ //ignore namespace definition attributes
59
+ if (attr.name.indexOf("xmlns") === 0) {
60
+ continue;
61
+ }
62
+ attrListToRender.push(attr);
63
+ }
64
+ }
65
+ attrListToRender.sort(this.attrCompare);
66
+ for (attr of attrListToRender) {
67
+ res.push(" ", attr.name, '="', utils.encodeSpecialCharactersInAttribute(attr.value), '"');
68
+ }
69
+ return res.join("");
70
+ }
71
+ /**
72
+ * Create the string of all namespace declarations that should appear on this element
73
+ *
74
+ * @param {Node} node. The node we now render
75
+ * @param {Array} prefixesInScope. The prefixes defined on this node
76
+ * parents which are a part of the output set
77
+ * @param {String} defaultNs. The current default namespace
78
+ * @return {String}
79
+ * @api private
80
+ */
81
+ renderNs(node, prefixesInScope, defaultNs, defaultNsForPrefix, inclusiveNamespacesPrefixList) {
82
+ let i;
83
+ let attr;
84
+ const res = [];
85
+ let newDefaultNs = defaultNs;
86
+ const nsListToRender = [];
87
+ const currNs = node.namespaceURI || "";
88
+ //handle the namespaceof the node itself
89
+ if (node.prefix) {
90
+ if (!isPrefixInScope(prefixesInScope, node.prefix, node.namespaceURI || defaultNsForPrefix[node.prefix])) {
91
+ nsListToRender.push({
92
+ prefix: node.prefix,
93
+ namespaceURI: node.namespaceURI || defaultNsForPrefix[node.prefix],
94
+ });
95
+ prefixesInScope.push({
96
+ prefix: node.prefix,
97
+ namespaceURI: node.namespaceURI || defaultNsForPrefix[node.prefix],
98
+ });
99
+ }
100
+ }
101
+ else if (defaultNs !== currNs) {
102
+ //new default ns
103
+ newDefaultNs = node.namespaceURI;
104
+ res.push(' xmlns="', newDefaultNs, '"');
105
+ }
106
+ //handle the attributes namespace
107
+ if (node.attributes) {
108
+ for (i = 0; i < node.attributes.length; ++i) {
109
+ attr = node.attributes[i];
110
+ //handle all prefixed attributes that are included in the prefix list and where
111
+ //the prefix is not defined already
112
+ if (attr.prefix &&
113
+ !isPrefixInScope(prefixesInScope, attr.localName, attr.value) &&
114
+ inclusiveNamespacesPrefixList.indexOf(attr.localName) >= 0) {
115
+ nsListToRender.push({ prefix: attr.localName, namespaceURI: attr.value });
116
+ prefixesInScope.push({ prefix: attr.localName, namespaceURI: attr.value });
117
+ }
118
+ //handle all prefixed attributes that are not xmlns definitions and where
119
+ //the prefix is not defined already
120
+ if (attr.prefix &&
121
+ !isPrefixInScope(prefixesInScope, attr.prefix, attr.namespaceURI) &&
122
+ attr.prefix !== "xmlns" &&
123
+ attr.prefix !== "xml") {
124
+ nsListToRender.push({ prefix: attr.prefix, namespaceURI: attr.namespaceURI });
125
+ prefixesInScope.push({ prefix: attr.prefix, namespaceURI: attr.namespaceURI });
126
+ }
127
+ }
128
+ }
129
+ nsListToRender.sort(this.nsCompare);
130
+ //render namespaces
131
+ for (const p of nsListToRender) {
132
+ res.push(" xmlns:", p.prefix, '="', p.namespaceURI, '"');
133
+ }
134
+ return { rendered: res.join(""), newDefaultNs: newDefaultNs };
135
+ }
136
+ /**
137
+ * @param node Node
138
+ */
139
+ processInner(node, prefixesInScope, defaultNs, defaultNsForPrefix, inclusiveNamespacesPrefixList) {
140
+ if (isDomNode.isCommentNode(node)) {
141
+ return this.renderComment(node);
142
+ }
143
+ if (node.data) {
144
+ return utils.encodeSpecialCharactersInText(node.data);
145
+ }
146
+ if (isDomNode.isElementNode(node)) {
147
+ let i;
148
+ let pfxCopy;
149
+ const ns = this.renderNs(node, prefixesInScope, defaultNs, defaultNsForPrefix, inclusiveNamespacesPrefixList);
150
+ const res = ["<", node.tagName, ns.rendered, this.renderAttrs(node), ">"];
151
+ for (i = 0; i < node.childNodes.length; ++i) {
152
+ pfxCopy = prefixesInScope.slice(0);
153
+ res.push(this.processInner(node.childNodes[i], pfxCopy, ns.newDefaultNs, defaultNsForPrefix, inclusiveNamespacesPrefixList));
154
+ }
155
+ res.push("</", node.tagName, ">");
156
+ return res.join("");
157
+ }
158
+ throw new Error(`Unable to exclusive canonicalize node type: ${node.nodeType}`);
159
+ }
160
+ // Thanks to deoxxa/xml-c14n for comment renderer
161
+ renderComment(node) {
162
+ if (!this.includeComments) {
163
+ return "";
164
+ }
165
+ const isOutsideDocument = node.ownerDocument === node.parentNode;
166
+ let isBeforeDocument = false;
167
+ let isAfterDocument = false;
168
+ if (isOutsideDocument) {
169
+ let nextNode = node;
170
+ let previousNode = node;
171
+ while (nextNode != null) {
172
+ if (nextNode === node.ownerDocument.documentElement) {
173
+ isBeforeDocument = true;
174
+ break;
175
+ }
176
+ nextNode = nextNode.nextSibling;
177
+ }
178
+ while (previousNode != null) {
179
+ if (previousNode === node.ownerDocument.documentElement) {
180
+ isAfterDocument = true;
181
+ break;
182
+ }
183
+ previousNode = previousNode.previousSibling;
184
+ }
185
+ }
186
+ const afterDocument = isAfterDocument ? "\n" : "";
187
+ const beforeDocument = isBeforeDocument ? "\n" : "";
188
+ const encodedText = utils.encodeSpecialCharactersInText(node.data);
189
+ return `${afterDocument}<!--${encodedText}-->${beforeDocument}`;
190
+ }
191
+ /**
192
+ * Perform canonicalization of the given element node
193
+ *
194
+ * @api public
195
+ */
196
+ process(elem, options) {
197
+ options = options || {};
198
+ let inclusiveNamespacesPrefixList = options.inclusiveNamespacesPrefixList || [];
199
+ const defaultNs = options.defaultNs || "";
200
+ const defaultNsForPrefix = options.defaultNsForPrefix || {};
201
+ const ancestorNamespaces = options.ancestorNamespaces || [];
202
+ /**
203
+ * If the inclusiveNamespacesPrefixList has not been explicitly provided then look it up in CanonicalizationMethod/InclusiveNamespaces
204
+ */
205
+ if (!utils.isArrayHasLength(inclusiveNamespacesPrefixList)) {
206
+ const CanonicalizationMethod = utils.findChildren(elem, "CanonicalizationMethod");
207
+ if (CanonicalizationMethod.length !== 0) {
208
+ const inclusiveNamespaces = utils.findChildren(CanonicalizationMethod[0], "InclusiveNamespaces");
209
+ if (inclusiveNamespaces.length !== 0) {
210
+ inclusiveNamespacesPrefixList = (inclusiveNamespaces[0].getAttribute("PrefixList") || "").split(" ");
211
+ }
212
+ }
213
+ }
214
+ /**
215
+ * If you have a PrefixList then use it and the ancestors to add the necessary namespaces
216
+ */
217
+ if (utils.isArrayHasLength(inclusiveNamespacesPrefixList)) {
218
+ inclusiveNamespacesPrefixList.forEach(function (prefix) {
219
+ if (ancestorNamespaces) {
220
+ ancestorNamespaces.forEach(function (ancestorNamespace) {
221
+ if (prefix === ancestorNamespace.prefix) {
222
+ elem.setAttributeNS("http://www.w3.org/2000/xmlns/", `xmlns:${prefix}`, ancestorNamespace.namespaceURI);
223
+ }
224
+ });
225
+ }
226
+ });
227
+ }
228
+ const res = this.processInner(elem, [], defaultNs, defaultNsForPrefix, inclusiveNamespacesPrefixList);
229
+ return res;
230
+ }
231
+ getAlgorithmName() {
232
+ return "http://www.w3.org/2001/10/xml-exc-c14n#";
233
+ }
234
+ }
235
+ exports.ExclusiveCanonicalization = ExclusiveCanonicalization;
236
+ class ExclusiveCanonicalizationWithComments extends ExclusiveCanonicalization {
237
+ constructor() {
238
+ super();
239
+ this.includeComments = true;
240
+ }
241
+ getAlgorithmName() {
242
+ return "http://www.w3.org/2001/10/xml-exc-c14n#WithComments";
243
+ }
244
+ }
245
+ exports.ExclusiveCanonicalizationWithComments = ExclusiveCanonicalizationWithComments;
246
+ //# sourceMappingURL=exclusive-canonicalization.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exclusive-canonicalization.js","sourceRoot":"","sources":["../src/exclusive-canonicalization.ts"],"names":[],"mappings":";;;AAKA,iCAAiC;AACjC,iDAAiD;AAEjD,SAAS,eAAe,CAAC,eAAe,EAAE,MAAM,EAAE,YAAY;IAC5D,IAAI,GAAG,GAAG,KAAK,CAAC;IAChB,eAAe,CAAC,OAAO,CAAC,UAAU,EAAE;QAClC,IAAI,EAAE,CAAC,MAAM,KAAK,MAAM,IAAI,EAAE,CAAC,YAAY,KAAK,YAAY,EAAE,CAAC;YAC7D,GAAG,GAAG,IAAI,CAAC;QACb,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,GAAG,CAAC;AACb,CAAC;AAED,MAAa,yBAAyB;IAGpC;QAFU,oBAAe,GAAG,KAAK,CAAC;QAGhC,IAAI,CAAC,eAAe,GAAG,KAAK,CAAC;IAC/B,CAAC;IAED,WAAW,CAAC,CAAC,EAAE,CAAC;QACd,IAAI,CAAC,CAAC,CAAC,YAAY,IAAI,CAAC,CAAC,YAAY,EAAE,CA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ED,gBAAgB;QACd,OAAO,qDAAqD,CAAC;IAC/D,CAAC;CACF;AATD,sFASC","sourcesContent":["import type {\r\n CanonicalizationOrTransformationAlgorithm,\r\n CanonicalizationOrTransformationAlgorithmProcessOptions,\r\n NamespacePrefix,\r\n} from \"./types\";\r\nimport * as utils from \"./utils\";\r\nimport * as isDomNode from \"@xmldom/is-dom-node\";\r\n\r\nfunction isPrefixInScope(prefixesInScope, prefix, namespaceURI) {\r\n let ret = false;\r\n prefixesInScope.forEach(function (pf) {\r\n if (pf.prefix === prefix && pf.namespaceURI === namespaceURI) {\r\n ret = true;\r\n }\r\n });\r\n\r\n return ret;\r\n}\r\n\r\nexport class ExclusiveCanonicalization implements CanonicalizationOrTransformationAlgorithm {\r\n protected includeComments = false;\r\n\r\n constructor() {\r\n this.includeComments = false;\r\n }\r\n\r\n attrCompare(a, b) {\r\n if (!a.namespaceURI && b.namespaceURI) {\r\n return -1;\r\n }\r\n if (!b.namespaceURI && a.namespaceURI) {\r\n return 1;\r\n }\r\n\r\n const left = a.namespaceURI + a.localName;\r\n const right = b.namespaceURI + b.localName;\r\n\r\n if (left === right) {\r\n return 0;\r\n } else if (left < right) {\r\n return -1;\r\n } else {\r\n return 1;\r\n }\r\n }\r\n\r\n nsCompare(a, b) {\r\n const attr1 = a.prefix;\r\n const attr2 = b.prefix;\r\n if (attr1 === attr2) {\r\n return 0;\r\n }\r\n return attr1.localeCompare(attr2);\r\n }\r\n\r\n renderAttrs(node) {\r\n let i;\r\n let attr;\r\n const res: string[] = [];\r\n const attrListToRender: Attr[] = [];\r\n\r\n if (isDomNode.isCommentNode(node)) {\r\n return this.renderComment(node);\r\n }\r\n\r\n if (node.attributes) {\r\n for (i = 0; i < node.attributes.length; ++i) {\r\n attr = node.attributes[i];\r\n //ignore namespace definition attributes\r\n if (attr.name.indexOf(\"xmlns\") === 0) {\r\n continue;\r\n }\r\n attrListToRender.push(attr);\r\n }\r\n }\r\n\r\n attrListToRender.sort(this.attrCompare);\r\n\r\n for (attr of attrListToRender) {\r\n res.push(\" \", attr.name, '=\"', utils.encodeSpecialCharactersInAttribute(attr.value), '\"');\r\n }\r\n\r\n return res.join(\"\");\r\n }\r\n\r\n /**\r\n * Create the string of all namespace declarations that should appear on this element\r\n *\r\n * @param {Node} node. The node we now render\r\n * @param {Array} prefixesInScope. The prefixes defined on this node\r\n * parents which are a part of the output set\r\n * @param {String} defaultNs. The current default namespace\r\n * @return {String}\r\n * @api private\r\n */\r\n renderNs(\r\n node,\r\n prefixesInScope,\r\n defaultNs,\r\n defaultNsForPrefix,\r\n inclusiveNamespacesPrefixList: string[],\r\n ) {\r\n let i;\r\n let attr;\r\n const res: string[] = [];\r\n let newDefaultNs = defaultNs;\r\n const nsListToRender: NamespacePrefix[] = [];\r\n const currNs = node.namespaceURI || \"\";\r\n\r\n //handle the namespaceof the node itself\r\n if (node.prefix) {\r\n if (\r\n !isPrefixInScope(\r\n prefixesInScope,\r\n node.prefix,\r\n node.namespaceURI || defaultNsForPrefix[node.prefix],\r\n )\r\n ) {\r\n nsListToRender.push({\r\n prefix: node.prefix,\r\n namespaceURI: node.namespaceURI || defaultNsForPrefix[node.prefix],\r\n });\r\n prefixesInScope.push({\r\n prefix: node.prefix,\r\n namespaceURI: node.namespaceURI || defaultNsForPrefix[node.prefix],\r\n });\r\n }\r\n } else if (defaultNs !== currNs) {\r\n //new default ns\r\n newDefaultNs = node.namespaceURI;\r\n res.push(' xmlns=\"', newDefaultNs, '\"');\r\n }\r\n\r\n //handle the attributes namespace\r\n if (node.attributes) {\r\n for (i = 0; i < node.attributes.length; ++i) {\r\n attr = node.attributes[i];\r\n\r\n //handle all prefixed attributes that are included in the prefix list and where\r\n //the prefix is not defined already\r\n if (\r\n attr.prefix &&\r\n !isPrefixInScope(prefixesInScope, attr.localName, attr.value) &&\r\n inclusiveNamespacesPrefixList.indexOf(attr.localName) >= 0\r\n ) {\r\n nsListToRender.push({ prefix: attr.localName, namespaceURI: attr.value });\r\n prefixesInScope.push({ prefix: attr.localName, namespaceURI: attr.value });\r\n }\r\n\r\n //handle all prefixed attributes that are not xmlns definitions and where\r\n //the prefix is not defined already\r\n if (\r\n attr.prefix &&\r\n !isPrefixInScope(prefixesInScope, attr.prefix, attr.namespaceURI) &&\r\n attr.prefix !== \"xmlns\" &&\r\n attr.prefix !== \"xml\"\r\n ) {\r\n nsListToRender.push({ prefix: attr.prefix, namespaceURI: attr.namespaceURI });\r\n prefixesInScope.push({ prefix: attr.prefix, namespaceURI: attr.namespaceURI });\r\n }\r\n }\r\n }\r\n\r\n nsListToRender.sort(this.nsCompare);\r\n\r\n //render namespaces\r\n for (const p of nsListToRender) {\r\n res.push(\" xmlns:\", p.prefix, '=\"', p.namespaceURI, '\"');\r\n }\r\n\r\n return { rendered: res.join(\"\"), newDefaultNs: newDefaultNs };\r\n }\r\n\r\n /**\r\n * @param node Node\r\n */\r\n processInner(\r\n node,\r\n prefixesInScope,\r\n defaultNs,\r\n defaultNsForPrefix,\r\n inclusiveNamespacesPrefixList: string[],\r\n ) {\r\n if (isDomNode.isCommentNode(node)) {\r\n return this.renderComment(node);\r\n }\r\n if (node.data) {\r\n return utils.encodeSpecialCharactersInText(node.data);\r\n }\r\n\r\n if (isDomNode.isElementNode(node)) {\r\n let i;\r\n let pfxCopy;\r\n const ns = this.renderNs(\r\n node,\r\n prefixesInScope,\r\n defaultNs,\r\n defaultNsForPrefix,\r\n inclusiveNamespacesPrefixList,\r\n );\r\n const res = [\"<\", node.tagName, ns.rendered, this.renderAttrs(node), \">\"];\r\n\r\n for (i = 0; i < node.childNodes.length; ++i) {\r\n pfxCopy = prefixesInScope.slice(0);\r\n res.push(\r\n this.processInner(\r\n node.childNodes[i],\r\n pfxCopy,\r\n ns.newDefaultNs,\r\n defaultNsForPrefix,\r\n inclusiveNamespacesPrefixList,\r\n ),\r\n );\r\n }\r\n\r\n res.push(\"</\", node.tagName, \">\");\r\n return res.join(\"\");\r\n }\r\n\r\n throw new Error(`Unable to exclusive canonicalize node type: ${node.nodeType}`);\r\n }\r\n\r\n // Thanks to deoxxa/xml-c14n for comment renderer\r\n renderComment(node: Comment) {\r\n if (!this.includeComments) {\r\n return \"\";\r\n }\r\n\r\n const isOutsideDocument = node.ownerDocument === node.parentNode;\r\n let isBeforeDocument = false;\r\n let isAfterDocument = false;\r\n\r\n if (isOutsideDocument) {\r\n let nextNode: ChildNode | null = node;\r\n let previousNode: ChildNode | null = node;\r\n\r\n while (nextNode != null) {\r\n if (nextNode === node.ownerDocument.documentElement) {\r\n isBeforeDocument = true;\r\n break;\r\n }\r\n\r\n nextNode = nextNode.nextSibling;\r\n }\r\n\r\n while (previousNode != null) {\r\n if (previousNode === node.ownerDocument.documentElement) {\r\n isAfterDocument = true;\r\n break;\r\n }\r\n\r\n previousNode = previousNode.previousSibling;\r\n }\r\n }\r\n\r\n const afterDocument = isAfterDocument ? \"\\n\" : \"\";\r\n const beforeDocument = isBeforeDocument ? \"\\n\" : \"\";\r\n const encodedText = utils.encodeSpecialCharactersInText(node.data);\r\n\r\n return `${afterDocument}<!--${encodedText}-->${beforeDocument}`;\r\n }\r\n\r\n /**\r\n * Perform canonicalization of the given element node\r\n *\r\n * @api public\r\n */\r\n process(elem: Element, options: CanonicalizationOrTransformationAlgorithmProcessOptions): string {\r\n options = options || {};\r\n let inclusiveNamespacesPrefixList = options.inclusiveNamespacesPrefixList || [];\r\n const defaultNs = options.defaultNs || \"\";\r\n const defaultNsForPrefix = options.defaultNsForPrefix || {};\r\n const ancestorNamespaces = options.ancestorNamespaces || [];\r\n\r\n /**\r\n * If the inclusiveNamespacesPrefixList has not been explicitly provided then look it up in CanonicalizationMethod/InclusiveNamespaces\r\n */\r\n if (!utils.isArrayHasLength(inclusiveNamespacesPrefixList)) {\r\n const CanonicalizationMethod = utils.findChildren(elem, \"CanonicalizationMethod\");\r\n if (CanonicalizationMethod.length !== 0) {\r\n const inclusiveNamespaces = utils.findChildren(\r\n CanonicalizationMethod[0],\r\n \"InclusiveNamespaces\",\r\n );\r\n if (inclusiveNamespaces.length !== 0) {\r\n inclusiveNamespacesPrefixList = (\r\n inclusiveNamespaces[0].getAttribute(\"PrefixList\") || \"\"\r\n ).split(\" \");\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * If you have a PrefixList then use it and the ancestors to add the necessary namespaces\r\n */\r\n if (utils.isArrayHasLength(inclusiveNamespacesPrefixList)) {\r\n inclusiveNamespacesPrefixList.forEach(function (prefix) {\r\n if (ancestorNamespaces) {\r\n ancestorNamespaces.forEach(function (ancestorNamespace) {\r\n if (prefix === ancestorNamespace.prefix) {\r\n elem.setAttributeNS(\r\n \"http://www.w3.org/2000/xmlns/\",\r\n `xmlns:${prefix}`,\r\n ancestorNamespace.namespaceURI,\r\n );\r\n }\r\n });\r\n }\r\n });\r\n }\r\n\r\n const res = this.processInner(\r\n elem,\r\n [],\r\n defaultNs,\r\n defaultNsForPrefix,\r\n inclusiveNamespacesPrefixList,\r\n );\r\n return res;\r\n }\r\n\r\n getAlgorithmName() {\r\n return \"http://www.w3.org/2001/10/xml-exc-c14n#\";\r\n }\r\n}\r\n\r\nexport class ExclusiveCanonicalizationWithComments extends ExclusiveCanonicalization {\r\n constructor() {\r\n super();\r\n this.includeComments = true;\r\n }\r\n\r\n getAlgorithmName() {\r\n return \"http://www.w3.org/2001/10/xml-exc-c14n#WithComments\";\r\n }\r\n}\r\n"]}