@rgrove/parse-xml 3.0.0 → 4.0.1
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/LICENSE +1 -1
- package/README.md +72 -97
- package/dist/browser.js +774 -0
- package/dist/browser.js.map +7 -0
- package/dist/global.min.js +10 -0
- package/dist/global.min.js.map +7 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +50 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/Parser.d.ts +218 -0
- package/dist/lib/Parser.d.ts.map +1 -0
- package/dist/lib/Parser.js +638 -0
- package/dist/lib/Parser.js.map +1 -0
- package/dist/lib/StringScanner.d.ts +97 -0
- package/dist/lib/StringScanner.d.ts.map +1 -0
- package/dist/lib/StringScanner.js +210 -0
- package/dist/lib/StringScanner.js.map +1 -0
- package/dist/lib/XmlCdata.d.ts +8 -0
- package/dist/lib/XmlCdata.d.ts.map +1 -0
- package/dist/lib/XmlCdata.js +15 -0
- package/dist/lib/XmlCdata.js.map +1 -0
- package/dist/lib/XmlComment.d.ts +16 -0
- package/dist/lib/XmlComment.d.ts.map +1 -0
- package/dist/lib/XmlComment.js +23 -0
- package/dist/lib/XmlComment.js.map +1 -0
- package/dist/lib/XmlDocument.d.ts +29 -0
- package/dist/lib/XmlDocument.d.ts.map +1 -0
- package/dist/lib/XmlDocument.js +47 -0
- package/dist/lib/XmlDocument.js.map +1 -0
- package/dist/lib/XmlElement.d.ts +40 -0
- package/dist/lib/XmlElement.d.ts.map +1 -0
- package/dist/lib/XmlElement.js +51 -0
- package/dist/lib/XmlElement.js.map +1 -0
- package/dist/lib/XmlNode.d.ts +74 -0
- package/dist/lib/XmlNode.d.ts.map +1 -0
- package/dist/lib/XmlNode.js +96 -0
- package/dist/lib/XmlNode.js.map +1 -0
- package/dist/lib/XmlProcessingInstruction.d.ts +22 -0
- package/dist/lib/XmlProcessingInstruction.d.ts.map +1 -0
- package/dist/lib/XmlProcessingInstruction.js +25 -0
- package/dist/lib/XmlProcessingInstruction.js.map +1 -0
- package/dist/lib/XmlText.d.ts +16 -0
- package/dist/lib/XmlText.d.ts.map +1 -0
- package/dist/lib/XmlText.js +23 -0
- package/dist/lib/XmlText.js.map +1 -0
- package/dist/lib/syntax.d.ts +69 -0
- package/dist/lib/syntax.d.ts.map +1 -0
- package/dist/lib/syntax.js +133 -0
- package/dist/lib/syntax.js.map +1 -0
- package/dist/lib/types.d.ts +5 -0
- package/dist/lib/types.d.ts.map +1 -0
- package/dist/lib/types.js +3 -0
- package/dist/lib/types.js.map +1 -0
- package/package.json +33 -26
- package/src/index.ts +30 -0
- package/src/lib/Parser.ts +819 -0
- package/src/lib/StringScanner.ts +254 -0
- package/src/lib/XmlCdata.ts +11 -0
- package/src/lib/XmlComment.ts +26 -0
- package/src/lib/XmlDocument.ts +57 -0
- package/src/lib/XmlElement.ts +81 -0
- package/src/lib/XmlNode.ts +107 -0
- package/src/lib/XmlProcessingInstruction.ts +35 -0
- package/src/lib/XmlText.ts +26 -0
- package/src/lib/syntax.ts +136 -0
- package/src/lib/types.ts +2 -0
- package/CHANGELOG.md +0 -162
- package/dist/types/index.d.ts +0 -68
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/lib/Parser.d.ts +0 -234
- package/dist/types/lib/Parser.d.ts.map +0 -1
- package/dist/types/lib/StringScanner.d.ts +0 -139
- package/dist/types/lib/StringScanner.d.ts.map +0 -1
- package/dist/types/lib/XmlCdata.d.ts +0 -11
- package/dist/types/lib/XmlCdata.d.ts.map +0 -1
- package/dist/types/lib/XmlComment.d.ts +0 -21
- package/dist/types/lib/XmlComment.d.ts.map +0 -1
- package/dist/types/lib/XmlDocument.d.ts +0 -42
- package/dist/types/lib/XmlDocument.d.ts.map +0 -1
- package/dist/types/lib/XmlElement.d.ts +0 -62
- package/dist/types/lib/XmlElement.d.ts.map +0 -1
- package/dist/types/lib/XmlNode.d.ts +0 -78
- package/dist/types/lib/XmlNode.d.ts.map +0 -1
- package/dist/types/lib/XmlProcessingInstruction.d.ts +0 -30
- package/dist/types/lib/XmlProcessingInstruction.d.ts.map +0 -1
- package/dist/types/lib/XmlText.d.ts +0 -21
- package/dist/types/lib/XmlText.d.ts.map +0 -1
- package/dist/types/lib/syntax.d.ts +0 -59
- package/dist/types/lib/syntax.d.ts.map +0 -1
- package/dist/umd/parse-xml.min.js +0 -2
- package/dist/umd/parse-xml.min.js.map +0 -1
- package/src/index.js +0 -67
- package/src/lib/Parser.js +0 -812
- package/src/lib/StringScanner.js +0 -312
- package/src/lib/XmlCdata.js +0 -17
- package/src/lib/XmlComment.js +0 -37
- package/src/lib/XmlDocument.js +0 -69
- package/src/lib/XmlElement.js +0 -101
- package/src/lib/XmlNode.js +0 -152
- package/src/lib/XmlProcessingInstruction.js +0 -48
- package/src/lib/XmlText.js +0 -37
- package/src/lib/syntax.js +0 -153
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
const emptyString = '';
|
|
2
|
+
const surrogatePair = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
|
|
3
|
+
|
|
4
|
+
/** @private */
|
|
5
|
+
export class StringScanner {
|
|
6
|
+
charIndex: number;
|
|
7
|
+
readonly string: string;
|
|
8
|
+
|
|
9
|
+
private readonly charCount: number;
|
|
10
|
+
private readonly charsToBytes: number[] | undefined;
|
|
11
|
+
private readonly length: number;
|
|
12
|
+
private readonly multiByteMode: boolean;
|
|
13
|
+
|
|
14
|
+
constructor(string: string) {
|
|
15
|
+
this.charCount = this.charLength(string, true);
|
|
16
|
+
this.charIndex = 0;
|
|
17
|
+
this.length = string.length;
|
|
18
|
+
this.multiByteMode = this.charCount !== this.length;
|
|
19
|
+
this.string = string;
|
|
20
|
+
|
|
21
|
+
if (this.multiByteMode) {
|
|
22
|
+
let charsToBytes = [];
|
|
23
|
+
|
|
24
|
+
// Create a mapping of character indexes to byte indexes. Since the string
|
|
25
|
+
// contains multibyte characters, a byte index may not necessarily align
|
|
26
|
+
// with a character index.
|
|
27
|
+
for (let byteIndex = 0, charIndex = 0; charIndex < this.charCount; ++charIndex) {
|
|
28
|
+
charsToBytes[charIndex] = byteIndex;
|
|
29
|
+
byteIndex += (string.codePointAt(byteIndex) as number) > 65535 ? 2 : 1;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
this.charsToBytes = charsToBytes;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Whether the current character index is at the end of the input string.
|
|
38
|
+
*/
|
|
39
|
+
get isEnd() {
|
|
40
|
+
return this.charIndex >= this.charCount;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// -- Protected Methods ------------------------------------------------------
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Returns the byte index of the given character index in the string. The two
|
|
47
|
+
* may differ in strings that contain multibyte characters.
|
|
48
|
+
*/
|
|
49
|
+
protected charIndexToByteIndex(charIndex: number = this.charIndex): number {
|
|
50
|
+
return this.multiByteMode
|
|
51
|
+
? (this.charsToBytes as number[])[charIndex] ?? Infinity
|
|
52
|
+
: charIndex;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Returns the number of characters in the given string, which may differ from
|
|
57
|
+
* the byte length if the string contains multibyte characters.
|
|
58
|
+
*/
|
|
59
|
+
protected charLength(string: string, multiByteSafe = this.multiByteMode): number {
|
|
60
|
+
// We could get the char length with `[ ...string ].length`, but that's
|
|
61
|
+
// actually slower than replacing surrogate pairs with single-byte
|
|
62
|
+
// characters and then counting the result.
|
|
63
|
+
return multiByteSafe
|
|
64
|
+
? string.replace(surrogatePair, '_').length
|
|
65
|
+
: string.length;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// -- Public Methods ---------------------------------------------------------
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Advances the scanner by the given number of characters, stopping if the end
|
|
72
|
+
* of the string is reached.
|
|
73
|
+
*/
|
|
74
|
+
advance(count = 1) {
|
|
75
|
+
this.charIndex = Math.min(this.charCount, this.charIndex + count);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Consumes and returns the given number of characters if possible, advancing
|
|
80
|
+
* the scanner and stopping if the end of the string is reached.
|
|
81
|
+
*
|
|
82
|
+
* If no characters could be consumed, an empty string will be returned.
|
|
83
|
+
*/
|
|
84
|
+
consume(count = 1): string {
|
|
85
|
+
let chars = this.peek(count);
|
|
86
|
+
this.advance(count);
|
|
87
|
+
return chars;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Consumes a match for the given sticky regex, advances the scanner, updates
|
|
92
|
+
* the `lastIndex` property of the regex, and returns the matching string.
|
|
93
|
+
*
|
|
94
|
+
* The regex must have a sticky flag ("y") so that its `lastIndex` prop can be
|
|
95
|
+
* used to anchor the match at the current scanner position.
|
|
96
|
+
*
|
|
97
|
+
* Returns the consumed string, or an empty string if nothing was consumed.
|
|
98
|
+
*/
|
|
99
|
+
consumeMatch(regex: RegExp): string {
|
|
100
|
+
if (!regex.sticky) {
|
|
101
|
+
throw new Error('`regex` must have a sticky flag ("y")');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
regex.lastIndex = this.charIndexToByteIndex();
|
|
105
|
+
|
|
106
|
+
let result = regex.exec(this.string);
|
|
107
|
+
|
|
108
|
+
if (result === null || result.length === 0) {
|
|
109
|
+
return emptyString;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
let match = result[0] as string;
|
|
113
|
+
this.advance(this.charLength(match));
|
|
114
|
+
return match;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Consumes and returns all characters for which the given function returns a
|
|
119
|
+
* truthy value, stopping on the first falsy return value or if the end of the
|
|
120
|
+
* input is reached.
|
|
121
|
+
*/
|
|
122
|
+
consumeMatchFn(fn: (char: string) => boolean): string {
|
|
123
|
+
let char;
|
|
124
|
+
let match = emptyString;
|
|
125
|
+
|
|
126
|
+
while ((char = this.peek()) && fn(char)) {
|
|
127
|
+
match += char;
|
|
128
|
+
this.advance();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return match;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Consumes the given string if it exists at the current character index, and
|
|
136
|
+
* advances the scanner.
|
|
137
|
+
*
|
|
138
|
+
* If the given string doesn't exist at the current character index, an empty
|
|
139
|
+
* string will be returned and the scanner will not be advanced.
|
|
140
|
+
*/
|
|
141
|
+
consumeString(stringToConsume: string): string {
|
|
142
|
+
if (this.consumeStringFast(stringToConsume)) {
|
|
143
|
+
return stringToConsume;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (this.multiByteMode) {
|
|
147
|
+
let { length } = stringToConsume;
|
|
148
|
+
let charLengthToMatch = this.charLength(stringToConsume);
|
|
149
|
+
|
|
150
|
+
if (charLengthToMatch !== length
|
|
151
|
+
&& stringToConsume === this.peek(charLengthToMatch)) {
|
|
152
|
+
|
|
153
|
+
this.advance(charLengthToMatch);
|
|
154
|
+
return stringToConsume;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return emptyString;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Does the same thing as `consumeString()`, but doesn't support consuming
|
|
163
|
+
* multibyte characters. This can be faster if you only need to match single
|
|
164
|
+
* byte characters.
|
|
165
|
+
*/
|
|
166
|
+
consumeStringFast(stringToConsume: string): string {
|
|
167
|
+
let { length } = stringToConsume;
|
|
168
|
+
|
|
169
|
+
if (this.peek(length) === stringToConsume) {
|
|
170
|
+
this.advance(length);
|
|
171
|
+
return stringToConsume;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return emptyString;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Consumes characters until the given global regex is matched, advancing the
|
|
179
|
+
* scanner up to (but not beyond) the beginning of the match. If the regex
|
|
180
|
+
* doesn't match, nothing will be consumed.
|
|
181
|
+
*
|
|
182
|
+
* Returns the consumed string, or an empty string if nothing was consumed.
|
|
183
|
+
*/
|
|
184
|
+
consumeUntilMatch(regex: RegExp): string {
|
|
185
|
+
let restOfString = this.string.slice(this.charIndexToByteIndex());
|
|
186
|
+
let matchByteIndex = restOfString.search(regex);
|
|
187
|
+
|
|
188
|
+
if (matchByteIndex <= 0) {
|
|
189
|
+
return emptyString;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
let result = restOfString.slice(0, matchByteIndex);
|
|
193
|
+
this.advance(this.charLength(result));
|
|
194
|
+
return result;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Consumes characters until the given string is found, advancing the scanner
|
|
199
|
+
* up to (but not beyond) that point. If the string is never found, nothing
|
|
200
|
+
* will be consumed.
|
|
201
|
+
*
|
|
202
|
+
* Returns the consumed string, or an empty string if nothing was consumed.
|
|
203
|
+
*/
|
|
204
|
+
consumeUntilString(searchString: string): string {
|
|
205
|
+
let { string } = this;
|
|
206
|
+
let byteIndex = this.charIndexToByteIndex();
|
|
207
|
+
let matchByteIndex = string.indexOf(searchString, byteIndex);
|
|
208
|
+
|
|
209
|
+
if (matchByteIndex <= 0) {
|
|
210
|
+
return emptyString;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
let result = string.slice(byteIndex, matchByteIndex);
|
|
214
|
+
this.advance(this.charLength(result));
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Returns the given number of characters starting at the current character
|
|
220
|
+
* index, without advancing the scanner and without exceeding the end of the
|
|
221
|
+
* input string.
|
|
222
|
+
*/
|
|
223
|
+
peek(count = 1): string {
|
|
224
|
+
let { charIndex, multiByteMode, string } = this;
|
|
225
|
+
|
|
226
|
+
if (multiByteMode) {
|
|
227
|
+
// Inlining this comparison instead of checking `this.isEnd` improves perf
|
|
228
|
+
// slightly since `peek()` is called so frequently.
|
|
229
|
+
if (charIndex >= this.charCount) {
|
|
230
|
+
return emptyString;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return string.slice(
|
|
234
|
+
this.charIndexToByteIndex(charIndex),
|
|
235
|
+
this.charIndexToByteIndex(charIndex + count),
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return string.slice(charIndex, charIndex + count);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Resets the scanner position to the given character _index_, or to the start
|
|
244
|
+
* of the input string if no index is given.
|
|
245
|
+
*
|
|
246
|
+
* If _index_ is negative, the scanner position will be moved backward by that
|
|
247
|
+
* many characters, stopping if the beginning of the string is reached.
|
|
248
|
+
*/
|
|
249
|
+
reset(index = 0) {
|
|
250
|
+
this.charIndex = index >= 0
|
|
251
|
+
? Math.min(this.charCount, index)
|
|
252
|
+
: Math.max(0, this.charIndex + index);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { XmlNode } from './XmlNode.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A comment within an XML document.
|
|
5
|
+
*/
|
|
6
|
+
export class XmlComment extends XmlNode {
|
|
7
|
+
/**
|
|
8
|
+
* Content of this comment.
|
|
9
|
+
*/
|
|
10
|
+
content: string;
|
|
11
|
+
|
|
12
|
+
constructor(content = '') {
|
|
13
|
+
super();
|
|
14
|
+
this.content = content;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
override get type() {
|
|
18
|
+
return XmlNode.TYPE_COMMENT;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
override toJSON() {
|
|
22
|
+
return Object.assign(XmlNode.prototype.toJSON.call(this), {
|
|
23
|
+
content: this.content,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { XmlElement } from './XmlElement.js';
|
|
2
|
+
import { XmlNode } from './XmlNode.js';
|
|
3
|
+
|
|
4
|
+
import type { XmlComment } from './XmlComment.js';
|
|
5
|
+
import type { XmlProcessingInstruction } from './XmlProcessingInstruction.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Represents an XML document. All elements within the document are descendants
|
|
9
|
+
* of this node.
|
|
10
|
+
*/
|
|
11
|
+
export class XmlDocument extends XmlNode {
|
|
12
|
+
/**
|
|
13
|
+
* Child nodes of this document.
|
|
14
|
+
*/
|
|
15
|
+
readonly children: Array<XmlComment | XmlProcessingInstruction | XmlElement>;
|
|
16
|
+
|
|
17
|
+
constructor(children: Array<XmlComment | XmlElement | XmlProcessingInstruction> = []) {
|
|
18
|
+
super();
|
|
19
|
+
this.children = children;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
override get document() {
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Root element of this document, or `null` if this document is empty.
|
|
28
|
+
*/
|
|
29
|
+
get root(): XmlElement | null {
|
|
30
|
+
for (let child of this.children) {
|
|
31
|
+
if (child instanceof XmlElement) {
|
|
32
|
+
return child;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Text content of this document and all its descendants.
|
|
41
|
+
*/
|
|
42
|
+
get text(): string {
|
|
43
|
+
return this.children
|
|
44
|
+
.map(child => 'text' in child ? child.text : '')
|
|
45
|
+
.join('');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
override get type() {
|
|
49
|
+
return XmlNode.TYPE_DOCUMENT;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
override toJSON() {
|
|
53
|
+
return Object.assign(XmlNode.prototype.toJSON.call(this), {
|
|
54
|
+
children: this.children.map(child => child.toJSON()),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { XmlNode } from './XmlNode.js';
|
|
2
|
+
|
|
3
|
+
import type { JsonObject } from './types.js';
|
|
4
|
+
import type { XmlCdata } from './XmlCdata.js';
|
|
5
|
+
import type { XmlComment } from './XmlComment.js';
|
|
6
|
+
import type { XmlProcessingInstruction } from './XmlProcessingInstruction.js';
|
|
7
|
+
import type { XmlText } from './XmlText.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Element in an XML document.
|
|
11
|
+
*/
|
|
12
|
+
export class XmlElement extends XmlNode {
|
|
13
|
+
/**
|
|
14
|
+
* Attributes on this element.
|
|
15
|
+
*/
|
|
16
|
+
attributes: {[attrName: string]: string};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Child nodes of this element.
|
|
20
|
+
*/
|
|
21
|
+
children: Array<XmlCdata | XmlComment | XmlElement | XmlProcessingInstruction | XmlText>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Name of this element.
|
|
25
|
+
*/
|
|
26
|
+
name: string;
|
|
27
|
+
|
|
28
|
+
constructor(
|
|
29
|
+
name: string,
|
|
30
|
+
attributes: {[attrName: string]: string} = Object.create(null),
|
|
31
|
+
children: Array<XmlCdata | XmlComment | XmlElement | XmlProcessingInstruction | XmlText> = [],
|
|
32
|
+
) {
|
|
33
|
+
super();
|
|
34
|
+
|
|
35
|
+
this.name = name;
|
|
36
|
+
this.attributes = attributes;
|
|
37
|
+
this.children = children;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Whether this element is empty (meaning it has no children).
|
|
42
|
+
*/
|
|
43
|
+
get isEmpty(): boolean {
|
|
44
|
+
return this.children.length === 0;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
override get preserveWhitespace(): boolean {
|
|
48
|
+
let node: XmlNode | null = this; // eslint-disable-line @typescript-eslint/no-this-alias
|
|
49
|
+
|
|
50
|
+
while (node instanceof XmlElement) {
|
|
51
|
+
if ('xml:space' in node.attributes) {
|
|
52
|
+
return node.attributes['xml:space'] === 'preserve';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
node = node.parent;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Text content of this element and all its descendants.
|
|
63
|
+
*/
|
|
64
|
+
get text(): string {
|
|
65
|
+
return this.children
|
|
66
|
+
.map(child => 'text' in child ? child.text : '')
|
|
67
|
+
.join('');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
override get type() {
|
|
71
|
+
return XmlNode.TYPE_ELEMENT;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
override toJSON(): JsonObject {
|
|
75
|
+
return Object.assign(XmlNode.prototype.toJSON.call(this), {
|
|
76
|
+
name: this.name,
|
|
77
|
+
attributes: this.attributes,
|
|
78
|
+
children: this.children.map(child => child.toJSON()),
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { JsonObject } from './types.js';
|
|
2
|
+
import type { XmlDocument } from './XmlDocument.js';
|
|
3
|
+
import type { XmlElement } from './XmlElement.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Base interface for a node in an XML document.
|
|
7
|
+
*/
|
|
8
|
+
export class XmlNode {
|
|
9
|
+
/**
|
|
10
|
+
* Type value for an `XmlCdata` node.
|
|
11
|
+
*/
|
|
12
|
+
static readonly TYPE_CDATA = 'cdata';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Type value for an `XmlComment` node.
|
|
16
|
+
*/
|
|
17
|
+
static readonly TYPE_COMMENT = 'comment';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Type value for an `XmlDocument` node.
|
|
21
|
+
*/
|
|
22
|
+
static readonly TYPE_DOCUMENT = 'document';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Type value for an `XmlElement` node.
|
|
26
|
+
*/
|
|
27
|
+
static readonly TYPE_ELEMENT = 'element';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Type value for an `XmlProcessingInstruction` node.
|
|
31
|
+
*/
|
|
32
|
+
static readonly TYPE_PROCESSING_INSTRUCTION = 'pi';
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Type value for an `XmlText` node.
|
|
36
|
+
*/
|
|
37
|
+
static readonly TYPE_TEXT = 'text';
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Parent node of this node, or `null` if this node has no parent.
|
|
41
|
+
*/
|
|
42
|
+
parent: XmlDocument | XmlElement | null = null;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Document that contains this node, or `null` if this node is not associated
|
|
46
|
+
* with a document.
|
|
47
|
+
*/
|
|
48
|
+
get document(): XmlDocument | null {
|
|
49
|
+
return this.parent?.document ?? null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Whether this node is the root node of the document.
|
|
54
|
+
*/
|
|
55
|
+
get isRootNode(): boolean {
|
|
56
|
+
return this.parent !== null && this.parent === this.document;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Whether whitespace should be preserved in the content of this element and
|
|
61
|
+
* its children.
|
|
62
|
+
*
|
|
63
|
+
* This is influenced by the value of the special `xml:space` attribute, and
|
|
64
|
+
* will be `true` for any node whose `xml:space` attribute is set to
|
|
65
|
+
* "preserve". If a node has no such attribute, it will inherit the value of
|
|
66
|
+
* the nearest ancestor that does (if any).
|
|
67
|
+
*
|
|
68
|
+
* @see https://www.w3.org/TR/2008/REC-xml-20081126/#sec-white-space
|
|
69
|
+
*/
|
|
70
|
+
get preserveWhitespace(): boolean {
|
|
71
|
+
return Boolean(this.parent?.preserveWhitespace);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Type of this node.
|
|
76
|
+
*
|
|
77
|
+
* The value of this property is a string that matches one of the static
|
|
78
|
+
* `TYPE_*` properties on the `XmlNode` class (e.g. `TYPE_ELEMENT`,
|
|
79
|
+
* `TYPE_TEXT`, etc.).
|
|
80
|
+
*
|
|
81
|
+
* The `XmlNode` class itself is a base class and doesn't have its own type
|
|
82
|
+
* name.
|
|
83
|
+
*/
|
|
84
|
+
get type() {
|
|
85
|
+
return '';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Returns a JSON-serializable object representing this node, minus properties
|
|
90
|
+
* that could result in circular references.
|
|
91
|
+
*/
|
|
92
|
+
toJSON(): JsonObject {
|
|
93
|
+
let json: JsonObject = {
|
|
94
|
+
type: this.type,
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
if (this.isRootNode) {
|
|
98
|
+
json.isRootNode = true;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (this.preserveWhitespace) {
|
|
102
|
+
json.preserveWhitespace = true;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return json;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { XmlNode } from './XmlNode.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A processing instruction within an XML document.
|
|
5
|
+
*/
|
|
6
|
+
export class XmlProcessingInstruction extends XmlNode {
|
|
7
|
+
/**
|
|
8
|
+
* Content of this processing instruction.
|
|
9
|
+
*/
|
|
10
|
+
content: string;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Name of this processing instruction. Also sometimes referred to as the
|
|
14
|
+
* processing instruction "target".
|
|
15
|
+
*/
|
|
16
|
+
name: string;
|
|
17
|
+
|
|
18
|
+
constructor(name: string, content = '') {
|
|
19
|
+
super();
|
|
20
|
+
|
|
21
|
+
this.name = name;
|
|
22
|
+
this.content = content;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
override get type() {
|
|
26
|
+
return XmlNode.TYPE_PROCESSING_INSTRUCTION;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
override toJSON() {
|
|
30
|
+
return Object.assign(XmlNode.prototype.toJSON.call(this), {
|
|
31
|
+
name: this.name,
|
|
32
|
+
content: this.content,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { XmlNode } from './XmlNode.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Text content within an XML document.
|
|
5
|
+
*/
|
|
6
|
+
export class XmlText extends XmlNode {
|
|
7
|
+
/**
|
|
8
|
+
* Text content of this node.
|
|
9
|
+
*/
|
|
10
|
+
text: string;
|
|
11
|
+
|
|
12
|
+
constructor(text = '') {
|
|
13
|
+
super();
|
|
14
|
+
this.text = text;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
override get type() {
|
|
18
|
+
return XmlNode.TYPE_TEXT;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
override toJSON() {
|
|
22
|
+
return Object.assign(XmlNode.prototype.toJSON.call(this), {
|
|
23
|
+
text: this.text,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|