@xmldom/xmldom 0.9.0-beta.8 → 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.
@@ -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, including NodeList.
6
+ * Works with anything that has a `length` property and index access properties,
7
+ * including NodeList.
7
8
  *
8
- * @template {unknown} T
9
- * @param {Array<T> | ({length:number, [number]: T})} list
10
- * @param {function (item: T, index: number, list:Array<T> | ({length:number, [number]: T})):boolean} predicate
11
- * @param {Partial<Pick<ArrayConstructor['prototype'], 'find'>>?} ac `Array.prototype` by default,
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 (Object.prototype.hasOwnProperty.call(list, i)) {
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
- * @template T
43
- * @param {T} object the object to freeze
44
- * @param {Pick<ObjectConstructor, 'freeze'>} [oc=Object] `Object` by default,
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
 
57
66
  /**
58
- * Since we can not rely on `Object.assign` we provide a simplified version
59
- * that is sufficient for our needs.
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
+
77
+ /**
78
+ * Since xmldom can not rely on `Object.assign`,
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
- * @returns {Object} target
65
- * @throws TypeError if target is not an object
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 (Object.prototype.hasOwnProperty.call(source, key)) {
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
- * or a value that is an ASCII case-insensitive match for the attribute's canonical name,
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 document/parsing.
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
- * @return {boolean}
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.hasOwnProperty(name.toLowerCase());
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 closing like this: `<area />`.
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 closing.
144
- * xmldom does not have any logic for optional end tags cases and will report them as a warning.
145
- * Content that would go into the unopened element will instead be added as a sibling text node.
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<{area: boolean, col: boolean, img: boolean, wbr: boolean, link: boolean, hr: boolean, source: boolean, br: boolean, input: boolean, param: boolean, meta: boolean, embed: boolean, track: boolean, base: boolean}>}
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
- * in the context of the current document/parsing.
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
- * @return {boolean}
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.hasOwnProperty(tagName.toLowerCase());
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,90 +235,89 @@ 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
- * in the context of the current document/parsing.
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
- * @return {boolean}
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.hasOwnProperty(key) && !HTML_RAW_TEXT_ELEMENTS[key];
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
- * in the context of the current document/parsing.
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
- * @return {boolean}
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.hasOwnProperty(key) && HTML_RAW_TEXT_ELEMENTS[key];
266
+ return hasOwn(HTML_RAW_TEXT_ELEMENTS, key) && HTML_RAW_TEXT_ELEMENTS[key];
267
+ }
268
+ /**
269
+ * Only returns true if `value` matches MIME_TYPE.HTML, which indicates an HTML document.
270
+ *
271
+ * @param {string} mimeType
272
+ * @returns {mimeType is 'text/html'}
273
+ * @see https://www.iana.org/assignments/media-types/text/html
274
+ * @see https://en.wikipedia.org/wiki/HTML
275
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString
276
+ * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring
277
+ */
278
+ function isHTMLMimeType(mimeType) {
279
+ return mimeType === MIME_TYPE.HTML;
280
+ }
281
+ /**
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.
284
+ *
285
+ * @param {string} mimeType
286
+ * @returns {boolean}
287
+ * @see https://dom.spec.whatwg.org/#dom-document-createelement
288
+ * @see https://dom.spec.whatwg.org/#dom-domimplementation-createdocument
289
+ * @see https://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument
290
+ */
291
+ function hasDefaultHTMLNamespace(mimeType) {
292
+ return isHTMLMimeType(mimeType) || mimeType === MIME_TYPE.XML_XHTML_APPLICATION;
229
293
  }
230
294
 
231
295
  /**
232
296
  * All mime types that are allowed as input to `DOMParser.parseFromString`
233
297
  *
234
- * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString#Argument02 MDN
235
- * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#domparsersupportedtype WHATWG HTML Spec
236
- * @see DOMParser.prototype.parseFromString
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}
237
303
  */
238
304
  var MIME_TYPE = freeze({
239
305
  /**
240
306
  * `text/html`, the only mime type that triggers treating an XML document as HTML.
241
307
  *
242
- * @see DOMParser.SupportedType.isHTML
243
308
  * @see https://www.iana.org/assignments/media-types/text/html IANA MimeType registration
244
309
  * @see https://en.wikipedia.org/wiki/HTML Wikipedia
245
310
  * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString MDN
246
- * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring WHATWG HTML Spec
311
+ * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring
312
+ * WHATWG HTML Spec
247
313
  */
248
314
  HTML: 'text/html',
249
315
 
250
- /**
251
- * Helper method to check a mime type if it indicates an HTML document
252
- *
253
- * @param {string} [value]
254
- * @returns {boolean}
255
- *
256
- * @see [IANA MimeType registration](https://www.iana.org/assignments/media-types/text/html)
257
- * @see [Wikipedia](https://en.wikipedia.org/wiki/HTML)
258
- * @see [`DOMParser.parseFromString` @ MDN](https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString)
259
- * @see [`DOMParser.parseFromString` @ HTML Specification](https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring)
260
- */
261
- isHTML: function (value) {
262
- return value === MIME_TYPE.HTML;
263
- },
264
-
265
- /**
266
- * For both the `text/html` and the `application/xhtml+xml` namespace
267
- * the spec defines that the HTML namespace is provided as the default in some cases.
268
- *
269
- * @param {string} mimeType
270
- * @returns {boolean}
271
- *
272
- * @see https://dom.spec.whatwg.org/#dom-document-createelement
273
- * @see https://dom.spec.whatwg.org/#dom-domimplementation-createdocument
274
- * @see https://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument
275
- */
276
- hasDefaultHTMLNamespace: function (mimeType) {
277
- return MIME_TYPE.isHTML(mimeType) || mimeType === MIME_TYPE.XML_XHTML_APPLICATION;
278
- },
279
-
280
316
  /**
281
317
  * `application/xml`, the standard mime type for XML documents.
282
318
  *
283
- * @see https://www.iana.org/assignments/media-types/application/xml IANA MimeType registration
319
+ * @see https://www.iana.org/assignments/media-types/application/xml IANA MimeType
320
+ * registration
284
321
  * @see https://tools.ietf.org/html/rfc7303#section-9.1 RFC 7303
285
322
  * @see https://en.wikipedia.org/wiki/XML_and_MIME Wikipedia
286
323
  */
@@ -299,7 +336,8 @@ var MIME_TYPE = freeze({
299
336
  * `application/xhtml+xml`, indicates an XML document that has the default HTML namespace,
300
337
  * but is parsed as an XML document.
301
338
  *
302
- * @see https://www.iana.org/assignments/media-types/application/xhtml+xml IANA MimeType registration
339
+ * @see https://www.iana.org/assignments/media-types/application/xhtml+xml IANA MimeType
340
+ * registration
303
341
  * @see https://dom.spec.whatwg.org/#dom-domimplementation-createdocument WHATWG DOM Spec
304
342
  * @see https://en.wikipedia.org/wiki/XHTML Wikipedia
305
343
  */
@@ -314,7 +352,30 @@ var MIME_TYPE = freeze({
314
352
  */
315
353
  XML_SVG_IMAGE: 'image/svg+xml',
316
354
  });
355
+ /**
356
+ * @typedef {'application/xhtml+xml' | 'application/xml' | 'image/svg+xml' | 'text/html' | 'text/xml'}
357
+ * MimeType
358
+ */
359
+ /**
360
+ * @type {MimeType[]}
361
+ * @private
362
+ * Basically `Object.values`, which is not available in ES5.
363
+ */
364
+ var _MIME_TYPES = Object.keys(MIME_TYPE).map(function (key) {
365
+ return MIME_TYPE[key];
366
+ });
317
367
 
368
+ /**
369
+ * Only returns true if `mimeType` is one of the allowed values for
370
+ * `DOMParser.parseFromString`.
371
+ *
372
+ * @param {string} mimeType
373
+ * @returns {mimeType is 'application/xhtml+xml' | 'application/xml' | 'image/svg+xml' | 'text/html' | 'text/xml'}
374
+ *
375
+ */
376
+ function isValidMimeType(mimeType) {
377
+ return _MIME_TYPES.indexOf(mimeType) > -1;
378
+ }
318
379
  /**
319
380
  * Namespaces that are used in this code base.
320
381
  *
@@ -328,17 +389,6 @@ var NAMESPACE = freeze({
328
389
  */
329
390
  HTML: 'http://www.w3.org/1999/xhtml',
330
391
 
331
- /**
332
- * Checks if `uri` equals `NAMESPACE.HTML`.
333
- *
334
- * @param {string} [uri]
335
- *
336
- * @see NAMESPACE.HTML
337
- */
338
- isHTML: function (uri) {
339
- return uri === NAMESPACE.HTML;
340
- },
341
-
342
392
  /**
343
393
  * The SVG namespace.
344
394
  *
@@ -354,44 +404,26 @@ var NAMESPACE = freeze({
354
404
  XML: 'http://www.w3.org/XML/1998/namespace',
355
405
 
356
406
  /**
357
- * The `xmlns:` namespace
407
+ * The `xmlns:` namespace.
358
408
  *
359
409
  * @see https://www.w3.org/2000/xmlns/
360
410
  */
361
411
  XMLNS: 'http://www.w3.org/2000/xmlns/',
362
412
  });
363
413
 
364
- // https://www.w3.org/TR/xml-names/#ns-decl
365
- // https://www.w3.org/TR/REC-xml/#NT-Name
366
- // https://www.w3.org/TR/xml-names/#ns-qualnames
367
- // NAME_START_CHAR without ":"
368
- var QNAME_START_CHAR =
369
- /[A-Z_a-z\xC0-\xD6\xD8-\xF6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/; //\u10000-\uEFFFF
370
- //[4] NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
371
- var NAME_START_CHAR = new RegExp('[:' + QNAME_START_CHAR.source.slice(1, -1) + ']');
372
- var NAME_CHAR = new RegExp('[\\-\\.0-9' + NAME_START_CHAR.source.slice(1, -1) + '\\u00B7\\u0300-\\u036F\\u203F-\\u2040]');
373
- //[4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
374
- // NAME_CHAR without ":"
375
- var QNAME_CHAR = new RegExp('[\\-\\.0-9' + QNAME_START_CHAR.source.slice(1, -1) + '\\u00B7\\u0300-\\u036F\\u203F-\\u2040]*');
376
- // https://www.w3.org/TR/xml-names/#NT-NCName
377
- var NCNAME = QNAME_START_CHAR.source + QNAME_CHAR.source;
378
- // https://www.w3.org/TR/xml-names/#ns-qualnames
379
- var QNAME = new RegExp('^' + NCNAME + '(?::' + NCNAME + ')?$');
380
-
381
414
  exports.assign = assign;
382
415
  exports.find = find;
383
416
  exports.freeze = freeze;
384
417
  exports.HTML_BOOLEAN_ATTRIBUTES = HTML_BOOLEAN_ATTRIBUTES;
385
418
  exports.HTML_RAW_TEXT_ELEMENTS = HTML_RAW_TEXT_ELEMENTS;
386
419
  exports.HTML_VOID_ELEMENTS = HTML_VOID_ELEMENTS;
420
+ exports.hasDefaultHTMLNamespace = hasDefaultHTMLNamespace;
421
+ exports.hasOwn = hasOwn;
387
422
  exports.isHTMLBooleanAttribute = isHTMLBooleanAttribute;
388
423
  exports.isHTMLRawTextElement = isHTMLRawTextElement;
389
424
  exports.isHTMLEscapableRawTextElement = isHTMLEscapableRawTextElement;
425
+ exports.isHTMLMimeType = isHTMLMimeType;
390
426
  exports.isHTMLVoidElement = isHTMLVoidElement;
427
+ exports.isValidMimeType = isValidMimeType;
391
428
  exports.MIME_TYPE = MIME_TYPE;
392
- exports.NAME_CHAR = NAME_CHAR;
393
- exports.NAME_START_CHAR = NAME_START_CHAR;
394
429
  exports.NAMESPACE = NAMESPACE;
395
- exports.QNAME = QNAME;
396
- exports.QNAME_CHAR = QNAME_CHAR;
397
- exports.QNAME_START_CHAR = QNAME_START_CHAR;