@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.
- package/CHANGELOG.md +114 -1
- package/index.d.ts +369 -21
- package/lib/.eslintrc.yml +1 -1
- package/lib/conventions.js +153 -121
- package/lib/dom-parser.js +202 -132
- package/lib/dom.js +1085 -414
- package/lib/entities.js +14 -9
- package/lib/errors.js +206 -0
- package/lib/grammar.js +528 -0
- package/lib/index.js +33 -1
- package/lib/sax.js +395 -173
- package/package.json +73 -71
- package/readme.md +41 -44
package/lib/entities.js
CHANGED
|
@@ -7,7 +7,8 @@ var freeze = require('./conventions').freeze;
|
|
|
7
7
|
*
|
|
8
8
|
* @see https://www.w3.org/TR/2006/REC-xml11-20060816/#sec-predefined-ent W3C XML 1.1
|
|
9
9
|
* @see https://www.w3.org/TR/2008/REC-xml-20081126/#sec-predefined-ent W3C XML 1.0
|
|
10
|
-
* @see https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Predefined_entities_in_XML
|
|
10
|
+
* @see https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Predefined_entities_in_XML
|
|
11
|
+
* Wikipedia
|
|
11
12
|
*/
|
|
12
13
|
exports.XML_ENTITIES = freeze({
|
|
13
14
|
amp: '&',
|
|
@@ -21,15 +22,18 @@ exports.XML_ENTITIES = freeze({
|
|
|
21
22
|
* A map of all entities that are detected in an HTML document.
|
|
22
23
|
* They contain all entries from `XML_ENTITIES`.
|
|
23
24
|
*
|
|
24
|
-
* @see XML_ENTITIES
|
|
25
|
-
* @see DOMParser.parseFromString
|
|
26
|
-
* @see DOMImplementation.prototype.createHTMLDocument
|
|
27
|
-
* @see https://html.spec.whatwg.org/#named-character-references WHATWG HTML(5)
|
|
25
|
+
* @see {@link XML_ENTITIES}
|
|
26
|
+
* @see {@link DOMParser.parseFromString}
|
|
27
|
+
* @see {@link DOMImplementation.prototype.createHTMLDocument}
|
|
28
|
+
* @see https://html.spec.whatwg.org/#named-character-references WHATWG HTML(5)
|
|
29
|
+
* Spec
|
|
28
30
|
* @see https://html.spec.whatwg.org/entities.json JSON
|
|
29
31
|
* @see https://www.w3.org/TR/xml-entity-names/ W3C XML Entity Names
|
|
30
32
|
* @see https://www.w3.org/TR/html4/sgml/entities.html W3C HTML4/SGML
|
|
31
|
-
* @see https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Character_entity_references_in_HTML
|
|
32
|
-
*
|
|
33
|
+
* @see https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Character_entity_references_in_HTML
|
|
34
|
+
* Wikipedia (HTML)
|
|
35
|
+
* @see https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Entities_representing_special_characters_in_XHTML
|
|
36
|
+
* Wikpedia (XHTML)
|
|
33
37
|
*/
|
|
34
38
|
exports.HTML_ENTITIES = freeze({
|
|
35
39
|
Aacute: '\u00C1',
|
|
@@ -2160,7 +2164,8 @@ exports.HTML_ENTITIES = freeze({
|
|
|
2160
2164
|
});
|
|
2161
2165
|
|
|
2162
2166
|
/**
|
|
2163
|
-
* @deprecated
|
|
2164
|
-
*
|
|
2167
|
+
* @deprecated
|
|
2168
|
+
* Use `HTML_ENTITIES` instead.
|
|
2169
|
+
* @see {@link HTML_ENTITIES}
|
|
2165
2170
|
*/
|
|
2166
2171
|
exports.entityMap = exports.HTML_ENTITIES;
|
package/lib/errors.js
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var conventions = require('./conventions');
|
|
4
|
+
|
|
5
|
+
function extendError(constructor, writableName) {
|
|
6
|
+
constructor.prototype = Object.create(Error.prototype, {
|
|
7
|
+
constructor: { value: constructor },
|
|
8
|
+
name: { value: constructor.name, enumerable: true, writable: writableName },
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
var DOMExceptionName = conventions.freeze({
|
|
13
|
+
/**
|
|
14
|
+
* the default value as defined by the spec
|
|
15
|
+
*/
|
|
16
|
+
Error: 'Error',
|
|
17
|
+
/**
|
|
18
|
+
* @deprecated
|
|
19
|
+
* Use RangeError instead.
|
|
20
|
+
*/
|
|
21
|
+
IndexSizeError: 'IndexSizeError',
|
|
22
|
+
/**
|
|
23
|
+
* @deprecated
|
|
24
|
+
* Just to match the related static code, not part of the spec.
|
|
25
|
+
*/
|
|
26
|
+
DomstringSizeError: 'DomstringSizeError',
|
|
27
|
+
HierarchyRequestError: 'HierarchyRequestError',
|
|
28
|
+
WrongDocumentError: 'WrongDocumentError',
|
|
29
|
+
InvalidCharacterError: 'InvalidCharacterError',
|
|
30
|
+
/**
|
|
31
|
+
* @deprecated
|
|
32
|
+
* Just to match the related static code, not part of the spec.
|
|
33
|
+
*/
|
|
34
|
+
NoDataAllowedError: 'NoDataAllowedError',
|
|
35
|
+
NoModificationAllowedError: 'NoModificationAllowedError',
|
|
36
|
+
NotFoundError: 'NotFoundError',
|
|
37
|
+
NotSupportedError: 'NotSupportedError',
|
|
38
|
+
InUseAttributeError: 'InUseAttributeError',
|
|
39
|
+
InvalidStateError: 'InvalidStateError',
|
|
40
|
+
SyntaxError: 'SyntaxError',
|
|
41
|
+
InvalidModificationError: 'InvalidModificationError',
|
|
42
|
+
NamespaceError: 'NamespaceError',
|
|
43
|
+
/**
|
|
44
|
+
* @deprecated
|
|
45
|
+
* Use TypeError for invalid arguments,
|
|
46
|
+
* "NotSupportedError" DOMException for unsupported operations,
|
|
47
|
+
* and "NotAllowedError" DOMException for denied requests instead.
|
|
48
|
+
*/
|
|
49
|
+
InvalidAccessError: 'InvalidAccessError',
|
|
50
|
+
/**
|
|
51
|
+
* @deprecated
|
|
52
|
+
* Just to match the related static code, not part of the spec.
|
|
53
|
+
*/
|
|
54
|
+
ValidationError: 'ValidationError',
|
|
55
|
+
/**
|
|
56
|
+
* @deprecated
|
|
57
|
+
* Use TypeError instead.
|
|
58
|
+
*/
|
|
59
|
+
TypeMismatchError: 'TypeMismatchError',
|
|
60
|
+
SecurityError: 'SecurityError',
|
|
61
|
+
NetworkError: 'NetworkError',
|
|
62
|
+
AbortError: 'AbortError',
|
|
63
|
+
/**
|
|
64
|
+
* @deprecated
|
|
65
|
+
* Just to match the related static code, not part of the spec.
|
|
66
|
+
*/
|
|
67
|
+
URLMismatchError: 'URLMismatchError',
|
|
68
|
+
QuotaExceededError: 'QuotaExceededError',
|
|
69
|
+
TimeoutError: 'TimeoutError',
|
|
70
|
+
InvalidNodeTypeError: 'InvalidNodeTypeError',
|
|
71
|
+
DataCloneError: 'DataCloneError',
|
|
72
|
+
EncodingError: 'EncodingError',
|
|
73
|
+
NotReadableError: 'NotReadableError',
|
|
74
|
+
UnknownError: 'UnknownError',
|
|
75
|
+
ConstraintError: 'ConstraintError',
|
|
76
|
+
DataError: 'DataError',
|
|
77
|
+
TransactionInactiveError: 'TransactionInactiveError',
|
|
78
|
+
ReadOnlyError: 'ReadOnlyError',
|
|
79
|
+
VersionError: 'VersionError',
|
|
80
|
+
OperationError: 'OperationError',
|
|
81
|
+
NotAllowedError: 'NotAllowedError',
|
|
82
|
+
OptOutError: 'OptOutError',
|
|
83
|
+
});
|
|
84
|
+
var DOMExceptionNames = Object.keys(DOMExceptionName);
|
|
85
|
+
|
|
86
|
+
function isValidDomExceptionCode(value) {
|
|
87
|
+
return typeof value === 'number' && value >= 1 && value <= 25;
|
|
88
|
+
}
|
|
89
|
+
function endsWithError(value) {
|
|
90
|
+
return typeof value === 'string' && value.substring(value.length - DOMExceptionName.Error.length) === DOMExceptionName.Error;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* DOM operations only raise exceptions in "exceptional" circumstances, i.e., when an operation
|
|
94
|
+
* is impossible to perform (either for logical reasons, because data is lost, or because the
|
|
95
|
+
* implementation has become unstable). In general, DOM methods return specific error values in
|
|
96
|
+
* ordinary processing situations, such as out-of-bound errors when using NodeList.
|
|
97
|
+
*
|
|
98
|
+
* Implementations should raise other exceptions under other circumstances. For example,
|
|
99
|
+
* implementations should raise an implementation-dependent exception if a null argument is
|
|
100
|
+
* passed when null was not expected.
|
|
101
|
+
*
|
|
102
|
+
* This implementation supports the following usages:
|
|
103
|
+
* 1. according to the living standard (both arguments are mandatory):
|
|
104
|
+
* ```
|
|
105
|
+
* new DOMException("message (can be empty)", DOMExceptionNames.HierarchyRequestError)
|
|
106
|
+
* ```
|
|
107
|
+
* 2. according to previous xmldom implementation (only the first argument is required):
|
|
108
|
+
* ```
|
|
109
|
+
* new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "optional message")
|
|
110
|
+
* ```
|
|
111
|
+
* both result in the proper name being set.
|
|
112
|
+
*
|
|
113
|
+
* @class DOMException
|
|
114
|
+
* @param {number | string} messageOrCode
|
|
115
|
+
* The reason why an operation is not acceptable.
|
|
116
|
+
* If it is a number, it is used to determine the `name`, see
|
|
117
|
+
* {@link https://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-258A00AF ExceptionCode}
|
|
118
|
+
* @param {string | keyof typeof DOMExceptionName | Error} [nameOrMessage]
|
|
119
|
+
* The `name` to use for the error.
|
|
120
|
+
* If `messageOrCode` is a number, this arguments is used as the `message` instead.
|
|
121
|
+
* @augments Error
|
|
122
|
+
* @see https://webidl.spec.whatwg.org/#idl-DOMException
|
|
123
|
+
* @see https://webidl.spec.whatwg.org/#dfn-error-names-table
|
|
124
|
+
* @see https://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-17189187
|
|
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
|
+
function DOMException(messageOrCode, nameOrMessage) {
|
|
129
|
+
// support old way of passing arguments: first argument is a valid number
|
|
130
|
+
if (isValidDomExceptionCode(messageOrCode)) {
|
|
131
|
+
this.name = DOMExceptionNames[messageOrCode];
|
|
132
|
+
this.message = nameOrMessage || '';
|
|
133
|
+
} else {
|
|
134
|
+
this.message = messageOrCode;
|
|
135
|
+
this.name = endsWithError(nameOrMessage) ? nameOrMessage : DOMExceptionName.Error;
|
|
136
|
+
}
|
|
137
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, DOMException);
|
|
138
|
+
}
|
|
139
|
+
extendError(DOMException, true);
|
|
140
|
+
Object.defineProperties(DOMException.prototype, {
|
|
141
|
+
code: {
|
|
142
|
+
enumerable: true,
|
|
143
|
+
get: function () {
|
|
144
|
+
var code = DOMExceptionNames.indexOf(this.name);
|
|
145
|
+
if (isValidDomExceptionCode(code)) return code;
|
|
146
|
+
return 0;
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
var ExceptionCode = {
|
|
152
|
+
INDEX_SIZE_ERR: 1,
|
|
153
|
+
DOMSTRING_SIZE_ERR: 2,
|
|
154
|
+
HIERARCHY_REQUEST_ERR: 3,
|
|
155
|
+
WRONG_DOCUMENT_ERR: 4,
|
|
156
|
+
INVALID_CHARACTER_ERR: 5,
|
|
157
|
+
NO_DATA_ALLOWED_ERR: 6,
|
|
158
|
+
NO_MODIFICATION_ALLOWED_ERR: 7,
|
|
159
|
+
NOT_FOUND_ERR: 8,
|
|
160
|
+
NOT_SUPPORTED_ERR: 9,
|
|
161
|
+
INUSE_ATTRIBUTE_ERR: 10,
|
|
162
|
+
INVALID_STATE_ERR: 11,
|
|
163
|
+
SYNTAX_ERR: 12,
|
|
164
|
+
INVALID_MODIFICATION_ERR: 13,
|
|
165
|
+
NAMESPACE_ERR: 14,
|
|
166
|
+
INVALID_ACCESS_ERR: 15,
|
|
167
|
+
VALIDATION_ERR: 16,
|
|
168
|
+
TYPE_MISMATCH_ERR: 17,
|
|
169
|
+
SECURITY_ERR: 18,
|
|
170
|
+
NETWORK_ERR: 19,
|
|
171
|
+
ABORT_ERR: 20,
|
|
172
|
+
URL_MISMATCH_ERR: 21,
|
|
173
|
+
QUOTA_EXCEEDED_ERR: 22,
|
|
174
|
+
TIMEOUT_ERR: 23,
|
|
175
|
+
INVALID_NODE_TYPE_ERR: 24,
|
|
176
|
+
DATA_CLONE_ERR: 25,
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
var entries = Object.entries(ExceptionCode);
|
|
180
|
+
for (var i = 0; i < entries.length; i++) {
|
|
181
|
+
var key = entries[i][0];
|
|
182
|
+
var value = entries[i][1];
|
|
183
|
+
DOMException[key] = value;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Creates an error that will not be caught by XMLReader aka the SAX parser.
|
|
188
|
+
*
|
|
189
|
+
* @class
|
|
190
|
+
* @param {string} message
|
|
191
|
+
* @param {any} [locator]
|
|
192
|
+
* @param {Error} [cause]
|
|
193
|
+
* Optional, can provide details about the location in the source.
|
|
194
|
+
*/
|
|
195
|
+
function ParseError(message, locator, cause) {
|
|
196
|
+
this.message = message;
|
|
197
|
+
this.locator = locator;
|
|
198
|
+
this.cause = cause;
|
|
199
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, ParseError);
|
|
200
|
+
}
|
|
201
|
+
extendError(ParseError);
|
|
202
|
+
|
|
203
|
+
exports.DOMException = DOMException;
|
|
204
|
+
exports.DOMExceptionName = DOMExceptionName;
|
|
205
|
+
exports.ParseError = ParseError;
|
|
206
|
+
exports.ExceptionCode = ExceptionCode;
|