xslt-processor 3.0.1 → 3.1.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.
Files changed (226) hide show
  1. package/constants.d.ts +12 -12
  2. package/constants.js +16 -16
  3. package/dom/functions.d.ts +14 -14
  4. package/dom/functions.js +56 -57
  5. package/dom/functions.js.map +1 -1
  6. package/dom/index.d.ts +6 -6
  7. package/dom/index.js +22 -22
  8. package/dom/util.d.ts +7 -7
  9. package/dom/util.js +42 -43
  10. package/dom/util.js.map +1 -1
  11. package/dom/xdocument.d.ts +16 -16
  12. package/dom/xdocument.js +73 -74
  13. package/dom/xdocument.js.map +1 -1
  14. package/dom/xml-functions.d.ts +58 -58
  15. package/dom/xml-functions.js +369 -370
  16. package/dom/xml-functions.js.map +1 -1
  17. package/dom/xml-output-options.d.ts +6 -6
  18. package/dom/xml-output-options.js +2 -2
  19. package/dom/xml-parser.d.ts +47 -47
  20. package/dom/xml-parser.js +307 -307
  21. package/dom/xml-parser.js.map +1 -1
  22. package/dom/xmltoken.d.ts +12 -12
  23. package/dom/xmltoken.js +101 -101
  24. package/dom/xnode.d.ts +73 -73
  25. package/dom/xnode.js +450 -450
  26. package/dom/xnode.js.map +1 -1
  27. package/index.d.ts +4 -4
  28. package/index.js +12 -12
  29. package/package.json +7 -7
  30. package/test-without-jest.d.ts +1 -0
  31. package/test-without-jest.js +63 -0
  32. package/test-without-jest.js.map +1 -0
  33. package/umd/constants.d.ts +12 -12
  34. package/umd/dom/functions.d.ts +14 -14
  35. package/umd/dom/index.d.ts +6 -6
  36. package/umd/dom/util.d.ts +7 -7
  37. package/umd/dom/xdocument.d.ts +16 -16
  38. package/umd/dom/xml-functions.d.ts +58 -58
  39. package/umd/dom/xml-output-options.d.ts +6 -6
  40. package/umd/dom/xml-parser.d.ts +47 -47
  41. package/umd/dom/xmltoken.d.ts +12 -12
  42. package/umd/dom/xnode.d.ts +73 -73
  43. package/umd/index.d.ts +4 -4
  44. package/umd/test-without-jest.d.ts +1 -0
  45. package/umd/xpath/common-function.d.ts +8 -8
  46. package/umd/xpath/expr-context.d.ts +116 -111
  47. package/umd/xpath/expressions/binary-expr.d.ts +11 -11
  48. package/umd/xpath/expressions/expression.d.ts +4 -4
  49. package/umd/xpath/expressions/filter-expr.d.ts +9 -9
  50. package/umd/xpath/expressions/function-call-expr.d.ts +12 -12
  51. package/umd/xpath/expressions/index.d.ts +13 -13
  52. package/umd/xpath/expressions/literal-expr.d.ts +7 -7
  53. package/umd/xpath/expressions/location-expr.d.ts +15 -15
  54. package/umd/xpath/expressions/number-expr.d.ts +7 -7
  55. package/umd/xpath/expressions/path-expr.d.ts +9 -9
  56. package/umd/xpath/expressions/predicate-expr.d.ts +8 -8
  57. package/umd/xpath/expressions/step-expr.d.ts +25 -25
  58. package/umd/xpath/expressions/token-expr.d.ts +7 -7
  59. package/umd/xpath/expressions/unary-minus-expr.d.ts +8 -8
  60. package/umd/xpath/expressions/union-expr.d.ts +9 -9
  61. package/umd/xpath/expressions/variable-expr.d.ts +7 -7
  62. package/umd/xpath/functions/index.d.ts +4 -2
  63. package/umd/xpath/functions/internal-functions.d.ts +2 -2
  64. package/umd/xpath/functions/non-standard.d.ts +12 -12
  65. package/umd/xpath/functions/standard-20.d.ts +5 -5
  66. package/umd/xpath/functions/standard.d.ts +40 -40
  67. package/umd/xpath/functions/xslt-specific.d.ts +3 -0
  68. package/umd/xpath/grammar-rule-candidate.d.ts +8 -8
  69. package/umd/xpath/index.d.ts +3 -3
  70. package/umd/xpath/match-resolver.d.ts +55 -55
  71. package/umd/xpath/node-tests/index.d.ts +8 -8
  72. package/umd/xpath/node-tests/node-test-any.d.ts +6 -6
  73. package/umd/xpath/node-tests/node-test-comment.d.ts +6 -6
  74. package/umd/xpath/node-tests/node-test-element-or-attribute.d.ts +6 -6
  75. package/umd/xpath/node-tests/node-test-name.d.ts +10 -10
  76. package/umd/xpath/node-tests/node-test-nc.d.ts +9 -9
  77. package/umd/xpath/node-tests/node-test-pi.d.ts +8 -8
  78. package/umd/xpath/node-tests/node-test-text.d.ts +6 -6
  79. package/umd/xpath/node-tests/node-test.d.ts +5 -5
  80. package/umd/xpath/tokens.d.ts +62 -62
  81. package/umd/xpath/values/boolean-value.d.ts +11 -11
  82. package/umd/xpath/values/index.d.ts +5 -5
  83. package/umd/xpath/values/node-set-value.d.ts +11 -11
  84. package/umd/xpath/values/node-value.d.ts +7 -7
  85. package/umd/xpath/values/number-value.d.ts +11 -11
  86. package/umd/xpath/values/string-value.d.ts +11 -11
  87. package/umd/xpath/xpath-grammar-rules.d.ts +68 -68
  88. package/umd/xpath/xpath-token-rule.d.ts +7 -7
  89. package/umd/xpath/xpath.d.ts +174 -174
  90. package/umd/xpathdebug.d.ts +2 -2
  91. package/umd/xslt/index.d.ts +3 -3
  92. package/umd/xslt/xslt-decimal-format-settings.d.ts +28 -28
  93. package/umd/xslt/xslt-options.d.ts +7 -7
  94. package/umd/xslt/xslt-parameter.d.ts +5 -5
  95. package/umd/xslt/xslt.d.ts +260 -207
  96. package/umd/xslt-processor.js +1 -15
  97. package/umd/xslt-processor.js.map +1 -1
  98. package/xpath/common-function.d.ts +8 -8
  99. package/xpath/common-function.js +31 -32
  100. package/xpath/common-function.js.map +1 -1
  101. package/xpath/expr-context.d.ts +116 -111
  102. package/xpath/expr-context.js +190 -189
  103. package/xpath/expr-context.js.map +1 -1
  104. package/xpath/expressions/binary-expr.d.ts +11 -11
  105. package/xpath/expressions/binary-expr.js +165 -165
  106. package/xpath/expressions/binary-expr.js.map +1 -1
  107. package/xpath/expressions/expression.d.ts +4 -4
  108. package/xpath/expressions/expression.js +9 -9
  109. package/xpath/expressions/filter-expr.d.ts +9 -9
  110. package/xpath/expressions/filter-expr.js +52 -52
  111. package/xpath/expressions/filter-expr.js.map +1 -1
  112. package/xpath/expressions/function-call-expr.d.ts +12 -12
  113. package/xpath/expressions/function-call-expr.js +96 -95
  114. package/xpath/expressions/function-call-expr.js.map +1 -1
  115. package/xpath/expressions/index.d.ts +13 -13
  116. package/xpath/expressions/index.js +29 -29
  117. package/xpath/expressions/literal-expr.d.ts +7 -7
  118. package/xpath/expressions/literal-expr.js +33 -33
  119. package/xpath/expressions/literal-expr.js.map +1 -1
  120. package/xpath/expressions/location-expr.d.ts +15 -15
  121. package/xpath/expressions/location-expr.js +98 -98
  122. package/xpath/expressions/location-expr.js.map +1 -1
  123. package/xpath/expressions/number-expr.d.ts +7 -7
  124. package/xpath/expressions/number-expr.js +33 -33
  125. package/xpath/expressions/number-expr.js.map +1 -1
  126. package/xpath/expressions/path-expr.d.ts +9 -9
  127. package/xpath/expressions/path-expr.js +51 -51
  128. package/xpath/expressions/path-expr.js.map +1 -1
  129. package/xpath/expressions/predicate-expr.d.ts +8 -8
  130. package/xpath/expressions/predicate-expr.js +40 -40
  131. package/xpath/expressions/predicate-expr.js.map +1 -1
  132. package/xpath/expressions/step-expr.d.ts +25 -25
  133. package/xpath/expressions/step-expr.js +280 -280
  134. package/xpath/expressions/step-expr.js.map +1 -1
  135. package/xpath/expressions/token-expr.d.ts +7 -7
  136. package/xpath/expressions/token-expr.js +33 -33
  137. package/xpath/expressions/token-expr.js.map +1 -1
  138. package/xpath/expressions/unary-minus-expr.d.ts +8 -8
  139. package/xpath/expressions/unary-minus-expr.js +33 -33
  140. package/xpath/expressions/unary-minus-expr.js.map +1 -1
  141. package/xpath/expressions/union-expr.d.ts +9 -9
  142. package/xpath/expressions/union-expr.js +50 -50
  143. package/xpath/expressions/union-expr.js.map +1 -1
  144. package/xpath/expressions/variable-expr.d.ts +7 -7
  145. package/xpath/expressions/variable-expr.js +32 -32
  146. package/xpath/expressions/variable-expr.js.map +1 -1
  147. package/xpath/functions/index.d.ts +4 -2
  148. package/xpath/functions/index.js +20 -18
  149. package/xpath/functions/index.js.map +1 -1
  150. package/xpath/functions/internal-functions.d.ts +2 -2
  151. package/xpath/functions/internal-functions.js +21 -22
  152. package/xpath/functions/internal-functions.js.map +1 -1
  153. package/xpath/functions/non-standard.d.ts +12 -12
  154. package/xpath/functions/non-standard.js +44 -45
  155. package/xpath/functions/non-standard.js.map +1 -1
  156. package/xpath/functions/standard-20.d.ts +5 -5
  157. package/xpath/functions/standard-20.js +25 -26
  158. package/xpath/functions/standard-20.js.map +1 -1
  159. package/xpath/functions/standard.d.ts +40 -40
  160. package/xpath/functions/standard.js +441 -442
  161. package/xpath/functions/standard.js.map +1 -1
  162. package/xpath/functions/xslt-specific.d.ts +3 -0
  163. package/xpath/functions/xslt-specific.js +14 -0
  164. package/xpath/functions/xslt-specific.js.map +1 -0
  165. package/xpath/grammar-rule-candidate.d.ts +8 -8
  166. package/xpath/grammar-rule-candidate.js +2 -2
  167. package/xpath/index.d.ts +3 -3
  168. package/xpath/index.js +19 -19
  169. package/xpath/match-resolver.d.ts +55 -55
  170. package/xpath/match-resolver.js +136 -136
  171. package/xpath/match-resolver.js.map +1 -1
  172. package/xpath/node-tests/index.d.ts +8 -8
  173. package/xpath/node-tests/index.js +17 -17
  174. package/xpath/node-tests/node-test-any.d.ts +6 -6
  175. package/xpath/node-tests/node-test-any.js +14 -14
  176. package/xpath/node-tests/node-test-comment.d.ts +6 -6
  177. package/xpath/node-tests/node-test-comment.js +14 -14
  178. package/xpath/node-tests/node-test-element-or-attribute.d.ts +6 -6
  179. package/xpath/node-tests/node-test-element-or-attribute.js +15 -15
  180. package/xpath/node-tests/node-test-name.d.ts +10 -10
  181. package/xpath/node-tests/node-test-name.js +38 -38
  182. package/xpath/node-tests/node-test-name.js.map +1 -1
  183. package/xpath/node-tests/node-test-nc.d.ts +9 -9
  184. package/xpath/node-tests/node-test-nc.js +16 -16
  185. package/xpath/node-tests/node-test-pi.d.ts +8 -8
  186. package/xpath/node-tests/node-test-pi.js +16 -16
  187. package/xpath/node-tests/node-test-text.d.ts +6 -6
  188. package/xpath/node-tests/node-test-text.js +14 -14
  189. package/xpath/node-tests/node-test.d.ts +5 -5
  190. package/xpath/node-tests/node-test.js +2 -2
  191. package/xpath/tokens.d.ts +62 -62
  192. package/xpath/tokens.js +300 -300
  193. package/xpath/values/boolean-value.d.ts +11 -11
  194. package/xpath/values/boolean-value.js +23 -23
  195. package/xpath/values/index.d.ts +5 -5
  196. package/xpath/values/index.js +46 -46
  197. package/xpath/values/node-set-value.d.ts +11 -11
  198. package/xpath/values/node-set-value.js +27 -27
  199. package/xpath/values/node-set-value.js.map +1 -1
  200. package/xpath/values/node-value.d.ts +7 -7
  201. package/xpath/values/node-value.js +2 -2
  202. package/xpath/values/number-value.d.ts +11 -11
  203. package/xpath/values/number-value.js +23 -23
  204. package/xpath/values/string-value.d.ts +11 -11
  205. package/xpath/values/string-value.js +23 -23
  206. package/xpath/xpath-grammar-rules.d.ts +68 -68
  207. package/xpath/xpath-grammar-rules.js +74 -74
  208. package/xpath/xpath-token-rule.d.ts +7 -7
  209. package/xpath/xpath-token-rule.js +2 -2
  210. package/xpath/xpath.d.ts +174 -174
  211. package/xpath/xpath.js +911 -911
  212. package/xpath/xpath.js.map +1 -1
  213. package/xpathdebug.d.ts +2 -2
  214. package/xpathdebug.js +187 -187
  215. package/xpathdebug.js.map +1 -1
  216. package/xslt/index.d.ts +3 -3
  217. package/xslt/index.js +19 -19
  218. package/xslt/xslt-decimal-format-settings.d.ts +28 -28
  219. package/xslt/xslt-decimal-format-settings.js +2 -2
  220. package/xslt/xslt-options.d.ts +7 -7
  221. package/xslt/xslt-options.js +2 -2
  222. package/xslt/xslt-parameter.d.ts +5 -5
  223. package/xslt/xslt-parameter.js +2 -2
  224. package/xslt/xslt.d.ts +260 -207
  225. package/xslt/xslt.js +1290 -1126
  226. package/xslt/xslt.js.map +1 -1
