@xmldom/xmldom 0.9.0-beta.7 → 0.9.0-beta.9
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 +47 -0
- package/index.d.ts +361 -21
- package/lib/conventions.js +66 -65
- package/lib/dom-parser.js +140 -81
- package/lib/dom.js +48 -40
- package/lib/grammar.js +510 -0
- package/lib/index.js +15 -1
- package/lib/sax.js +286 -106
- package/package.json +4 -5
- package/readme.md +31 -42
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,53 @@ 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.9](https://github.com/xmldom/xmldom/compare/0.9.0-beta.8...0.9.0-beta.9)
|
|
8
|
+
|
|
9
|
+
### Fixed
|
|
10
|
+
|
|
11
|
+
- Set nodeName property in ProcessingInstruction [`#509`](https://github.com/xmldom/xmldom/pull/509) / [`#505`](https://github.com/xmldom/xmldom/issues/505)
|
|
12
|
+
- preserve DOCTYPE internal subset [`#498`](https://github.com/xmldom/xmldom/pull/498) / [`#497`](https://github.com/xmldom/xmldom/pull/497) / [`#117`](https://github.com/xmldom/xmldom/issues/117)\
|
|
13
|
+
BREAKING CHANGES: Many documents that were previously accepted by xmldom, esecially non well-formed ones are no longer accepted. Some issues that were formerly reported as errors are now a fatalError.
|
|
14
|
+
- DOMParser: Align parseFromString errors with specs [`#454`](https://github.com/xmldom/xmldom/pull/454)
|
|
15
|
+
|
|
16
|
+
### Chore
|
|
17
|
+
|
|
18
|
+
- stop running mutation tests using stryker [`#496`](https://github.com/xmldom/xmldom/pull/496)
|
|
19
|
+
- make `toErrorSnapshot` windows compatible [`#503`](https://github.com/xmldom/xmldom/pull/503)
|
|
20
|
+
|
|
21
|
+
Thank you, [@cjbarth](https://github.com/cjbarth), [@shunkica](https://github.com/shunkica), [@pmahend1](https://github.com/pmahend1), [@niklasl](https://github.com/niklasl), for your contributions
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
## [0.8.9](https://github.com/xmldom/xmldom/compare/0.8.8...0.8.9)
|
|
25
|
+
|
|
26
|
+
### Fixed
|
|
27
|
+
|
|
28
|
+
- Set nodeName property in ProcessingInstruction [`#509`](https://github.com/xmldom/xmldom/pull/509) / [`#505`](https://github.com/xmldom/xmldom/issues/505)
|
|
29
|
+
|
|
30
|
+
Thank you, [@cjbarth](https://github.com/cjbarth), for your contributions
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
## [0.7.12](https://github.com/xmldom/xmldom/compare/0.7.11...0.7.12)
|
|
34
|
+
|
|
35
|
+
### Fixed
|
|
36
|
+
|
|
37
|
+
- Set nodeName property in ProcessingInstruction [`#509`](https://github.com/xmldom/xmldom/pull/509) / [`#505`](https://github.com/xmldom/xmldom/issues/505)
|
|
38
|
+
|
|
39
|
+
Thank you, [@cjbarth](https://github.com/cjbarth), for your contributions
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
## [0.9.0-beta.8](https://github.com/xmldom/xmldom/compare/0.9.0-beta.7...0.9.0-beta.8)
|
|
43
|
+
|
|
44
|
+
### Fixed
|
|
45
|
+
|
|
46
|
+
- Throw DOMException when calling removeChild with invalid parameter [`#494`](https://github.com/xmldom/xmldom/pull/494) / [`#135`](https://github.com/xmldom/xmldom/issues/135)
|
|
47
|
+
|
|
48
|
+
BREAKING CHANGE: Previously it was possible (but not documented) to call `Node.removeChild` with any node in the tree,
|
|
49
|
+
and with certain exceptions, it would work. This is no longer the case: calling `Node.removeChild` with an argument that is not a direct child of the node that it is called from, will throw a NotFoundError DOMException, as it is described by the specs.
|
|
50
|
+
|
|
51
|
+
Thank you, [@noseworthy](https://github.com/noseworthy), [@davidmc24](https://github.com/davidmc24), for your contributions
|
|
52
|
+
|
|
53
|
+
|
|
7
54
|
## [0.9.0-beta.7](https://github.com/xmldom/xmldom/compare/0.9.0-beta.6...0.9.0-beta.7)
|
|
8
55
|
|
|
9
56
|
### Feature
|
package/index.d.ts
CHANGED
|
@@ -1,45 +1,385 @@
|
|
|
1
1
|
/// <reference lib="dom" />
|
|
2
2
|
|
|
3
3
|
declare module '@xmldom/xmldom' {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
// START ./lib/conventions.js
|
|
5
|
+
/**
|
|
6
|
+
* Since xmldom can not rely on `Object.assign`,
|
|
7
|
+
* it uses/provides a simplified version that is sufficient for its needs.
|
|
8
|
+
*
|
|
9
|
+
* @throws TypeError if target is not an object
|
|
10
|
+
*
|
|
11
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
|
|
12
|
+
* @see https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.assign
|
|
13
|
+
*/
|
|
14
|
+
function assign<T, S>(target:T, source:S): T & S;
|
|
15
|
+
/**
|
|
16
|
+
* Only returns true if `value` matches MIME_TYPE.HTML, which indicates an HTML document.
|
|
17
|
+
*
|
|
18
|
+
* @see https://www.iana.org/assignments/media-types/text/html
|
|
19
|
+
* @see https://en.wikipedia.org/wiki/HTML
|
|
20
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString
|
|
21
|
+
* @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring
|
|
22
|
+
*/
|
|
23
|
+
function isHtmlMimeType(mimeType: string): mimeType is MIME_TYPE.HTML;
|
|
24
|
+
/**
|
|
25
|
+
* Only returns true if `mimeType` is one of the allowed values for `DOMParser.parseFromString`.
|
|
26
|
+
*/
|
|
27
|
+
function isValidMimeType(mimeType: string): mimeType is MIME_TYPE;
|
|
7
28
|
|
|
8
|
-
|
|
9
|
-
|
|
29
|
+
/**
|
|
30
|
+
* All mime types that are allowed as input to `DOMParser.parseFromString`
|
|
31
|
+
*
|
|
32
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString#Argument02 MDN
|
|
33
|
+
* @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#domparsersupportedtype WHATWG HTML Spec
|
|
34
|
+
* @see DOMParser.prototype.parseFromString
|
|
35
|
+
*/
|
|
36
|
+
enum MIME_TYPE {
|
|
37
|
+
/**
|
|
38
|
+
* `text/html`, the only mime type that triggers treating an XML document as HTML.
|
|
39
|
+
*
|
|
40
|
+
* @see https://www.iana.org/assignments/media-types/text/html IANA MimeType registration
|
|
41
|
+
* @see https://en.wikipedia.org/wiki/HTML Wikipedia
|
|
42
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString MDN
|
|
43
|
+
* @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring WHATWG HTML Spec
|
|
44
|
+
*/
|
|
45
|
+
HTML = 'text/html',
|
|
46
|
+
/**
|
|
47
|
+
* `application/xml`, the standard mime type for XML documents.
|
|
48
|
+
*
|
|
49
|
+
* @see https://www.iana.org/assignments/media-types/application/xml IANA MimeType registration
|
|
50
|
+
* @see https://tools.ietf.org/html/rfc7303#section-9.1 RFC 7303
|
|
51
|
+
* @see https://en.wikipedia.org/wiki/XML_and_MIME Wikipedia
|
|
52
|
+
*/
|
|
53
|
+
XML_APPLICATION = 'application/xml',
|
|
54
|
+
/**
|
|
55
|
+
* `text/html`, an alias for `application/xml`.
|
|
56
|
+
*
|
|
57
|
+
* @see https://tools.ietf.org/html/rfc7303#section-9.2 RFC 7303
|
|
58
|
+
* @see https://www.iana.org/assignments/media-types/text/xml IANA MimeType registration
|
|
59
|
+
* @see https://en.wikipedia.org/wiki/XML_and_MIME Wikipedia
|
|
60
|
+
*/
|
|
61
|
+
XML_TEXT = 'text/xml',
|
|
62
|
+
/**
|
|
63
|
+
* `application/xhtml+xml`, indicates an XML document that has the default HTML namespace,
|
|
64
|
+
* but is parsed as an XML document.
|
|
65
|
+
*
|
|
66
|
+
* @see https://www.iana.org/assignments/media-types/application/xhtml+xml IANA MimeType registration
|
|
67
|
+
* @see https://dom.spec.whatwg.org/#dom-domimplementation-createdocument WHATWG DOM Spec
|
|
68
|
+
* @see https://en.wikipedia.org/wiki/XHTML Wikipedia
|
|
69
|
+
*/
|
|
70
|
+
XML_XHTML_APPLICATION = 'application/xhtml+xml',
|
|
71
|
+
/**
|
|
72
|
+
* `image/svg+xml`,
|
|
73
|
+
*
|
|
74
|
+
* @see https://www.iana.org/assignments/media-types/image/svg+xml IANA MimeType registration
|
|
75
|
+
* @see https://www.w3.org/TR/SVG11/ W3C SVG 1.1
|
|
76
|
+
* @see https://en.wikipedia.org/wiki/Scalable_Vector_Graphics Wikipedia
|
|
77
|
+
*/
|
|
78
|
+
XML_SVG_IMAGE = 'image/svg+xml',
|
|
10
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Namespaces that are used in xmldom.
|
|
82
|
+
*
|
|
83
|
+
* @see http://www.w3.org/TR/REC-xml-names
|
|
84
|
+
*/
|
|
85
|
+
enum NAMESPACE {
|
|
86
|
+
/**
|
|
87
|
+
* The XHTML namespace.
|
|
88
|
+
*
|
|
89
|
+
* @see http://www.w3.org/1999/xhtml
|
|
90
|
+
*/
|
|
91
|
+
HTML = 'http://www.w3.org/1999/xhtml',
|
|
92
|
+
/**
|
|
93
|
+
* The SVG namespace.
|
|
94
|
+
*
|
|
95
|
+
* @see http://www.w3.org/2000/svg
|
|
96
|
+
*/
|
|
97
|
+
SVG = 'http://www.w3.org/2000/svg',
|
|
98
|
+
/**
|
|
99
|
+
* The `xml:` namespace.
|
|
100
|
+
*
|
|
101
|
+
* @see http://www.w3.org/XML/1998/namespace
|
|
102
|
+
*/
|
|
103
|
+
XML = 'http://www.w3.org/XML/1998/namespace',
|
|
11
104
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
105
|
+
/**
|
|
106
|
+
* The `xmlns:` namespace
|
|
107
|
+
*
|
|
108
|
+
* @see https://www.w3.org/2000/xmlns/
|
|
109
|
+
*/
|
|
110
|
+
XMLNS = 'http://www.w3.org/2000/xmlns/',
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* A custom error that will not be caught by XMLReader aka the SAX parser.
|
|
115
|
+
*/
|
|
116
|
+
class ParseError extends Error {
|
|
117
|
+
constructor(message:string, locator?:any);
|
|
118
|
+
}
|
|
119
|
+
// END ./lib/conventions.js
|
|
120
|
+
|
|
121
|
+
// START ./lib/dom.js
|
|
122
|
+
/**
|
|
123
|
+
* The error class for errors reported by the DOM API.
|
|
124
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMException
|
|
125
|
+
* @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/ecma-script-binding.html
|
|
126
|
+
* @see http://www.w3.org/TR/REC-DOM-Level-1/ecma-script-language-binding.html
|
|
127
|
+
*/
|
|
128
|
+
class DOMException extends Error {
|
|
129
|
+
constructor(code: number, message: string);
|
|
15
130
|
}
|
|
16
131
|
|
|
132
|
+
interface DOMImplementation {
|
|
133
|
+
/**
|
|
134
|
+
* The DOMImplementation interface represents an object providing methods
|
|
135
|
+
* which are not dependent on any particular document.
|
|
136
|
+
* Such an object is returned by the `Document.implementation` property.
|
|
137
|
+
*
|
|
138
|
+
* __The individual methods describe the differences compared to the specs.__
|
|
139
|
+
*
|
|
140
|
+
* @constructor
|
|
141
|
+
*
|
|
142
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation MDN
|
|
143
|
+
* @see https://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#ID-102161490 DOM Level 1 Core
|
|
144
|
+
* (Initial)
|
|
145
|
+
* @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-102161490 DOM Level 2 Core
|
|
146
|
+
* @see https://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-102161490 DOM Level 3 Core
|
|
147
|
+
* @see https://dom.spec.whatwg.org/#domimplementation DOM Living Standard
|
|
148
|
+
*/
|
|
149
|
+
new (): DOMImplementation;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Creates an XML Document object of the specified type with its document element.
|
|
153
|
+
*
|
|
154
|
+
* __It behaves slightly different from the description in the living standard__:
|
|
155
|
+
* - There is no interface/class `XMLDocument`, it returns a `Document` instance (with it's
|
|
156
|
+
* `type` set to `'xml'`).
|
|
157
|
+
* - `encoding`, `mode`, `origin`, `url` fields are currently not declared.
|
|
158
|
+
*
|
|
159
|
+
* @returns {Document} the XML document
|
|
160
|
+
*
|
|
161
|
+
* @see DOMImplementation.createHTMLDocument
|
|
162
|
+
*
|
|
163
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createDocument MDN
|
|
164
|
+
* @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#Level-2-Core-DOM-createDocument DOM
|
|
165
|
+
* Level 2 Core (initial)
|
|
166
|
+
* @see https://dom.spec.whatwg.org/#dom-domimplementation-createdocument DOM Level 2 Core
|
|
167
|
+
*/
|
|
168
|
+
createDocument(
|
|
169
|
+
namespaceURI: string | null,
|
|
170
|
+
qualifiedName: string,
|
|
171
|
+
doctype?: DocumentType | null
|
|
172
|
+
): Document;
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Returns a doctype, with the given `qualifiedName`, `publicId`, and `systemId`.
|
|
176
|
+
*
|
|
177
|
+
* __This behavior is slightly different from the in the specs__:
|
|
178
|
+
* - `encoding`, `mode`, `origin`, `url` fields are currently not declared.
|
|
179
|
+
*
|
|
180
|
+
* @returns {DocumentType} which can either be used with `DOMImplementation.createDocument`
|
|
181
|
+
* upon document creation or can be put into the document via methods like
|
|
182
|
+
* `Node.insertBefore()` or `Node.replaceChild()`
|
|
183
|
+
*
|
|
184
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createDocumentType
|
|
185
|
+
* MDN
|
|
186
|
+
* @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#Level-2-Core-DOM-createDocType DOM
|
|
187
|
+
* Level 2 Core
|
|
188
|
+
* @see https://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype DOM Living
|
|
189
|
+
* Standard
|
|
190
|
+
*/
|
|
191
|
+
createDocumentType(
|
|
192
|
+
qualifiedName: string,
|
|
193
|
+
publicId?: string,
|
|
194
|
+
systemId?: string
|
|
195
|
+
): DocumentType;
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Returns an HTML document, that might already have a basic DOM structure.
|
|
199
|
+
*
|
|
200
|
+
* __It behaves slightly different from the description in the living standard__:
|
|
201
|
+
* - If the first argument is `false` no initial nodes are added (steps 3-7 in the specs are
|
|
202
|
+
* omitted)
|
|
203
|
+
* - `encoding`, `mode`, `origin`, `url` fields are currently not declared.
|
|
204
|
+
*
|
|
205
|
+
* @see DOMImplementation.createDocument
|
|
206
|
+
*
|
|
207
|
+
* @see https://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument
|
|
208
|
+
* @see https://dom.spec.whatwg.org/#html-document
|
|
209
|
+
*/
|
|
210
|
+
createHTMLDocument(title?: string | false): Document;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* The DOMImplementation.hasFeature() method returns a Boolean flag indicating if a given
|
|
214
|
+
* feature is supported. The different implementations fairly diverged in what kind of features
|
|
215
|
+
* were reported. The latest version of the spec settled to force this method to always return
|
|
216
|
+
* true, where the functionality was accurate and in use.
|
|
217
|
+
*
|
|
218
|
+
* @deprecated It is deprecated and modern browsers return true in all cases.
|
|
219
|
+
*
|
|
220
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/hasFeature MDN
|
|
221
|
+
* @see https://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#ID-5CED94D7 DOM Level 1 Core
|
|
222
|
+
* @see https://dom.spec.whatwg.org/#dom-domimplementation-hasfeature DOM Living Standard
|
|
223
|
+
*/
|
|
224
|
+
hasFeature(feature: string, version?: string): true;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
var XMLSerializer: XMLSerializerStatic;
|
|
17
228
|
interface XMLSerializerStatic {
|
|
18
|
-
new (): XMLSerializer
|
|
229
|
+
new (): XMLSerializer;
|
|
19
230
|
}
|
|
231
|
+
// END ./lib/dom.js
|
|
20
232
|
|
|
233
|
+
// START ./lib/dom-parser.js
|
|
234
|
+
var DOMParser: DOMParserStatic;
|
|
235
|
+
interface DOMParserStatic {
|
|
236
|
+
/**
|
|
237
|
+
* The DOMParser interface provides the ability to parse XML or HTML source code
|
|
238
|
+
* from a string into a DOM `Document`.
|
|
239
|
+
*
|
|
240
|
+
* _xmldom is different from the spec in that it allows an `options` parameter,
|
|
241
|
+
* to control the behavior._
|
|
242
|
+
*
|
|
243
|
+
* @param {DOMParserOptions} [options]
|
|
244
|
+
* @constructor
|
|
245
|
+
*
|
|
246
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser
|
|
247
|
+
* @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-parsing-and-serialization
|
|
248
|
+
*/
|
|
249
|
+
new (options?: DOMParserOptions): DOMParser;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* The DOMParser interface provides the ability to parse XML or HTML source code
|
|
254
|
+
* from a string into a DOM `Document`.
|
|
255
|
+
*
|
|
256
|
+
* _xmldom is different from the spec in that it allows an `options` parameter,
|
|
257
|
+
* to control the behavior._
|
|
258
|
+
*
|
|
259
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser
|
|
260
|
+
* @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-parsing-and-serialization
|
|
261
|
+
*/
|
|
21
262
|
interface DOMParser {
|
|
22
|
-
|
|
263
|
+
/**
|
|
264
|
+
* Parses `source` using the options in the way configured by the `DOMParserOptions` of `this`
|
|
265
|
+
* `DOMParser`. If `mimeType` is `text/html` an HTML `Document` is created, otherwise an XML
|
|
266
|
+
* `Document` is created.
|
|
267
|
+
*
|
|
268
|
+
* __It behaves different from the description in the living standard__:
|
|
269
|
+
* - Uses the `options` passed to the `DOMParser` constructor to modify the
|
|
270
|
+
* behavior.
|
|
271
|
+
* - Any unexpected input is reported to `onError` with either a `warning`, `error` or `fatalError` level.
|
|
272
|
+
* - Any `fatalError` throws a `ParseError` which prevents further processing.
|
|
273
|
+
* - Any error thrown by `onError` is converted to a `ParseError` which prevents further processing
|
|
274
|
+
* - If no `Document` was created during parsing it is reported as a `fatalError`.
|
|
275
|
+
*
|
|
276
|
+
* @throws ParseError for any `fatalError` or anything that is thrown by `onError`
|
|
277
|
+
* @throws TypeError for any invalid `mimeType`
|
|
278
|
+
* @returns the `Document` node
|
|
279
|
+
*
|
|
280
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString
|
|
281
|
+
* @see https://html.spec.whatwg.org/#dom-domparser-parsefromstring-dev
|
|
282
|
+
*/
|
|
283
|
+
parseFromString(source: string, mimeType: MIME_TYPE = MIME_TYPE.XML_TEXT): Document;
|
|
23
284
|
}
|
|
24
285
|
|
|
25
286
|
interface XMLSerializer {
|
|
26
|
-
serializeToString(node: Node): string
|
|
287
|
+
serializeToString(node: Node, nodeFilter?: (node: Node) => boolean): string;
|
|
27
288
|
}
|
|
28
289
|
|
|
29
290
|
interface DOMParserOptions {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
291
|
+
/**
|
|
292
|
+
* The method to use instead of `Object.assign` (defaults to `conventions.assign`),
|
|
293
|
+
* which is used to copy values from the options before they are used for parsing.
|
|
294
|
+
*
|
|
295
|
+
* @private
|
|
296
|
+
* @see conventions.assign
|
|
297
|
+
*/
|
|
298
|
+
readonly assign?: typeof Object.assign;
|
|
299
|
+
/**
|
|
300
|
+
* For internal testing: The class for creating an instance for handling events from the SAX
|
|
301
|
+
* parser.
|
|
302
|
+
* __**Warning: By configuring a faulty implementation,
|
|
303
|
+
* the specified behavior can completely be broken.**__
|
|
304
|
+
*
|
|
305
|
+
* @private
|
|
306
|
+
*/
|
|
307
|
+
readonly domHandler?: unknown;
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* DEPRECATED: Use `onError` instead!
|
|
311
|
+
*
|
|
312
|
+
* For backwards compatibility:
|
|
313
|
+
* If it is a function, it will be used as a value for `onError`,
|
|
314
|
+
* but it receives different argument types than before 0.9.0.
|
|
315
|
+
* @throws If it is an object.
|
|
316
|
+
* @deprecated
|
|
317
|
+
*/
|
|
318
|
+
readonly errorHandler?: ErrorHandlerFunction;
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Configures if the nodes created during parsing
|
|
322
|
+
* will have a `lineNumber` and a `columnNumber` attribute
|
|
323
|
+
* describing their location in the XML string.
|
|
324
|
+
* Default is true.
|
|
325
|
+
*/
|
|
326
|
+
readonly locator?: boolean;
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* used to replace line endings before parsing, defaults to `normalizeLineEndings`,
|
|
330
|
+
* which normalizes line endings according to <https://www.w3.org/TR/xml11/#sec-line-ends>.
|
|
331
|
+
*/
|
|
332
|
+
readonly normalizeLineEndings?: (source: string) => string;
|
|
333
|
+
/**
|
|
334
|
+
* A function that is invoked for every error that occurs during parsing.
|
|
335
|
+
*
|
|
336
|
+
* If it is not provided, all errors are reported to `console.error`
|
|
337
|
+
* and only `fatalError`s are thrown as a `ParseError`,
|
|
338
|
+
* which prevents any further processing.
|
|
339
|
+
* If the provided method throws, a `ParserError` is thrown,
|
|
340
|
+
* which prevents any further processing.
|
|
341
|
+
*
|
|
342
|
+
* Be aware that many `warning`s are considered an error
|
|
343
|
+
* that prevents further processing in most implementations.
|
|
344
|
+
*
|
|
345
|
+
* @param level the error level as reported by the SAXParser
|
|
346
|
+
* @param message the error message
|
|
347
|
+
* @param context the DOMHandler instance used for parsing
|
|
348
|
+
*
|
|
349
|
+
* @see onErrorStopParsing
|
|
350
|
+
* @see onWarningStopParsing
|
|
351
|
+
*/
|
|
352
|
+
readonly onError?: ErrorHandlerFunction;
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* The XML namespaces that should be assumed when parsing.
|
|
356
|
+
* The default namespace can be provided by the key that is the empty string.
|
|
357
|
+
* When the `mimeType` for HTML, XHTML or SVG are passed to `parseFromString`,
|
|
358
|
+
* the default namespace that will be used,
|
|
359
|
+
* will be overridden according to the specification.
|
|
360
|
+
*/
|
|
361
|
+
readonly xmlns?: Readonly<Record<string, string | null | undefined>>;
|
|
34
362
|
}
|
|
35
363
|
|
|
36
364
|
interface ErrorHandlerFunction {
|
|
37
|
-
(level: 'warn' | 'error' | 'fatalError', msg: string): void
|
|
365
|
+
(level: 'warn' | 'error' | 'fatalError', msg: string, context: any): void;
|
|
38
366
|
}
|
|
39
367
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
368
|
+
/**
|
|
369
|
+
* A method that prevents any further parsing when an `error`
|
|
370
|
+
* with level `error` is reported during parsing.
|
|
371
|
+
*
|
|
372
|
+
* @see DOMParserOptions.onError
|
|
373
|
+
* @see onWarningStopParsing
|
|
374
|
+
*/
|
|
375
|
+
function onErrorStopParsing(): void | never;
|
|
376
|
+
/**
|
|
377
|
+
* A method that prevents any further parsing when an `error`
|
|
378
|
+
* with any level is reported during parsing.
|
|
379
|
+
*
|
|
380
|
+
* @see DOMParserOptions.onError
|
|
381
|
+
* @see onErrorStopParsing
|
|
382
|
+
*/
|
|
383
|
+
function onWarningStopParsing(): never;
|
|
384
|
+
// END ./lib/dom-parser.js
|
|
45
385
|
}
|
package/lib/conventions.js
CHANGED
|
@@ -55,8 +55,8 @@ function freeze(object, oc) {
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
|
-
* Since
|
|
59
|
-
* that is sufficient for
|
|
58
|
+
* Since xmldom can not rely on `Object.assign`,
|
|
59
|
+
* it uses/provides a simplified version that is sufficient for its needs.
|
|
60
60
|
*
|
|
61
61
|
* @param {Object} target
|
|
62
62
|
* @param {Object | null | undefined} source
|
|
@@ -227,6 +227,34 @@ function isHTMLEscapableRawTextElement(tagName) {
|
|
|
227
227
|
var key = tagName.toLowerCase();
|
|
228
228
|
return HTML_RAW_TEXT_ELEMENTS.hasOwnProperty(key) && HTML_RAW_TEXT_ELEMENTS[key];
|
|
229
229
|
}
|
|
230
|
+
/**
|
|
231
|
+
* Only returns true if `value` matches MIME_TYPE.HTML, which indicates an HTML document.
|
|
232
|
+
*
|
|
233
|
+
* @param {string} mimeType
|
|
234
|
+
* @returns {mimeType is 'text/html'}
|
|
235
|
+
*
|
|
236
|
+
* @see https://www.iana.org/assignments/media-types/text/html
|
|
237
|
+
* @see https://en.wikipedia.org/wiki/HTML
|
|
238
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString
|
|
239
|
+
* @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring
|
|
240
|
+
*/
|
|
241
|
+
function isHTMLMimeType(mimeType) {
|
|
242
|
+
return mimeType === MIME_TYPE.HTML;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* For both the `text/html` and the `application/xhtml+xml` namespace
|
|
246
|
+
* the spec defines that the HTML namespace is provided as the default.
|
|
247
|
+
*
|
|
248
|
+
* @param {string} mimeType
|
|
249
|
+
* @returns {boolean}
|
|
250
|
+
*
|
|
251
|
+
* @see https://dom.spec.whatwg.org/#dom-document-createelement
|
|
252
|
+
* @see https://dom.spec.whatwg.org/#dom-domimplementation-createdocument
|
|
253
|
+
* @see https://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument
|
|
254
|
+
*/
|
|
255
|
+
function hasDefaultHTMLNamespace(mimeType) {
|
|
256
|
+
return isHTMLMimeType(mimeType) || mimeType === MIME_TYPE.XML_XHTML_APPLICATION;
|
|
257
|
+
}
|
|
230
258
|
|
|
231
259
|
/**
|
|
232
260
|
* All mime types that are allowed as input to `DOMParser.parseFromString`
|
|
@@ -239,7 +267,6 @@ var MIME_TYPE = freeze({
|
|
|
239
267
|
/**
|
|
240
268
|
* `text/html`, the only mime type that triggers treating an XML document as HTML.
|
|
241
269
|
*
|
|
242
|
-
* @see DOMParser.SupportedType.isHTML
|
|
243
270
|
* @see https://www.iana.org/assignments/media-types/text/html IANA MimeType registration
|
|
244
271
|
* @see https://en.wikipedia.org/wiki/HTML Wikipedia
|
|
245
272
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString MDN
|
|
@@ -247,36 +274,6 @@ var MIME_TYPE = freeze({
|
|
|
247
274
|
*/
|
|
248
275
|
HTML: 'text/html',
|
|
249
276
|
|
|
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
277
|
/**
|
|
281
278
|
* `application/xml`, the standard mime type for XML documents.
|
|
282
279
|
*
|
|
@@ -314,7 +311,25 @@ var MIME_TYPE = freeze({
|
|
|
314
311
|
*/
|
|
315
312
|
XML_SVG_IMAGE: 'image/svg+xml',
|
|
316
313
|
});
|
|
314
|
+
/**
|
|
315
|
+
* @typedef {'application/xhtml+xml' | 'application/xml' | 'image/svg+xml' | 'text/html' | 'text/xml'} MimeType
|
|
316
|
+
*/
|
|
317
|
+
/**
|
|
318
|
+
* @type {MimeType[]}
|
|
319
|
+
* @private
|
|
320
|
+
*/
|
|
321
|
+
var _MIME_TYPES = Object.keys(MIME_TYPE).map(function (key) {
|
|
322
|
+
return MIME_TYPE[key];
|
|
323
|
+
});
|
|
317
324
|
|
|
325
|
+
/**
|
|
326
|
+
* Only returns true if `mimeType` is one of the allowed values for `DOMParser.parseFromString`.
|
|
327
|
+
* @param {string} mimeType
|
|
328
|
+
* @returns {mimeType is 'application/xhtml+xml' | 'application/xml' | 'image/svg+xml' | 'text/html' | 'text/xml'}
|
|
329
|
+
*/
|
|
330
|
+
function isValidMimeType(mimeType) {
|
|
331
|
+
return _MIME_TYPES.indexOf(mimeType) > -1;
|
|
332
|
+
}
|
|
318
333
|
/**
|
|
319
334
|
* Namespaces that are used in this code base.
|
|
320
335
|
*
|
|
@@ -328,17 +343,6 @@ var NAMESPACE = freeze({
|
|
|
328
343
|
*/
|
|
329
344
|
HTML: 'http://www.w3.org/1999/xhtml',
|
|
330
345
|
|
|
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
346
|
/**
|
|
343
347
|
* The SVG namespace.
|
|
344
348
|
*
|
|
@@ -361,22 +365,20 @@ var NAMESPACE = freeze({
|
|
|
361
365
|
XMLNS: 'http://www.w3.org/2000/xmlns/',
|
|
362
366
|
});
|
|
363
367
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
// https://www.w3.org/TR/xml-names/#ns-qualnames
|
|
379
|
-
var QNAME = new RegExp('^' + NCNAME + '(?::' + NCNAME + ')?$');
|
|
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;
|
|
380
382
|
|
|
381
383
|
exports.assign = assign;
|
|
382
384
|
exports.find = find;
|
|
@@ -384,14 +386,13 @@ exports.freeze = freeze;
|
|
|
384
386
|
exports.HTML_BOOLEAN_ATTRIBUTES = HTML_BOOLEAN_ATTRIBUTES;
|
|
385
387
|
exports.HTML_RAW_TEXT_ELEMENTS = HTML_RAW_TEXT_ELEMENTS;
|
|
386
388
|
exports.HTML_VOID_ELEMENTS = HTML_VOID_ELEMENTS;
|
|
389
|
+
exports.hasDefaultHTMLNamespace = hasDefaultHTMLNamespace;
|
|
387
390
|
exports.isHTMLBooleanAttribute = isHTMLBooleanAttribute;
|
|
388
391
|
exports.isHTMLRawTextElement = isHTMLRawTextElement;
|
|
389
392
|
exports.isHTMLEscapableRawTextElement = isHTMLEscapableRawTextElement;
|
|
393
|
+
exports.isHTMLMimeType = isHTMLMimeType;
|
|
390
394
|
exports.isHTMLVoidElement = isHTMLVoidElement;
|
|
395
|
+
exports.isValidMimeType = isValidMimeType;
|
|
391
396
|
exports.MIME_TYPE = MIME_TYPE;
|
|
392
|
-
exports.NAME_CHAR = NAME_CHAR;
|
|
393
|
-
exports.NAME_START_CHAR = NAME_START_CHAR;
|
|
394
397
|
exports.NAMESPACE = NAMESPACE;
|
|
395
|
-
exports.
|
|
396
|
-
exports.QNAME_CHAR = QNAME_CHAR;
|
|
397
|
-
exports.QNAME_START_CHAR = QNAME_START_CHAR;
|
|
398
|
+
exports.ParseError = ParseError;
|