@xmldom/xmldom 0.9.0-beta.9 → 0.9.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.
- package/CHANGELOG.md +78 -0
- package/index.d.ts +84 -76
- package/lib/.eslintrc.yml +1 -1
- package/lib/conventions.js +108 -77
- package/lib/dom-parser.js +98 -87
- package/lib/dom.js +1059 -391
- package/lib/entities.js +14 -9
- package/lib/errors.js +206 -0
- package/lib/grammar.js +21 -3
- package/lib/index.js +21 -3
- package/lib/sax.js +157 -115
- package/package.json +73 -70
- package/readme.md +11 -3
package/lib/conventions.js
CHANGED
|
@@ -3,15 +3,15 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* Ponyfill for `Array.prototype.find` which is only available in ES6 runtimes.
|
|
5
5
|
*
|
|
6
|
-
* Works with anything that has a `length` property and index access properties,
|
|
6
|
+
* Works with anything that has a `length` property and index access properties,
|
|
7
|
+
* including NodeList.
|
|
7
8
|
*
|
|
8
|
-
* @
|
|
9
|
-
* @param {
|
|
10
|
-
* @param {
|
|
11
|
-
*
|
|
12
|
-
* allows injecting a custom implementation in tests
|
|
9
|
+
* @param {T[] | { length: number; [number]: T }} list
|
|
10
|
+
* @param {function (item: T, index: number, list:T[]):boolean} predicate
|
|
11
|
+
* @param {Partial<Pick<ArrayConstructor['prototype'], 'find'>>?} ac
|
|
12
|
+
* Allows injecting a custom implementation in tests (`Array.prototype` by default).
|
|
13
13
|
* @returns {T | undefined}
|
|
14
|
-
*
|
|
14
|
+
* @template {unknown} T
|
|
15
15
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
|
|
16
16
|
* @see https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.find
|
|
17
17
|
*/
|
|
@@ -23,7 +23,7 @@ function find(list, predicate, ac) {
|
|
|
23
23
|
return ac.find.call(list, predicate);
|
|
24
24
|
}
|
|
25
25
|
for (var i = 0; i < list.length; i++) {
|
|
26
|
-
if (
|
|
26
|
+
if (hasOwn(list, i)) {
|
|
27
27
|
var item = list[i];
|
|
28
28
|
if (predicate.call(undefined, item, i, list)) {
|
|
29
29
|
return item;
|
|
@@ -39,31 +39,51 @@ function find(list, predicate, ac) {
|
|
|
39
39
|
*
|
|
40
40
|
* Is used to create "enum like" objects.
|
|
41
41
|
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* allows to inject custom object constructor for tests
|
|
46
|
-
* @returns {Readonly<T>}
|
|
42
|
+
* If `Object.getOwnPropertyDescriptors` is available,
|
|
43
|
+
* a new object with all properties of object but without any prototype is created and returned
|
|
44
|
+
* after freezing it.
|
|
47
45
|
*
|
|
46
|
+
* @param {T} object
|
|
47
|
+
* The object to freeze.
|
|
48
|
+
* @param {Pick<ObjectConstructor, 'create' | 'freeze' | 'getOwnPropertyDescriptors'>} [oc=Object]
|
|
49
|
+
* `Object` by default,
|
|
50
|
+
* allows to inject custom object constructor for tests.
|
|
51
|
+
* @returns {Readonly<T>}
|
|
52
|
+
* @template {Object} T
|
|
48
53
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
|
|
54
|
+
* @prettierignore
|
|
49
55
|
*/
|
|
50
56
|
function freeze(object, oc) {
|
|
51
57
|
if (oc === undefined) {
|
|
52
58
|
oc = Object;
|
|
53
59
|
}
|
|
60
|
+
if (oc && typeof oc.getOwnPropertyDescriptors === 'function') {
|
|
61
|
+
object = oc.create(null, oc.getOwnPropertyDescriptors(object));
|
|
62
|
+
}
|
|
54
63
|
return oc && typeof oc.freeze === 'function' ? oc.freeze(object) : object;
|
|
55
64
|
}
|
|
56
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Implementation for `Object.hasOwn` but ES5 compatible.
|
|
68
|
+
*
|
|
69
|
+
* @param {any} object
|
|
70
|
+
* @param {string | number} key
|
|
71
|
+
* @returns {boolean}
|
|
72
|
+
*/
|
|
73
|
+
function hasOwn(object, key) {
|
|
74
|
+
return Object.prototype.hasOwnProperty.call(object, key);
|
|
75
|
+
}
|
|
76
|
+
|
|
57
77
|
/**
|
|
58
78
|
* Since xmldom can not rely on `Object.assign`,
|
|
59
79
|
* it uses/provides a simplified version that is sufficient for its needs.
|
|
60
80
|
*
|
|
61
81
|
* @param {Object} target
|
|
62
82
|
* @param {Object | null | undefined} source
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
* @throws TypeError
|
|
66
|
-
*
|
|
83
|
+
* @returns {Object}
|
|
84
|
+
* The target with the merged/overridden properties.
|
|
85
|
+
* @throws {TypeError}
|
|
86
|
+
* If target is not an object.
|
|
67
87
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
|
|
68
88
|
* @see https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.assign
|
|
69
89
|
*/
|
|
@@ -72,7 +92,7 @@ function assign(target, source) {
|
|
|
72
92
|
throw new TypeError('target is not an object');
|
|
73
93
|
}
|
|
74
94
|
for (var key in source) {
|
|
75
|
-
if (
|
|
95
|
+
if (hasOwn(source, key)) {
|
|
76
96
|
target[key] = source[key];
|
|
77
97
|
}
|
|
78
98
|
}
|
|
@@ -84,8 +104,8 @@ function assign(target, source) {
|
|
|
84
104
|
* The presence of a boolean attribute on an element represents the `true` value,
|
|
85
105
|
* and the absence of the attribute represents the `false` value.
|
|
86
106
|
*
|
|
87
|
-
* If the attribute is present, its value must either be the empty string
|
|
88
|
-
*
|
|
107
|
+
* If the attribute is present, its value must either be the empty string, or a value that is
|
|
108
|
+
* an ASCII case-insensitive match for the attribute's canonical name,
|
|
89
109
|
* with no leading or trailing whitespace.
|
|
90
110
|
*
|
|
91
111
|
* Note: The values `"true"` and `"false"` are not allowed on boolean attributes.
|
|
@@ -123,28 +143,46 @@ var HTML_BOOLEAN_ATTRIBUTES = freeze({
|
|
|
123
143
|
|
|
124
144
|
/**
|
|
125
145
|
* Check if `name` is matching one of the HTML boolean attribute names.
|
|
126
|
-
* This method doesn't check if such attributes are allowed in the context of the current
|
|
146
|
+
* This method doesn't check if such attributes are allowed in the context of the current
|
|
147
|
+
* document/parsing.
|
|
127
148
|
*
|
|
128
149
|
* @param {string} name
|
|
129
|
-
* @
|
|
130
|
-
* @see HTML_BOOLEAN_ATTRIBUTES
|
|
150
|
+
* @returns {boolean}
|
|
151
|
+
* @see {@link HTML_BOOLEAN_ATTRIBUTES}
|
|
131
152
|
* @see https://html.spec.whatwg.org/#boolean-attributes
|
|
132
153
|
* @see https://html.spec.whatwg.org/#attributes-3
|
|
133
154
|
*/
|
|
134
155
|
function isHTMLBooleanAttribute(name) {
|
|
135
|
-
return HTML_BOOLEAN_ATTRIBUTES
|
|
156
|
+
return hasOwn(HTML_BOOLEAN_ATTRIBUTES, name.toLowerCase());
|
|
136
157
|
}
|
|
137
158
|
|
|
138
159
|
/**
|
|
139
160
|
* Void elements only have a start tag; end tags must not be specified for void elements.
|
|
140
|
-
* These elements should be written as self
|
|
161
|
+
* These elements should be written as self-closing like this: `<area />`.
|
|
141
162
|
* This should not be confused with optional tags that HTML allows to omit the end tag for
|
|
142
163
|
* (like `li`, `tr` and others), which can have content after them,
|
|
143
|
-
* so they can not be written as self
|
|
144
|
-
* xmldom does not have any logic for optional end tags cases
|
|
145
|
-
*
|
|
164
|
+
* so they can not be written as self-closing.
|
|
165
|
+
* xmldom does not have any logic for optional end tags cases,
|
|
166
|
+
* and will report them as a warning.
|
|
167
|
+
* Content that would go into the unopened element,
|
|
168
|
+
* will instead be added as a sibling text node.
|
|
146
169
|
*
|
|
147
|
-
* @type {Readonly<{
|
|
170
|
+
* @type {Readonly<{
|
|
171
|
+
* area: boolean;
|
|
172
|
+
* col: boolean;
|
|
173
|
+
* img: boolean;
|
|
174
|
+
* wbr: boolean;
|
|
175
|
+
* link: boolean;
|
|
176
|
+
* hr: boolean;
|
|
177
|
+
* source: boolean;
|
|
178
|
+
* br: boolean;
|
|
179
|
+
* input: boolean;
|
|
180
|
+
* param: boolean;
|
|
181
|
+
* meta: boolean;
|
|
182
|
+
* embed: boolean;
|
|
183
|
+
* track: boolean;
|
|
184
|
+
* base: boolean;
|
|
185
|
+
* }>}
|
|
148
186
|
* @see https://html.spec.whatwg.org/#void-elements
|
|
149
187
|
* @see https://html.spec.whatwg.org/#optional-tags
|
|
150
188
|
*/
|
|
@@ -167,24 +205,24 @@ var HTML_VOID_ELEMENTS = freeze({
|
|
|
167
205
|
|
|
168
206
|
/**
|
|
169
207
|
* Check if `tagName` is matching one of the HTML void element names.
|
|
170
|
-
* This method doesn't check if such tags are allowed
|
|
171
|
-
*
|
|
208
|
+
* This method doesn't check if such tags are allowed in the context of the current
|
|
209
|
+
* document/parsing.
|
|
172
210
|
*
|
|
173
211
|
* @param {string} tagName
|
|
174
|
-
* @
|
|
175
|
-
* @see HTML_VOID_ELEMENTS
|
|
212
|
+
* @returns {boolean}
|
|
213
|
+
* @see {@link HTML_VOID_ELEMENTS}
|
|
176
214
|
* @see https://html.spec.whatwg.org/#void-elements
|
|
177
215
|
*/
|
|
178
216
|
function isHTMLVoidElement(tagName) {
|
|
179
|
-
return HTML_VOID_ELEMENTS
|
|
217
|
+
return hasOwn(HTML_VOID_ELEMENTS, tagName.toLowerCase());
|
|
180
218
|
}
|
|
181
219
|
|
|
182
220
|
/**
|
|
183
221
|
* Tag names that are raw text elements according to HTML spec.
|
|
184
222
|
* The value denotes whether they are escapable or not.
|
|
185
223
|
*
|
|
186
|
-
* @see isHTMLEscapableRawTextElement
|
|
187
|
-
* @see isHTMLRawTextElement
|
|
224
|
+
* @see {@link isHTMLEscapableRawTextElement}
|
|
225
|
+
* @see {@link isHTMLRawTextElement}
|
|
188
226
|
* @see https://html.spec.whatwg.org/#raw-text-elements
|
|
189
227
|
* @see https://html.spec.whatwg.org/#escapable-raw-text-elements
|
|
190
228
|
*/
|
|
@@ -197,42 +235,41 @@ var HTML_RAW_TEXT_ELEMENTS = freeze({
|
|
|
197
235
|
|
|
198
236
|
/**
|
|
199
237
|
* Check if `tagName` is matching one of the HTML raw text element names.
|
|
200
|
-
* This method doesn't check if such tags are allowed
|
|
201
|
-
*
|
|
238
|
+
* This method doesn't check if such tags are allowed in the context of the current
|
|
239
|
+
* document/parsing.
|
|
202
240
|
*
|
|
203
241
|
* @param {string} tagName
|
|
204
|
-
* @
|
|
205
|
-
* @see isHTMLEscapableRawTextElement
|
|
206
|
-
* @see HTML_RAW_TEXT_ELEMENTS
|
|
242
|
+
* @returns {boolean}
|
|
243
|
+
* @see {@link isHTMLEscapableRawTextElement}
|
|
244
|
+
* @see {@link HTML_RAW_TEXT_ELEMENTS}
|
|
207
245
|
* @see https://html.spec.whatwg.org/#raw-text-elements
|
|
208
246
|
* @see https://html.spec.whatwg.org/#escapable-raw-text-elements
|
|
209
247
|
*/
|
|
210
248
|
function isHTMLRawTextElement(tagName) {
|
|
211
249
|
var key = tagName.toLowerCase();
|
|
212
|
-
return HTML_RAW_TEXT_ELEMENTS
|
|
250
|
+
return hasOwn(HTML_RAW_TEXT_ELEMENTS, key) && !HTML_RAW_TEXT_ELEMENTS[key];
|
|
213
251
|
}
|
|
214
252
|
/**
|
|
215
253
|
* Check if `tagName` is matching one of the HTML escapable raw text element names.
|
|
216
|
-
* This method doesn't check if such tags are allowed
|
|
217
|
-
*
|
|
254
|
+
* This method doesn't check if such tags are allowed in the context of the current
|
|
255
|
+
* document/parsing.
|
|
218
256
|
*
|
|
219
257
|
* @param {string} tagName
|
|
220
|
-
* @
|
|
221
|
-
* @see isHTMLRawTextElement
|
|
222
|
-
* @see HTML_RAW_TEXT_ELEMENTS
|
|
258
|
+
* @returns {boolean}
|
|
259
|
+
* @see {@link isHTMLRawTextElement}
|
|
260
|
+
* @see {@link HTML_RAW_TEXT_ELEMENTS}
|
|
223
261
|
* @see https://html.spec.whatwg.org/#raw-text-elements
|
|
224
262
|
* @see https://html.spec.whatwg.org/#escapable-raw-text-elements
|
|
225
263
|
*/
|
|
226
264
|
function isHTMLEscapableRawTextElement(tagName) {
|
|
227
265
|
var key = tagName.toLowerCase();
|
|
228
|
-
return HTML_RAW_TEXT_ELEMENTS
|
|
266
|
+
return hasOwn(HTML_RAW_TEXT_ELEMENTS, key) && HTML_RAW_TEXT_ELEMENTS[key];
|
|
229
267
|
}
|
|
230
268
|
/**
|
|
231
269
|
* Only returns true if `value` matches MIME_TYPE.HTML, which indicates an HTML document.
|
|
232
270
|
*
|
|
233
271
|
* @param {string} mimeType
|
|
234
272
|
* @returns {mimeType is 'text/html'}
|
|
235
|
-
*
|
|
236
273
|
* @see https://www.iana.org/assignments/media-types/text/html
|
|
237
274
|
* @see https://en.wikipedia.org/wiki/HTML
|
|
238
275
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString
|
|
@@ -242,12 +279,11 @@ function isHTMLMimeType(mimeType) {
|
|
|
242
279
|
return mimeType === MIME_TYPE.HTML;
|
|
243
280
|
}
|
|
244
281
|
/**
|
|
245
|
-
* For both the `text/html` and the `application/xhtml+xml` namespace
|
|
246
|
-
*
|
|
282
|
+
* For both the `text/html` and the `application/xhtml+xml` namespace the spec defines that the
|
|
283
|
+
* HTML namespace is provided as the default.
|
|
247
284
|
*
|
|
248
285
|
* @param {string} mimeType
|
|
249
286
|
* @returns {boolean}
|
|
250
|
-
*
|
|
251
287
|
* @see https://dom.spec.whatwg.org/#dom-document-createelement
|
|
252
288
|
* @see https://dom.spec.whatwg.org/#dom-domimplementation-createdocument
|
|
253
289
|
* @see https://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument
|
|
@@ -259,9 +295,11 @@ function hasDefaultHTMLNamespace(mimeType) {
|
|
|
259
295
|
/**
|
|
260
296
|
* All mime types that are allowed as input to `DOMParser.parseFromString`
|
|
261
297
|
*
|
|
262
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString#Argument02
|
|
263
|
-
*
|
|
264
|
-
* @see
|
|
298
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString#Argument02
|
|
299
|
+
* MDN
|
|
300
|
+
* @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#domparsersupportedtype
|
|
301
|
+
* WHATWG HTML Spec
|
|
302
|
+
* @see {@link DOMParser.prototype.parseFromString}
|
|
265
303
|
*/
|
|
266
304
|
var MIME_TYPE = freeze({
|
|
267
305
|
/**
|
|
@@ -270,14 +308,16 @@ var MIME_TYPE = freeze({
|
|
|
270
308
|
* @see https://www.iana.org/assignments/media-types/text/html IANA MimeType registration
|
|
271
309
|
* @see https://en.wikipedia.org/wiki/HTML Wikipedia
|
|
272
310
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString MDN
|
|
273
|
-
* @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring
|
|
311
|
+
* @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring
|
|
312
|
+
* WHATWG HTML Spec
|
|
274
313
|
*/
|
|
275
314
|
HTML: 'text/html',
|
|
276
315
|
|
|
277
316
|
/**
|
|
278
317
|
* `application/xml`, the standard mime type for XML documents.
|
|
279
318
|
*
|
|
280
|
-
* @see https://www.iana.org/assignments/media-types/application/xml IANA MimeType
|
|
319
|
+
* @see https://www.iana.org/assignments/media-types/application/xml IANA MimeType
|
|
320
|
+
* registration
|
|
281
321
|
* @see https://tools.ietf.org/html/rfc7303#section-9.1 RFC 7303
|
|
282
322
|
* @see https://en.wikipedia.org/wiki/XML_and_MIME Wikipedia
|
|
283
323
|
*/
|
|
@@ -296,7 +336,8 @@ var MIME_TYPE = freeze({
|
|
|
296
336
|
* `application/xhtml+xml`, indicates an XML document that has the default HTML namespace,
|
|
297
337
|
* but is parsed as an XML document.
|
|
298
338
|
*
|
|
299
|
-
* @see https://www.iana.org/assignments/media-types/application/xhtml+xml IANA MimeType
|
|
339
|
+
* @see https://www.iana.org/assignments/media-types/application/xhtml+xml IANA MimeType
|
|
340
|
+
* registration
|
|
300
341
|
* @see https://dom.spec.whatwg.org/#dom-domimplementation-createdocument WHATWG DOM Spec
|
|
301
342
|
* @see https://en.wikipedia.org/wiki/XHTML Wikipedia
|
|
302
343
|
*/
|
|
@@ -312,20 +353,25 @@ var MIME_TYPE = freeze({
|
|
|
312
353
|
XML_SVG_IMAGE: 'image/svg+xml',
|
|
313
354
|
});
|
|
314
355
|
/**
|
|
315
|
-
* @typedef {'application/xhtml+xml' | 'application/xml' | 'image/svg+xml' |
|
|
356
|
+
* @typedef {'application/xhtml+xml' | 'application/xml' | 'image/svg+xml' | 'text/html' | 'text/xml'}
|
|
357
|
+
* MimeType
|
|
316
358
|
*/
|
|
317
359
|
/**
|
|
318
360
|
* @type {MimeType[]}
|
|
319
361
|
* @private
|
|
362
|
+
* Basically `Object.values`, which is not available in ES5.
|
|
320
363
|
*/
|
|
321
364
|
var _MIME_TYPES = Object.keys(MIME_TYPE).map(function (key) {
|
|
322
365
|
return MIME_TYPE[key];
|
|
323
366
|
});
|
|
324
367
|
|
|
325
368
|
/**
|
|
326
|
-
* Only returns true if `mimeType` is one of the allowed values for
|
|
369
|
+
* Only returns true if `mimeType` is one of the allowed values for
|
|
370
|
+
* `DOMParser.parseFromString`.
|
|
371
|
+
*
|
|
327
372
|
* @param {string} mimeType
|
|
328
373
|
* @returns {mimeType is 'application/xhtml+xml' | 'application/xml' | 'image/svg+xml' | 'text/html' | 'text/xml'}
|
|
374
|
+
*
|
|
329
375
|
*/
|
|
330
376
|
function isValidMimeType(mimeType) {
|
|
331
377
|
return _MIME_TYPES.indexOf(mimeType) > -1;
|
|
@@ -358,28 +404,13 @@ var NAMESPACE = freeze({
|
|
|
358
404
|
XML: 'http://www.w3.org/XML/1998/namespace',
|
|
359
405
|
|
|
360
406
|
/**
|
|
361
|
-
* The `xmlns:` namespace
|
|
407
|
+
* The `xmlns:` namespace.
|
|
362
408
|
*
|
|
363
409
|
* @see https://www.w3.org/2000/xmlns/
|
|
364
410
|
*/
|
|
365
411
|
XMLNS: 'http://www.w3.org/2000/xmlns/',
|
|
366
412
|
});
|
|
367
413
|
|
|
368
|
-
/**
|
|
369
|
-
* Creates an error that will not be caught by XMLReader aka the SAX parser.
|
|
370
|
-
*
|
|
371
|
-
* @param {string} message
|
|
372
|
-
* @param {any?} locator Optional, can provide details about the location in the source
|
|
373
|
-
* @constructor
|
|
374
|
-
*/
|
|
375
|
-
function ParseError(message, locator) {
|
|
376
|
-
this.message = message;
|
|
377
|
-
this.locator = locator;
|
|
378
|
-
if (Error.captureStackTrace) Error.captureStackTrace(this, ParseError);
|
|
379
|
-
}
|
|
380
|
-
ParseError.prototype = new Error();
|
|
381
|
-
ParseError.prototype.name = ParseError.name;
|
|
382
|
-
|
|
383
414
|
exports.assign = assign;
|
|
384
415
|
exports.find = find;
|
|
385
416
|
exports.freeze = freeze;
|
|
@@ -387,6 +418,7 @@ exports.HTML_BOOLEAN_ATTRIBUTES = HTML_BOOLEAN_ATTRIBUTES;
|
|
|
387
418
|
exports.HTML_RAW_TEXT_ELEMENTS = HTML_RAW_TEXT_ELEMENTS;
|
|
388
419
|
exports.HTML_VOID_ELEMENTS = HTML_VOID_ELEMENTS;
|
|
389
420
|
exports.hasDefaultHTMLNamespace = hasDefaultHTMLNamespace;
|
|
421
|
+
exports.hasOwn = hasOwn;
|
|
390
422
|
exports.isHTMLBooleanAttribute = isHTMLBooleanAttribute;
|
|
391
423
|
exports.isHTMLRawTextElement = isHTMLRawTextElement;
|
|
392
424
|
exports.isHTMLEscapableRawTextElement = isHTMLEscapableRawTextElement;
|
|
@@ -395,4 +427,3 @@ exports.isHTMLVoidElement = isHTMLVoidElement;
|
|
|
395
427
|
exports.isValidMimeType = isValidMimeType;
|
|
396
428
|
exports.MIME_TYPE = MIME_TYPE;
|
|
397
429
|
exports.NAMESPACE = NAMESPACE;
|
|
398
|
-
exports.ParseError = ParseError;
|