package/dom/xml-parser.js CHANGED
@@ -1,308 +1,308 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.XmlParser = void 0;
7
- var he_1 = __importDefault(require("he"));
8
- var functions_1 = require("./functions");
9
- var xdocument_1 = require("./xdocument");
10
- var xmltoken_1 = require("./xmltoken");
11
- var constants_1 = require("../constants");
12
- /**
13
- * Original author: Steffen Meschkat <mesch@google.com> (the `xmlParse` function,
14
- * now `xmlStrictParse`).
15
- *
16
- * An XML parse and a minimal DOM implementation that just supports
17
- * the subset of the W3C DOM that is used in the XSLT implementation.
18
- */
19
- var XmlParser = /** @class */ (function () {
20
- function XmlParser() {
21
- this.regexEmpty = /\/$/;
22
- this.XML10_TAGNAME_REGEXP = new RegExp("^(".concat(xmltoken_1.XML10_NAME, ")"));
23
- this.XML10_ATTRIBUTE_REGEXP = new RegExp(xmltoken_1.XML10_ATTRIBUTE, 'g');
24
- this.XML11_TAGNAME_REGEXP = new RegExp("^(".concat(xmltoken_1.XML11_NAME, ")"));
25
- this.XML11_ATTRIBUTE_REGEXP = new RegExp(xmltoken_1.XML11_ATTRIBUTE, 'g');
26
- this.lenientHtmlTags = ['hr', 'link', 'meta'];
27
- }
28
- /**
29
- * The entry point for this parser.
30
- * It verifies whether the document seems to be HTML.
31
- * HTML is a special case if XML and it should be parsed differently.
32
- * @param xmlOrHtml The XML or HTML content to be parsed.
33
- * @returns A DOM document.
34
- */
35
- XmlParser.prototype.xmlParse = function (xmlOrHtml) {
36
- if (xmlOrHtml.toUpperCase().startsWith('<!DOCTYPE HTML')) {
37
- return this.htmlParse(xmlOrHtml);
38
- }
39
- return this.xmlStrictParse(xmlOrHtml);
40
- };
41
- /**
42
- * Given an XNode, returns an object mapping prefixes to their corresponding namespaces in its scope.
43
- * Default namespace is treated as if its prefix were the empty string.
44
- * @param node The Node.
45
- * @returns An object with prefixes and namespace URLs.
46
- */
47
- XmlParser.prototype.namespaceMapAt = function (node) {
48
- var map = {
49
- // reserved namespaces: https://www.w3.org/TR/REC-xml-names/#xmlReserved
50
- xmlns: 'http://www.w3.org/2000/xmlns/',
51
- xml: 'http://www.w3.org/XML/1998/namespace'
52
- };
53
- var n = node;
54
- while (n !== null) {
55
- for (var i = 0; i < n.childNodes.length; i++) {
56
- var childNode = n.childNodes[i];
57
- if (childNode.nodeType !== constants_1.DOM_ATTRIBUTE_NODE) {
58
- continue;
59
- }
60
- if (childNode.nodeName.startsWith('xmlns:')) {
61
- var prefix = childNode.nodeName.split(':')[1];
62
- if (!(prefix in map))
63
- map[prefix] = childNode.nodeValue;
64
- }
65
- else if (childNode.nodeName == 'xmlns') {
66
- if (!('' in map))
67
- map[''] = childNode.nodeValue || null;
68
- }
69
- }
70
- n = n.parentNode;
71
- }
72
- return map;
73
- };
74
- /**
75
- * HTML needs to be parsed differently because it's a special case of XML.
76
- * Sources:
77
- *
78
- * - https://blog.teamtreehouse.com/to-close-or-not-to-close-tags-in-html5
79
- * @param htmlText The HTML text
80
- * @returns A DOM document.
81
- */
82
- XmlParser.prototype.htmlParse = function (htmlText) {
83
- var xmlDocument = new xdocument_1.XDocument();
84
- var root = xmlDocument;
85
- var stack = [];
86
- var parent = root;
87
- stack.push(parent);
88
- var tag = false, quotes = false, doublequotes = false, start = 0;
89
- for (var i = 0; i < htmlText.length; ++i) {
90
- var char = htmlText.charAt(i);
91
- if (tag) {
92
- if (!doublequotes && char === "'") {
93
- quotes = !quotes;
94
- }
95
- else if (!quotes && char === '"') {
96
- doublequotes = !doublequotes;
97
- }
98
- else if (!quotes && !doublequotes && char === '>') {
99
- var text = htmlText.slice(start, i);
100
- if (text.charAt(0) === '/') { // {
101
- stack.pop();
102
- parent = stack[stack.length - 1];
103
- }
104
- else if (text.charAt(0) === '!') {
105
- // Ignore comments
106
- // console.log(`Ignored ${text}`);
107
- }
108
- else {
109
- var empty = text.match(this.regexEmpty);
110
- var tagName = this.XML10_TAGNAME_REGEXP.exec(text)[1];
111
- var node = (0, functions_1.domCreateElement)(xmlDocument, tagName);
112
- var attribute = void 0;
113
- while ((attribute = this.XML10_ATTRIBUTE_REGEXP.exec(text))) {
114
- var val = he_1.default.decode(attribute[5] || attribute[7] || '');
115
- (0, functions_1.domSetAttribute)(node, attribute[1], val);
116
- }
117
- node.siblingPosition = parent.childNodes.length;
118
- (0, functions_1.domAppendChild)(parent, node);
119
- // The fundamental difference between this parse function
120
- // and the strict XML parse is here:
121
- // HTML is lenient with certain tags, that don't need to be closed.
122
- if (!empty && !this.lenientHtmlTags.includes(tagName)) {
123
- parent = node;
124
- stack.push(node);
125
- }
126
- }
127
- start = i + 1;
128
- tag = false;
129
- quotes = false;
130
- doublequotes = false;
131
- }
132
- }
133
- else {
134
- if (char === '<') {
135
- var text = htmlText.slice(start, i);
136
- if (text && parent !== root) {
137
- (0, functions_1.domAppendChild)(parent, (0, functions_1.domCreateTextNode)(xmlDocument, text));
138
- }
139
- if (htmlText.slice(i + 1, i + 4) === '!--') {
140
- var endTagIndex = htmlText.slice(i + 4).indexOf('-->');
141
- if (endTagIndex) {
142
- var node = (0, functions_1.domCreateComment)(xmlDocument, htmlText.slice(i + 4, i + endTagIndex + 4));
143
- (0, functions_1.domAppendChild)(parent, node);
144
- i += endTagIndex + 6;
145
- }
146
- }
147
- else if (htmlText.slice(i + 1, i + 9) === '!DOCTYPE') {
148
- var endTagIndex = htmlText.slice(i + 9).indexOf('>');
149
- if (endTagIndex) {
150
- var dtdValue = htmlText.slice(i + 9, i + endTagIndex + 9).trimStart();
151
- // TODO: Not sure if this is a good solution.
152
- // Trying to implement this: https://github.com/DesignLiquido/xslt-processor/issues/30
153
- var node = (0, functions_1.domCreateDTDSection)(xmlDocument, dtdValue);
154
- (0, functions_1.domAppendChild)(parent, node);
155
- i += endTagIndex + dtdValue.length + 5;
156
- }
157
- }
158
- else {
159
- tag = true;
160
- }
161
- start = i + 1;
162
- }
163
- }
164
- }
165
- return xmlDocument;
166
- };
167
- /**
168
- * Parses the given XML string with our custom, JavaScript XML parser.
169
- * @param xml The XML String.
170
- * @returns A XDocument.
171
- * @author Steffen Meschkat <mesch@google.com>
172
- */
173
- XmlParser.prototype.xmlStrictParse = function (xml) {
174
- var regexTagname;
175
- var regexAttribute;
176
- if (xml.match(/^<\?xml/)) {
177
- // When an XML document begins with an XML declaration
178
- // VersionInfo must appear.
179
- if (xml.search(new RegExp(xmltoken_1.XML10_VERSION_INFO)) === 5) {
180
- regexTagname = this.XML10_TAGNAME_REGEXP;
181
- regexAttribute = this.XML10_ATTRIBUTE_REGEXP;
182
- }
183
- else if (xml.search(new RegExp(xmltoken_1.XML11_VERSION_INFO)) === 5) {
184
- regexTagname = this.XML11_TAGNAME_REGEXP;
185
- regexAttribute = this.XML11_ATTRIBUTE_REGEXP;
186
- }
187
- else {
188
- throw new Error('XML VersionInfo has an unknown version number.');
189
- }
190
- }
191
- else {
192
- // When an XML declaration is missing it's an XML 1.0 document.
193
- regexTagname = this.XML10_TAGNAME_REGEXP;
194
- regexAttribute = this.XML10_ATTRIBUTE_REGEXP;
195
- }
196
- var xmlDocument = new xdocument_1.XDocument();
197
- var root = xmlDocument;
198
- var stack = [];
199
- var parent = root;
200
- stack.push(parent);
201
- var tag = false, quotes = false, doublequotes = false, start = 0;
202
- for (var i = 0; i < xml.length; ++i) {
203
- var char = xml.charAt(i);
204
- if (tag && !doublequotes && char === "'") {
205
- quotes = !quotes;
206
- }
207
- else if (tag && !quotes && char === '"') {
208
- doublequotes = !doublequotes;
209
- }
210
- else if (tag && char === '>' && !quotes && !doublequotes) {
211
- var text = xml.slice(start, i);
212
- if (text.charAt(0) === '/') {
213
- stack.pop();
214
- parent = stack[stack.length - 1];
215
- }
216
- else if (text.charAt(0) === '?') {
217
- // Ignore XML declaration and processing instructions
218
- }
219
- else if (text.charAt(0) === '!') {
220
- // Ignore comments
221
- // console.log(`Ignored ${text}`);
222
- }
223
- else {
224
- var empty = text.match(this.regexEmpty);
225
- var tagname = regexTagname.exec(text)[1];
226
- var node = (0, functions_1.domCreateElement)(xmlDocument, tagname);
227
- var attribute = void 0;
228
- while ((attribute = regexAttribute.exec(text))) {
229
- var val = he_1.default.decode(attribute[5] || attribute[7] || '');
230
- (0, functions_1.domSetAttribute)(node, attribute[1], val);
231
- }
232
- node.siblingPosition = parent.childNodes.length;
233
- (0, functions_1.domAppendChild)(parent, node);
234
- if (!empty) {
235
- parent = node;
236
- stack.push(node);
237
- }
238
- var namespaceMap = this.namespaceMapAt(node);
239
- if (node.prefix !== null) {
240
- if (node.prefix in namespaceMap)
241
- node.namespaceUri = namespaceMap[node.prefix];
242
- // else, prefix is undefined. do anything?
243
- }
244
- else {
245
- if ('' in namespaceMap)
246
- node.namespaceUri = namespaceMap[''];
247
- }
248
- for (var i_1 = 0; i_1 < node.childNodes.length; ++i_1) {
249
- var childNode = node.childNodes[i_1];
250
- if (childNode.nodeType !== constants_1.DOM_ATTRIBUTE_NODE) {
251
- continue;
252
- }
253
- if (childNode.prefix !== null && childNode.prefix in namespaceMap) {
254
- childNode.namespaceUri = namespaceMap[childNode.prefix];
255
- // else, prefix undefined.
256
- }
257
- // elements with no prefix always have no namespace, so do nothing here.
258
- }
259
- }
260
- start = i + 1;
261
- tag = false;
262
- quotes = false;
263
- doublequotes = false;
264
- }
265
- else if (!tag && char === '<') {
266
- var text = xml.slice(start, i);
267
- if (text && parent !== root) {
268
- (0, functions_1.domAppendChild)(parent, (0, functions_1.domCreateTextNode)(xmlDocument, text));
269
- }
270
- if (xml.slice(i + 1, i + 4) === '!--') {
271
- var endTagIndex = xml.slice(i + 4).indexOf('-->');
272
- if (endTagIndex) {
273
- var node = (0, functions_1.domCreateComment)(xmlDocument, xml.slice(i + 4, i + endTagIndex + 4));
274
- (0, functions_1.domAppendChild)(parent, node);
275
- i += endTagIndex + 6;
276
- }
277
- }
278
- else if (xml.slice(i + 1, i + 9) === '![CDATA[') {
279
- var endTagIndex = xml.slice(i + 9).indexOf(']]>');
280
- if (endTagIndex) {
281
- var node = (0, functions_1.domCreateCDATASection)(xmlDocument, xml.slice(i + 9, i + endTagIndex + 9));
282
- (0, functions_1.domAppendChild)(parent, node);
283
- i += endTagIndex + 11;
284
- }
285
- }
286
- else if (xml.slice(i + 1, i + 9) === '!DOCTYPE') { // "!DOCTYPE" can be used in a XSLT template.
287
- var endTagIndex = xml.slice(i + 9).indexOf('>');
288
- if (endTagIndex) {
289
- var dtdValue = xml.slice(i + 9, i + endTagIndex + 9).trimStart();
290
- // TODO: Not sure if this is a good solution.
291
- // Trying to implement this: https://github.com/DesignLiquido/xslt-processor/issues/30
292
- var node = (0, functions_1.domCreateDTDSection)(xmlDocument, dtdValue);
293
- (0, functions_1.domAppendChild)(parent, node);
294
- i += endTagIndex + dtdValue.length + 5;
295
- }
296
- }
297
- else {
298
- tag = true;
299
- }
300
- start = i + 1;
301
- }
302
- }
303
- return root;
304
- };
305
- return XmlParser;
306
- }());
307
- exports.XmlParser = XmlParser;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.XmlParser = void 0;
7
+ var he_1 = __importDefault(require("he"));
8
+ var functions_1 = require("./functions");
9
+ var xdocument_1 = require("./xdocument");
10
+ var xmltoken_1 = require("./xmltoken");
11
+ var constants_1 = require("../constants");
12
+ /**
13
+ * Original author: Steffen Meschkat <mesch@google.com> (the `xmlParse` function,
14
+ * now `xmlStrictParse`).
15
+ *
16
+ * An XML parse and a minimal DOM implementation that just supports
17
+ * the subset of the W3C DOM that is used in the XSLT implementation.
18
+ */
19
+ var XmlParser = /** @class */ (function () {
20
+ function XmlParser() {
21
+ this.regexEmpty = /\/$/;
22
+ this.XML10_TAGNAME_REGEXP = new RegExp("^(".concat(xmltoken_1.XML10_NAME, ")"));
23
+ this.XML10_ATTRIBUTE_REGEXP = new RegExp(xmltoken_1.XML10_ATTRIBUTE, 'g');
24
+ this.XML11_TAGNAME_REGEXP = new RegExp("^(".concat(xmltoken_1.XML11_NAME, ")"));
25
+ this.XML11_ATTRIBUTE_REGEXP = new RegExp(xmltoken_1.XML11_ATTRIBUTE, 'g');
26
+ this.lenientHtmlTags = ['hr', 'link', 'meta'];
27
+ }
28
+ /**
29
+ * The entry point for this parser.
30
+ * It verifies whether the document seems to be HTML.
31
+ * HTML is a special case if XML and it should be parsed differently.
32
+ * @param xmlOrHtml The XML or HTML content to be parsed.
33
+ * @returns A DOM document.
34
+ */
35
+ XmlParser.prototype.xmlParse = function (xmlOrHtml) {
36
+ if (xmlOrHtml.toUpperCase().startsWith('<!DOCTYPE HTML')) {
37
+ return this.htmlParse(xmlOrHtml);
38
+ }
39
+ return this.xmlStrictParse(xmlOrHtml);
40
+ };
41
+ /**
42
+ * Given an XNode, returns an object mapping prefixes to their corresponding namespaces in its scope.
43
+ * Default namespace is treated as if its prefix were the empty string.
44
+ * @param node The Node.
45
+ * @returns An object with prefixes and namespace URLs.
46
+ */
47
+ XmlParser.prototype.namespaceMapAt = function (node) {
48
+ var map = {
49
+ // reserved namespaces: https://www.w3.org/TR/REC-xml-names/#xmlReserved
50
+ xmlns: 'http://www.w3.org/2000/xmlns/',
51
+ xml: 'http://www.w3.org/XML/1998/namespace'
52
+ };
53
+ var n = node;
54
+ while (n !== null) {
55
+ for (var i = 0; i < n.childNodes.length; i++) {
56
+ var childNode = n.childNodes[i];
57
+ if (childNode.nodeType !== constants_1.DOM_ATTRIBUTE_NODE) {
58
+ continue;
59
+ }
60
+ if (childNode.nodeName.startsWith('xmlns:')) {
61
+ var prefix = childNode.nodeName.split(':')[1];
62
+ if (!(prefix in map))
63
+ map[prefix] = childNode.nodeValue;
64
+ }
65
+ else if (childNode.nodeName == 'xmlns') {
66
+ if (!('' in map))
67
+ map[''] = childNode.nodeValue || null;
68
+ }
69
+ }
70
+ n = n.parentNode;
71
+ }
72
+ return map;
73
+ };
74
+ /**
75
+ * HTML needs to be parsed differently because it's a special case of XML.
76
+ * Sources:
77
+ *
78
+ * - https://blog.teamtreehouse.com/to-close-or-not-to-close-tags-in-html5
79
+ * @param htmlText The HTML text
80
+ * @returns A DOM document.
81
+ */
82
+ XmlParser.prototype.htmlParse = function (htmlText) {
83
+ var xmlDocument = new xdocument_1.XDocument();
84
+ var root = xmlDocument;
85
+ var stack = [];
86
+ var parent = root;
87
+ stack.push(parent);
88
+ var tag = false, quotes = false, doublequotes = false, start = 0;
89
+ for (var i = 0; i < htmlText.length; ++i) {
90
+ var char = htmlText.charAt(i);
91
+ if (tag) {
92
+ if (!doublequotes && char === "'") {
93
+ quotes = !quotes;
94
+ }
95
+ else if (!quotes && char === '"') {
96
+ doublequotes = !doublequotes;
97
+ }
98
+ else if (!quotes && !doublequotes && char === '>') {
99
+ var text = htmlText.slice(start, i);
100
+ if (text.charAt(0) === '/') { // {
101
+ stack.pop();
102
+ parent = stack[stack.length - 1];
103
+ }
104
+ else if (text.charAt(0) === '!') {
105
+ // Ignore comments
106
+ // console.log(`Ignored ${text}`);
107
+ }
108
+ else {
109
+ var empty = text.match(this.regexEmpty);
110
+ var tagName = this.XML10_TAGNAME_REGEXP.exec(text)[1];
111
+ var node = (0, functions_1.domCreateElement)(xmlDocument, tagName);
112
+ var attribute = void 0;
113
+ while ((attribute = this.XML10_ATTRIBUTE_REGEXP.exec(text))) {
114
+ var val = he_1.default.decode(attribute[5] || attribute[7] || '');
115
+ (0, functions_1.domSetAttribute)(node, attribute[1], val);
116
+ }
117
+ node.siblingPosition = parent.childNodes.length;
118
+ (0, functions_1.domAppendChild)(parent, node);
119
+ // The fundamental difference between this parse function
120
+ // and the strict XML parse is here:
121
+ // HTML is lenient with certain tags, that don't need to be closed.
122
+ if (!empty && !this.lenientHtmlTags.includes(tagName)) {
123
+ parent = node;
124
+ stack.push(node);
125
+ }
126
+ }
127
+ start = i + 1;
128
+ tag = false;
129
+ quotes = false;
130
+ doublequotes = false;
131
+ }
132
+ }
133
+ else {
134
+ if (char === '<') {
135
+ var text = htmlText.slice(start, i);
136
+ if (text && parent !== root) {
137
+ (0, functions_1.domAppendChild)(parent, (0, functions_1.domCreateTextNode)(xmlDocument, text));
138
+ }
139
+ if (htmlText.slice(i + 1, i + 4) === '!--') {
140
+ var endTagIndex = htmlText.slice(i + 4).indexOf('-->');
141
+ if (endTagIndex) {
142
+ var node = (0, functions_1.domCreateComment)(xmlDocument, htmlText.slice(i + 4, i + endTagIndex + 4));
143
+ (0, functions_1.domAppendChild)(parent, node);
144
+ i += endTagIndex + 6;
145
+ }
146
+ }
147
+ else if (htmlText.slice(i + 1, i + 9) === '!DOCTYPE') {
148
+ var endTagIndex = htmlText.slice(i + 9).indexOf('>');
149
+ if (endTagIndex) {
150
+ var dtdValue = htmlText.slice(i + 9, i + endTagIndex + 9).trimStart();
151
+ // TODO: Not sure if this is a good solution.
152
+ // Trying to implement this: https://github.com/DesignLiquido/xslt-processor/issues/30
153
+ var node = (0, functions_1.domCreateDTDSection)(xmlDocument, dtdValue);
154
+ (0, functions_1.domAppendChild)(parent, node);
155
+ i += endTagIndex + dtdValue.length + 5;
156
+ }
157
+ }
158
+ else {
159
+ tag = true;
160
+ }
161
+ start = i + 1;
162
+ }
163
+ }
164
+ }
165
+ return xmlDocument;
166
+ };
167
+ /**
168
+ * Parses the given XML string with our custom, JavaScript XML parser.
169
+ * @param xml The XML String.
170
+ * @returns A XDocument.
171
+ * @author Steffen Meschkat <mesch@google.com>
172
+ */
173
+ XmlParser.prototype.xmlStrictParse = function (xml) {
174
+ var regexTagname;
175
+ var regexAttribute;
176
+ if (xml.match(/^<\?xml/)) {
177
+ // When an XML document begins with an XML declaration
178
+ // VersionInfo must appear.
179
+ if (xml.search(new RegExp(xmltoken_1.XML10_VERSION_INFO)) === 5) {
180
+ regexTagname = this.XML10_TAGNAME_REGEXP;
181
+ regexAttribute = this.XML10_ATTRIBUTE_REGEXP;
182
+ }
183
+ else if (xml.search(new RegExp(xmltoken_1.XML11_VERSION_INFO)) === 5) {
184
+ regexTagname = this.XML11_TAGNAME_REGEXP;
185
+ regexAttribute = this.XML11_ATTRIBUTE_REGEXP;
186
+ }
187
+ else {
188
+ throw new Error('XML VersionInfo has an unknown version number.');
189
+ }
190
+ }
191
+ else {
192
+ // When an XML declaration is missing it's an XML 1.0 document.
193
+ regexTagname = this.XML10_TAGNAME_REGEXP;
194
+ regexAttribute = this.XML10_ATTRIBUTE_REGEXP;
195
+ }
196
+ var xmlDocument = new xdocument_1.XDocument();
197
+ var root = xmlDocument;
198
+ var stack = [];
199
+ var parent = root;
200
+ stack.push(parent);
201
+ var tag = false, quotes = false, doublequotes = false, start = 0;
202
+ for (var i = 0; i < xml.length; ++i) {
203
+ var char = xml.charAt(i);
204
+ if (tag && !doublequotes && char === "'") {
205
+ quotes = !quotes;
206
+ }
207
+ else if (tag && !quotes && char === '"') {
208
+ doublequotes = !doublequotes;
209
+ }
210
+ else if (tag && char === '>' && !quotes && !doublequotes) {
211
+ var text = xml.slice(start, i);
212
+ if (text.charAt(0) === '/') {
213
+ stack.pop();
214
+ parent = stack[stack.length - 1];
215
+ }
216
+ else if (text.charAt(0) === '?') {
217
+ // Ignore XML declaration and processing instructions
218
+ }
219
+ else if (text.charAt(0) === '!') {
220
+ // Ignore comments
221
+ // console.log(`Ignored ${text}`);
222
+ }
223
+ else {
224
+ var empty = text.match(this.regexEmpty);
225
+ var tagname = regexTagname.exec(text)[1];
226
+ var node = (0, functions_1.domCreateElement)(xmlDocument, tagname);
227
+ var attribute = void 0;
228
+ while ((attribute = regexAttribute.exec(text))) {
229
+ var val = he_1.default.decode(attribute[5] || attribute[7] || '');
230
+ (0, functions_1.domSetAttribute)(node, attribute[1], val);
231
+ }
232
+ node.siblingPosition = parent.childNodes.length;
233
+ (0, functions_1.domAppendChild)(parent, node);
234
+ if (!empty) {
235
+ parent = node;
236
+ stack.push(node);
237
+ }
238
+ var namespaceMap = this.namespaceMapAt(node);
239
+ if (node.prefix !== null) {
240
+ if (node.prefix in namespaceMap)
241
+ node.namespaceUri = namespaceMap[node.prefix];
242
+ // else, prefix is undefined. do anything?
243
+ }
244
+ else {
245
+ if ('' in namespaceMap)
246
+ node.namespaceUri = namespaceMap[''];
247
+ }
248
+ for (var i_1 = 0; i_1 < node.childNodes.length; ++i_1) {
249
+ var childNode = node.childNodes[i_1];
250
+ if (childNode.nodeType !== constants_1.DOM_ATTRIBUTE_NODE) {
251
+ continue;
252
+ }
253
+ if (childNode.prefix !== null && childNode.prefix in namespaceMap) {
254
+ childNode.namespaceUri = namespaceMap[childNode.prefix];
255
+ // else, prefix undefined.
256
+ }
257
+ // elements with no prefix always have no namespace, so do nothing here.
258
+ }
259
+ }
260
+ start = i + 1;
261
+ tag = false;
262
+ quotes = false;
263
+ doublequotes = false;
264
+ }
265
+ else if (!tag && char === '<') {
266
+ var text = xml.slice(start, i);
267
+ if (text && parent !== root) {
268
+ (0, functions_1.domAppendChild)(parent, (0, functions_1.domCreateTextNode)(xmlDocument, text));
269
+ }
270
+ if (xml.slice(i + 1, i + 4) === '!--') {
271
+ var endTagIndex = xml.slice(i + 4).indexOf('-->');
272
+ if (endTagIndex) {
273
+ var node = (0, functions_1.domCreateComment)(xmlDocument, xml.slice(i + 4, i + endTagIndex + 4));
274
+ (0, functions_1.domAppendChild)(parent, node);
275
+ i += endTagIndex + 6;
276
+ }
277
+ }
278
+ else if (xml.slice(i + 1, i + 9) === '![CDATA[') {
279
+ var endTagIndex = xml.slice(i + 9).indexOf(']]>');
280
+ if (endTagIndex) {
281
+ var node = (0, functions_1.domCreateCDATASection)(xmlDocument, xml.slice(i + 9, i + endTagIndex + 9));
282
+ (0, functions_1.domAppendChild)(parent, node);
283
+ i += endTagIndex + 11;
284
+ }
285
+ }
286
+ else if (xml.slice(i + 1, i + 9) === '!DOCTYPE') { // "!DOCTYPE" can be used in a XSLT template.
287
+ var endTagIndex = xml.slice(i + 9).indexOf('>');
288
+ if (endTagIndex) {
289
+ var dtdValue = xml.slice(i + 9, i + endTagIndex + 9).trimStart();
290
+ // TODO: Not sure if this is a good solution.
291
+ // Trying to implement this: https://github.com/DesignLiquido/xslt-processor/issues/30
292
+ var node = (0, functions_1.domCreateDTDSection)(xmlDocument, dtdValue);
293
+ (0, functions_1.domAppendChild)(parent, node);
294
+ i += endTagIndex + dtdValue.length + 5;
295
+ }
296
+ }
297
+ else {
298
+ tag = true;
299
+ }
300
+ start = i + 1;
301
+ }
302
+ }
303
+ return root;
304
+ };
305
+ return XmlParser;
306
+ }());
307
+ exports.XmlParser = XmlParser;
308
308
  //# sourceMappingURL=xml-parser.js.map