@xmldom/xmldom 0.8.10 → 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,17 +1,17 @@
1
- 'use strict'
1
+ 'use strict';
2
2
 
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
  */
@@ -39,83 +39,265 @@ 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'> = Object} oc `Object` by default,
45
- * 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.
46
47
  * @returns {Readonly<T>}
47
- *
48
+ * @template {Object} T
48
49
  * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
49
50
  */
50
51
  function freeze(object, oc) {
51
52
  if (oc === undefined) {
52
- oc = Object
53
+ oc = Object;
53
54
  }
54
- return oc && typeof oc.freeze === 'function' ? oc.freeze(object) : object
55
+ return oc && typeof oc.freeze === 'function' ? oc.freeze(object) : object;
55
56
  }
56
57
 
57
58
  /**
58
- * Since we can not rely on `Object.assign` we provide a simplified version
59
- * 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.
60
61
  *
61
62
  * @param {Object} target
62
63
  * @param {Object | null | undefined} source
63
- *
64
- * @returns {Object} target
65
- * @throws TypeError if target is not an object
66
- *
64
+ * @returns {Object} The target with the merged/overridden properties.
65
+ * @throws {TypeError}
66
+ * If target is not an object.
67
67
  * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
68
68
  * @see https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.assign
69
69
  */
70
70
  function assign(target, source) {
71
71
  if (target === null || typeof target !== 'object') {
72
- throw new TypeError('target is not an object')
72
+ throw new TypeError('target is not an object');
73
73
  }
74
74
  for (var key in source) {
75
75
  if (Object.prototype.hasOwnProperty.call(source, key)) {
76
- target[key] = source[key]
76
+ target[key] = source[key];
77
77
  }
78
78
  }
79
- return target
79
+ return target;
80
+ }
81
+
82
+ /**
83
+ * A number of attributes are boolean attributes.
84
+ * The presence of a boolean attribute on an element represents the `true` value,
85
+ * and the absence of the attribute represents the `false` value.
86
+ *
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,
89
+ * with no leading or trailing whitespace.
90
+ *
91
+ * Note: The values `"true"` and `"false"` are not allowed on boolean attributes.
92
+ * To represent a `false` value, the attribute has to be omitted altogether.
93
+ *
94
+ * @see https://html.spec.whatwg.org/#boolean-attributes
95
+ * @see https://html.spec.whatwg.org/#attributes-3
96
+ */
97
+ var HTML_BOOLEAN_ATTRIBUTES = freeze({
98
+ allowfullscreen: true,
99
+ async: true,
100
+ autofocus: true,
101
+ autoplay: true,
102
+ checked: true,
103
+ controls: true,
104
+ default: true,
105
+ defer: true,
106
+ disabled: true,
107
+ formnovalidate: true,
108
+ hidden: true,
109
+ ismap: true,
110
+ itemscope: true,
111
+ loop: true,
112
+ multiple: true,
113
+ muted: true,
114
+ nomodule: true,
115
+ novalidate: true,
116
+ open: true,
117
+ playsinline: true,
118
+ readonly: true,
119
+ required: true,
120
+ reversed: true,
121
+ selected: true,
122
+ });
123
+
124
+ /**
125
+ * 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
127
+ * document/parsing.
128
+ *
129
+ * @param {string} name
130
+ * @returns {boolean}
131
+ * @see {@link HTML_BOOLEAN_ATTRIBUTES}
132
+ * @see https://html.spec.whatwg.org/#boolean-attributes
133
+ * @see https://html.spec.whatwg.org/#attributes-3
134
+ */
135
+ function isHTMLBooleanAttribute(name) {
136
+ return HTML_BOOLEAN_ATTRIBUTES.hasOwnProperty(name.toLowerCase());
137
+ }
138
+
139
+ /**
140
+ * Void elements only have a start tag; end tags must not be specified for void elements.
141
+ * These elements should be written as self-closing like this: `<area />`.
142
+ * This should not be confused with optional tags that HTML allows to omit the end tag for
143
+ * (like `li`, `tr` and others), which can have content after them,
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.
149
+ *
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
+ * }>}
166
+ * @see https://html.spec.whatwg.org/#void-elements
167
+ * @see https://html.spec.whatwg.org/#optional-tags
168
+ */
169
+ var HTML_VOID_ELEMENTS = freeze({
170
+ area: true,
171
+ base: true,
172
+ br: true,
173
+ col: true,
174
+ embed: true,
175
+ hr: true,
176
+ img: true,
177
+ input: true,
178
+ link: true,
179
+ meta: true,
180
+ param: true,
181
+ source: true,
182
+ track: true,
183
+ wbr: true,
184
+ });
185
+
186
+ /**
187
+ * Check if `tagName` is matching one of the HTML void element names.
188
+ * This method doesn't check if such tags are allowed in the context of the current
189
+ * document/parsing.
190
+ *
191
+ * @param {string} tagName
192
+ * @returns {boolean}
193
+ * @see {@link HTML_VOID_ELEMENTS}
194
+ * @see https://html.spec.whatwg.org/#void-elements
195
+ */
196
+ function isHTMLVoidElement(tagName) {
197
+ return HTML_VOID_ELEMENTS.hasOwnProperty(tagName.toLowerCase());
198
+ }
199
+
200
+ /**
201
+ * Tag names that are raw text elements according to HTML spec.
202
+ * The value denotes whether they are escapable or not.
203
+ *
204
+ * @see {@link isHTMLEscapableRawTextElement}
205
+ * @see {@link isHTMLRawTextElement}
206
+ * @see https://html.spec.whatwg.org/#raw-text-elements
207
+ * @see https://html.spec.whatwg.org/#escapable-raw-text-elements
208
+ */
209
+ var HTML_RAW_TEXT_ELEMENTS = freeze({
210
+ script: false,
211
+ style: false,
212
+ textarea: true,
213
+ title: true,
214
+ });
215
+
216
+ /**
217
+ * Check if `tagName` is matching one of the HTML raw text element names.
218
+ * This method doesn't check if such tags are allowed in the context of the current
219
+ * document/parsing.
220
+ *
221
+ * @param {string} tagName
222
+ * @returns {boolean}
223
+ * @see {@link isHTMLEscapableRawTextElement}
224
+ * @see {@link HTML_RAW_TEXT_ELEMENTS}
225
+ * @see https://html.spec.whatwg.org/#raw-text-elements
226
+ * @see https://html.spec.whatwg.org/#escapable-raw-text-elements
227
+ */
228
+ function isHTMLRawTextElement(tagName) {
229
+ var key = tagName.toLowerCase();
230
+ return HTML_RAW_TEXT_ELEMENTS.hasOwnProperty(key) && !HTML_RAW_TEXT_ELEMENTS[key];
231
+ }
232
+ /**
233
+ * Check if `tagName` is matching one of the HTML escapable raw text element names.
234
+ * This method doesn't check if such tags are allowed in the context of the current
235
+ * document/parsing.
236
+ *
237
+ * @param {string} tagName
238
+ * @returns {boolean}
239
+ * @see {@link isHTMLRawTextElement}
240
+ * @see {@link HTML_RAW_TEXT_ELEMENTS}
241
+ * @see https://html.spec.whatwg.org/#raw-text-elements
242
+ * @see https://html.spec.whatwg.org/#escapable-raw-text-elements
243
+ */
244
+ function isHTMLEscapableRawTextElement(tagName) {
245
+ var key = tagName.toLowerCase();
246
+ return HTML_RAW_TEXT_ELEMENTS.hasOwnProperty(key) && HTML_RAW_TEXT_ELEMENTS[key];
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;
80
273
  }
