@xmldom/xmldom 0.9.0-beta.1 → 0.9.0-beta.10

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