@xmldom/xmldom 0.8.2 → 0.9.0-beta.2

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 CHANGED
@@ -4,6 +4,57 @@ All notable changes to this project will be documented in this file.
4
4
 
5
5
  This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.9.0-beta.2](https://github.com/xmldom/xmldom/compare/0.9.0-beta.1...0.9.0-beta.2)
8
+
9
+
10
+ ### Fixed
11
+ - Avoid iterating over prototype properties [`#437`](https://github.com/xmldom/xmldom/pull/437) / [`#436`](https://github.com/xmldom/xmldom/issues/436)
12
+
13
+ Thank you, [@Supraja9726](https://github.com/Supraja9726) for your contributions
14
+
15
+ ## [0.9.0-beta.1](https://github.com/xmldom/xmldom/compare/0.8.2...0.9.0-beta.1)
16
+
17
+ ### Fixed
18
+
19
+ **Only use HTML rules if mimeType matches** [`#338`](https://github.com/xmldom/xmldom/pull/338), fixes [`#203`](https://github.com/xmldom/xmldom/issues/203)
20
+
21
+ In the living specs for parsing XML and HTML, that this library is trying to implement,
22
+ there is a distinction between the different types of documents being parsed:
23
+ There are quite some rules that are different for parsing, constructing and serializing XML vs HTML documents.
24
+
25
+ So far xmldom was always "detecting" whether "the HTML rules should be applied" by looking at the current namespace. So from the first time an the HTML default namespace (`http://www.w3.org/1999/xhtml`) was found, every node was treated as being part of an HTML document. This misconception is the root cause for quite some reported bugs.
26
+
27
+ BREAKING CHANGE: HTML rules are no longer applied just because of the namespace, but require the `mimeType` argument passed to `DOMParser.parseFromString(source, mimeType)` to match `'text/html'`. Doing so implies all rules for handling casing for tag and attribute names when parsing, creation of nodes and searching nodes.
28
+
29
+ BREAKING CHANGE: Correct the return type of `DOMParser.parseFromString` to `Document | undefined`. In case of parsing errors it was always possible that "the returned `Document`" has not been created. In case you are using Typescript you now need to handle those cases.
30
+
31
+ BREAKING CHANGE: The instance property `DOMParser.options` is no longer available, instead use the individual `readonly` property per option (`assign`, `domHandler`, `errorHandler`, `normalizeLineEndings`, `locator`, `xmlns`). Those also provides the default value if the option was not passed. The 'locator' option is now just a boolean (default remains `true`).
32
+
33
+ BREAKING CHANGE: The following methods no longer allow a (non spec compliant) boolean argument to toggle "HTML rules":
34
+ - `XMLSerializer.serializeToString`
35
+ - `Node.toString`
36
+ - `Document.toString`
37
+
38
+ The following interfaces have been implemented:
39
+ `DOMImplementation` now implements all methods defined in the DOM spec, but not all of the behavior is implemented (see docstring):
40
+ - `createDocument` creates an "XML Document" (prototype: `Document`, property `type` is `'xml'`)
41
+ - `createHTMLDocument` creates an "HTML Document" (type/prototype: `Document`, property `type` is `'html'`).
42
+ - when no argument is passed or the first argument is a string, the basic nodes for an HTML structure are created, as specified
43
+ - when the first argument is `false` no child nodes are created
44
+
45
+ `Document` now has two new readonly properties as specified in the DOM spec:
46
+ - `contentType` which is the mime-type that was used to create the document
47
+ - `type` which is either the string literal `'xml'` or `'html'`
48
+
49
+ `MIME_TYPE` (`/lib/conventions.js`):
50
+ - `hasDefaultHTMLNamespace` test if the provided string is one of the miem types that implies the default HTML namespace: `text/html` or `application/xhtml+xml`
51
+
52
+ Thank you [@weiwu-zhang](https://github.com/weiwu-zhang) for your contributions
53
+
54
+ ### Chore
55
+
56
+ - update multiple devDependencies
57
+
7
58
  ## [0.8.2](https://github.com/xmldom/xmldom/compare/0.8.1...0.8.2)
8
59
 
9
60
  ### Fixed
@@ -59,7 +110,7 @@ Thank you [@marrus-sh](https://github.com/marrus-sh), [@victorandree](https://gi
59
110
  ### Fixes:
60
111
 
61
112
  - Preserve default namespace when serializing [`#319`](https://github.com/xmldom/xmldom/issues/319) / [`#321`](https://github.com/xmldom/xmldom/pull/321)
62
- Thank you [@lupestro](https://github.com/lupestro)
113
+ Thank you, [@lupestro](https://github.com/lupestro)
63
114
 
64
115
  ## 0.7.4
65
116
 
@@ -68,7 +119,7 @@ Thank you [@marrus-sh](https://github.com/marrus-sh), [@victorandree](https://gi
68
119
  ### Fixes:
69
120
 
70
121
  - Restore ability to parse `__prototype__` attributes [`#315`](https://github.com/xmldom/xmldom/pull/315)
71
- Thank you [@dsimpsonOMF](https://github.com/dsimpsonOMF)
122
+ Thank you, [@dsimpsonOMF](https://github.com/dsimpsonOMF)
72
123
 
73
124
  ## 0.7.3
74
125
 
@@ -78,7 +129,7 @@ Thank you [@marrus-sh](https://github.com/marrus-sh), [@victorandree](https://gi
78
129
 
79
130
  - Add doctype when parsing from string [`#277`](https://github.com/xmldom/xmldom/issues/277) / [`#301`](https://github.com/xmldom/xmldom/pull/301)
80
131
  - Correct typo in error message [`#294`](https://github.com/xmldom/xmldom/pull/294)
81
- Thank you [@rrthomas](https://github.com/rrthomas)
132
+ Thank you, [@rrthomas](https://github.com/rrthomas)
82
133
 
83
134
  ### Refactor:
84
135
 
@@ -103,7 +154,7 @@ Thank you [@marrus-sh](https://github.com/marrus-sh), [@victorandree](https://gi
103
154
  ### Fixes:
104
155
 
105
156
  - Types: Add index.d.ts to packaged files [`#288`](https://github.com/xmldom/xmldom/pull/288)
106
- Thank you [@forty](https://github.com/forty)
157
+ Thank you, [@forty](https://github.com/forty)
107
158
 
108
159
  ## 0.7.1
109
160
 
@@ -112,7 +163,7 @@ Thank you [@marrus-sh](https://github.com/marrus-sh), [@victorandree](https://gi
112
163
  ### Fixes:
113
164
 
114
165
  - Types: Copy types from DefinitelyTyped [`#283`](https://github.com/xmldom/xmldom/pull/283)
115
- Thank you [@kachkaev](https://github.com/kachkaev)
166
+ Thank you, [@kachkaev](https://github.com/kachkaev)
116
167
 
117
168
  ### Chore:
118
169
  - package.json: remove author, maintainers, etc. [`#279`](https://github.com/xmldom/xmldom/pull/279)
@@ -129,7 +180,7 @@ For more details look at [`#278`](https://github.com/xmldom/xmldom/pull/278#issu
129
180
  ### Fixes:
130
181
 
131
182
  - Security: Misinterpretation of malicious XML input [`CVE-2021-32796`](https://github.com/xmldom/xmldom/security/advisories/GHSA-5fg8-2547-mr8q)
132
- - Implement `Document.getElementsByClassName` as specified [`#213`](https://github.com/xmldom/xmldom/pull/213), thank you [@ChALkeR](https://github.com/ChALkeR)
183
+ - Implement `Document.getElementsByClassName` as specified [`#213`](https://github.com/xmldom/xmldom/pull/213), thank you, [@ChALkeR](https://github.com/ChALkeR)
133
184
  - Inherit namespace prefix from parent when required [`#268`](https://github.com/xmldom/xmldom/pull/268)
134
185
  - Handle whitespace in closing tags [`#267`](https://github.com/xmldom/xmldom/pull/267)
135
186
  - Update `DOMImplementation` according to recent specs [`#210`](https://github.com/xmldom/xmldom/pull/210)
@@ -137,7 +188,7 @@ For more details look at [`#278`](https://github.com/xmldom/xmldom/pull/278#issu
137
188
  - No longer serializes any namespaces with an empty URI [`#244`](https://github.com/xmldom/xmldom/pull/244)
138
189
  (related to [`#168`](https://github.com/xmldom/xmldom/pull/168) released in 0.6.0)
139
190
  BREAKING CHANGE: Only if you rely on ["unsetting" a namespace prefix](https://github.com/xmldom/xmldom/pull/168#issuecomment-886984994) by setting it to an empty string
140
- - Set `localName` as part of `Document.createElement` [`#229`](https://github.com/xmldom/xmldom/pull/229), thank you [@rrthomas](https://github.com/rrthomas)
191
+ - Set `localName` as part of `Document.createElement` [`#229`](https://github.com/xmldom/xmldom/pull/229), thank you, [@rrthomas](https://github.com/rrthomas)
141
192
 
142
193
  ### CI
143
194
 
@@ -156,7 +207,7 @@ For more details look at [`#278`](https://github.com/xmldom/xmldom/pull/278#issu
156
207
 
157
208
  - Stop serializing empty namespace values like `xmlns:ds=""` [`#168`](https://github.com/xmldom/xmldom/pull/168)
158
209
  BREAKING CHANGE: If your code expected empty namespaces attributes to be serialized.
159
- Thank you [@pdecat](https://github.com/pdecat) and [@FranckDepoortere](https://github.com/FranckDepoortere)
210
+ Thank you, [@pdecat](https://github.com/pdecat) and [@FranckDepoortere](https://github.com/FranckDepoortere)
160
211
  - Escape `<` to `&lt;` when serializing attribute values [`#198`](https://github.com/xmldom/xmldom/issues/198) / [`#199`](https://github.com/xmldom/xmldom/pull/199)
161
212
 
162
213
  ## 0.5.0
package/index.d.ts CHANGED
@@ -1,43 +1,45 @@
1
1
  /// <reference lib="dom" />
2
2
 
3
- declare module "@xmldom/xmldom" {
4
- var DOMParser: DOMParserStatic;
5
- var XMLSerializer: XMLSerializerStatic;
6
- var DOMImplementation: DOMImplementationStatic;
7
-
8
- interface DOMImplementationStatic {
9
- new(): DOMImplementation;
10
- }
11
-
12
- interface DOMParserStatic {
13
- new (): DOMParser;
14
- new (options: Options): DOMParser;
15
- }
16
-
17
- interface XMLSerializerStatic {
18
- new (): XMLSerializer;
19
- }
20
-
21
- interface DOMParser {
22
- parseFromString(xmlsource: string, mimeType?: string): Document;
23
- }
24
-
25
- interface XMLSerializer {
26
- serializeToString(node: Node): string;
27
- }
28
-
29
- interface Options {
30
- locator?: any;
31
- errorHandler?: ErrorHandlerFunction | ErrorHandlerObject | undefined;
32
- }
33
-
34
- interface ErrorHandlerFunction {
35
- (level: string, msg: any): any;
36
- }
37
-
38
- interface ErrorHandlerObject {
39
- warning?: ((msg: any) => any) | undefined;
40
- error?: ((msg: any) => any) | undefined;
41
- fatalError?: ((msg: any) => any) | undefined;
42
- }
3
+ declare module '@xmldom/xmldom' {
4
+ var DOMParser: DOMParserStatic
5
+ var XMLSerializer: XMLSerializerStatic
6
+ var DOMImplementation: DOMImplementationStatic
7
+
8
+ interface DOMImplementationStatic {
9
+ new (): DOMImplementation
10
+ }
11
+
12
+ interface DOMParserStatic {
13
+ new (): DOMParser
14
+ new (options: DOMParserOptions): DOMParser
15
+ }
16
+
17
+ interface XMLSerializerStatic {
18
+ new (): XMLSerializer
19
+ }
20
+
21
+ interface DOMParser {
22
+ parseFromString(source: string, mimeType?: string): Document | undefined
23
+ }
24
+
25
+ interface XMLSerializer {
26
+ serializeToString(node: Node): string
27
+ }
28
+
29
+ interface DOMParserOptions {
30
+ errorHandler?: ErrorHandlerFunction | ErrorHandlerObject
31
+ locator?: boolean
32
+ normalizeLineEndings?: (source: string) => string
33
+ xmlns?: Record<string, string | null | undefined>
34
+ }
35
+
36
+ interface ErrorHandlerFunction {
37
+ (level: 'warn' | 'error' | 'fatalError', msg: string): void
38
+ }
39
+
40
+ interface ErrorHandlerObject {
41
+ warning?: (msg: string) => void
42
+ error?: (msg: string) => void
43
+ fatalError?: (msg: string) => void
44
+ }
43
45
  }
@@ -9,7 +9,7 @@
9
9
  *
10
10
  * @template T
11
11
  * @param {T} object the object to freeze
12
- * @param {Pick<ObjectConstructor, 'freeze'> = Object} oc `Object` by default,
12
+ * @param {Pick<ObjectConstructor, 'freeze'>} [oc=Object] `Object` by default,
13
13
  * allows to inject custom object constructor for tests
14
14
  * @returns {Readonly<T>}
15
15
  *
@@ -47,6 +47,155 @@ function assign(target, source) {
47
47
  return target
48
48
  }
49
49
 
50
+ /**
51
+ * A number of attributes are boolean attributes.
52
+ * The presence of a boolean attribute on an element represents the `true` value,
53
+ * and the absence of the attribute represents the `false` value.
54
+ *
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,
57
+ * with no leading or trailing whitespace.
58
+ *
59
+ * Note: The values `"true"` and `"false"` are not allowed on boolean attributes.
60
+ * To represent a `false` value, the attribute has to be omitted altogether.
61
+ *
62
+ * @see https://html.spec.whatwg.org/#boolean-attributes
63
+ * @see https://html.spec.whatwg.org/#attributes-3
64
+ */
65
+ var HTML_BOOLEAN_ATTRIBUTES = freeze({
66
+ allowfullscreen: true,
67
+ async: true,
68
+ autofocus: true,
69
+ autoplay: true,
70
+ checked: true,
71
+ controls: true,
72
+ default: true,
73
+ defer: true,
74
+ disabled: true,
75
+ formnovalidate: true,
76
+ hidden: true,
77
+ ismap: true,
78
+ itemscope: true,
79
+ loop: true,
80
+ multiple: true,
81
+ muted: true,
82
+ nomodule: true,
83
+ novalidate: true,
84
+ open: true,
85
+ playsinline: true,
86
+ readonly: true,
87
+ required: true,
88
+ reversed: true,
89
+ selected: true,
90
+ })
91
+
92
+ /**
93
+ * 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.
95
+ *
96
+ * @param {string} name
97
+ * @return {boolean}
98
+ * @see HTML_BOOLEAN_ATTRIBUTES
99
+ * @see https://html.spec.whatwg.org/#boolean-attributes
100
+ * @see https://html.spec.whatwg.org/#attributes-3
101
+ */
102
+ function isHTMLBooleanAttribute(name) {
103
+ return HTML_BOOLEAN_ATTRIBUTES.hasOwnProperty(name.toLowerCase())
104
+ }
105
+
106
+ /**
107
+ * 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 />`.
109
+ * This should not be confused with optional tags that HTML allows to omit the end tag for
110
+ * (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.
114
+ *
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}>}
116
+ * @see https://html.spec.whatwg.org/#void-elements
117
+ * @see https://html.spec.whatwg.org/#optional-tags
118
+ */
119
+ var HTML_VOID_ELEMENTS = freeze({
120
+ area: true,
121
+ base: true,
122
+ br: true,
123
+ col: true,
124
+ embed: true,
125
+ hr: true,
126
+ img: true,
127
+ input: true,
128
+ link: true,
129
+ meta: true,
130
+ param: true,
131
+ source: true,
132
+ track: true,
133
+ wbr: true,
134
+ })
135
+
136
+ /**
137
+ * 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.
140
+ *
141
+ * @param {string} tagName
142
+ * @return {boolean}
143
+ * @see HTML_VOID_ELEMENTS
144
+ * @see https://html.spec.whatwg.org/#void-elements
145
+ */
146
+ function isHTMLVoidElement(tagName) {
147
+ return HTML_VOID_ELEMENTS.hasOwnProperty(tagName.toLowerCase())
148
+ }
149
+
150
+ /**
151
+ * Tag names that are raw text elements according to HTML spec.
152
+ * The value denotes whether they are escapable or not.
153
+ *
154
+ * @see isHTMLEscapableRawTextElement
155
+ * @see isHTMLRawTextElement
156
+ * @see https://html.spec.whatwg.org/#raw-text-elements
157
+ * @see https://html.spec.whatwg.org/#escapable-raw-text-elements
158
+ */
159
+ var HTML_RAW_TEXT_ELEMENTS = freeze({
160
+ script: false,
161
+ style: false,
162
+ textarea: true,
163
+ title: true,
164
+ })
165
+
166
+ /**
167
+ * 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.
170
+ *
171
+ * @param {string} tagName
172
+ * @return {boolean}
173
+ * @see isHTMLEscapableRawTextElement
174
+ * @see HTML_RAW_TEXT_ELEMENTS
175
+ * @see https://html.spec.whatwg.org/#raw-text-elements
176
+ * @see https://html.spec.whatwg.org/#escapable-raw-text-elements
177
+ */
178
+ function isHTMLRawTextElement(tagName) {
179
+ var key = tagName.toLowerCase();
180
+ return HTML_RAW_TEXT_ELEMENTS.hasOwnProperty(key) && !HTML_RAW_TEXT_ELEMENTS[key];
181
+ }
182
+ /**
183
+ * 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.
186
+ *
187
+ * @param {string} tagName
188
+ * @return {boolean}
189
+ * @see isHTMLRawTextElement
190
+ * @see HTML_RAW_TEXT_ELEMENTS
191
+ * @see https://html.spec.whatwg.org/#raw-text-elements
192
+ * @see https://html.spec.whatwg.org/#escapable-raw-text-elements
193
+ */
194
+ function isHTMLEscapableRawTextElement(tagName) {
195
+ var key = tagName.toLowerCase();
196
+ return HTML_RAW_TEXT_ELEMENTS.hasOwnProperty(key) && HTML_RAW_TEXT_ELEMENTS[key];
197
+ }
198
+
50
199
  /**
51
200
  * All mime types that are allowed as input to `DOMParser.parseFromString`
52
201
  *
@@ -72,14 +221,32 @@ var MIME_TYPE = freeze({
72
221
  * @param {string} [value]
73
222
  * @returns {boolean}
74
223
  *
75
- * @see https://www.iana.org/assignments/media-types/text/html IANA MimeType registration
76
- * @see https://en.wikipedia.org/wiki/HTML Wikipedia
77
- * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString MDN
78
- * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring */
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
+ */
79
229
  isHTML: function (value) {
80
230
  return value === MIME_TYPE.HTML
81
231
  },
82
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
+
83
250
  /**
84
251
  * `application/xml`, the standard mime type for XML documents.
85
252
  *
@@ -164,7 +331,14 @@ var NAMESPACE = freeze({
164
331
  XMLNS: 'http://www.w3.org/2000/xmlns/',
165
332
  })
166
333
 
167
- exports.assign = assign;
168
- exports.freeze = freeze;
169
- exports.MIME_TYPE = MIME_TYPE;
170
- exports.NAMESPACE = NAMESPACE;
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