81
274
 
82
275
  /**
83
276
  * All mime types that are allowed as input to `DOMParser.parseFromString`
84
277
  *
85
- * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString#Argument02 MDN
86
- * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#domparsersupportedtype WHATWG HTML Spec
87
- * @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}
88
283
  */
89
284
  var MIME_TYPE = freeze({
90
285
  /**
91
286
  * `text/html`, the only mime type that triggers treating an XML document as HTML.
92
287
  *
93
- * @see DOMParser.SupportedType.isHTML
94
288
  * @see https://www.iana.org/assignments/media-types/text/html IANA MimeType registration
95
289
  * @see https://en.wikipedia.org/wiki/HTML Wikipedia
96
290
  * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString MDN
97
- * @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
98
293
  */
99
294
  HTML: 'text/html',
100
295
 
101
- /**
102
- * Helper method to check a mime type if it indicates an HTML document
103
- *
104
- * @param {string} [value]
105
- * @returns {boolean}
106
- *
107
- * @see https://www.iana.org/assignments/media-types/text/html IANA MimeType registration
108
- * @see https://en.wikipedia.org/wiki/HTML Wikipedia
109
- * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString MDN
110
- * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring */
111
- isHTML: function (value) {
112
- return value === MIME_TYPE.HTML
113
- },
114
-
115
296
  /**
116
297
  * `application/xml`, the standard mime type for XML documents.
117
298
  *
118
- * @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
119
301
  * @see https://tools.ietf.org/html/rfc7303#section-9.1 RFC 7303
120
302
  * @see https://en.wikipedia.org/wiki/XML_and_MIME Wikipedia
121
303
  */
@@ -134,7 +316,8 @@ var MIME_TYPE = freeze({
134
316
  * `application/xhtml+xml`, indicates an XML document that has the default HTML namespace,
135
317
  * but is parsed as an XML document.
136
318
  *
137
- * @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
138
321
  * @see https://dom.spec.whatwg.org/#dom-domimplementation-createdocument WHATWG DOM Spec
139
322
  * @see https://en.wikipedia.org/wiki/XHTML Wikipedia
140
323
  */
@@ -148,8 +331,30 @@ var MIME_TYPE = freeze({
148
331
  * @see https://en.wikipedia.org/wiki/Scalable_Vector_Graphics Wikipedia
149
332
  */
150
333
  XML_SVG_IMAGE: 'image/svg+xml',
151
- })
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
+ });
152
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
+ }
153
358
  /**
154
359
  * Namespaces that are used in this code base.
155
360
  *
@@ -163,17 +368,6 @@ var NAMESPACE = freeze({
163
368
  */
164
369
  HTML: 'http://www.w3.org/1999/xhtml',
165
370
 
166
- /**
167
- * Checks if `uri` equals `NAMESPACE.HTML`.
168
- *
169
- * @param {string} [uri]
170
- *
171
- * @see NAMESPACE.HTML
172
- */
173
- isHTML: function (uri) {
174
- return uri === NAMESPACE.HTML
175
- },
176
-
177
371
  /**
178
372
  * The SVG namespace.
179
373
  *
@@ -189,15 +383,42 @@ var NAMESPACE = freeze({
189
383
  XML: 'http://www.w3.org/XML/1998/namespace',
190
384
 
191
385
  /**
192
- * The `xmlns:` namespace
386
+ * The `xmlns:` namespace.
193
387
  *
194
388
  * @see https://www.w3.org/2000/xmlns/
195
389
  */
196
390
  XMLNS: 'http://www.w3.org/2000/xmlns/',
197
- })
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;
198
408
 
199
409
  exports.assign = assign;
200
410
  exports.find = find;
201
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;
202
422
  exports.MIME_TYPE = MIME_TYPE;
203
423
  exports.NAMESPACE = NAMESPACE;
424
+ exports.ParseError = ParseError;