xml-tokenizer 0.0.25 → 0.0.27
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/dist/cjs/get-q-name.js +1 -7
- package/dist/cjs/index.js +1 -75
- package/dist/cjs/selector/TokenSelectState.js +1 -106
- package/dist/cjs/selector/TokenSelectStateMachine.js +1 -150
- package/dist/cjs/selector/TokenSelector.js +1 -139
- package/dist/cjs/selector/match-string.js +1 -21
- package/dist/cjs/selector/select.js +1 -19
- package/dist/cjs/token-to-xml.js +1 -44
- package/dist/cjs/tokenizer/XmlError.js +1 -96
- package/dist/cjs/tokenizer/XmlStream.js +1 -447
- package/dist/cjs/tokenizer/ascii-constants.js +1 -69
- package/dist/cjs/tokenizer/tokenize.js +1 -408
- package/dist/cjs/tokenizer/utils.js +1 -47
- package/dist/cjs/tokens-to-xml.js +1 -13
- package/dist/cjs/xml-to-object.js +1 -69
- package/dist/cjs/xml-to-simplified-object.js +1 -86
- package/dist/esm/get-q-name.js +1 -5
- package/dist/esm/index.js +1 -14
- package/dist/esm/selector/TokenSelectState.js +1 -102
- package/dist/esm/selector/TokenSelectStateMachine.js +1 -148
- package/dist/esm/selector/TokenSelector.js +1 -137
- package/dist/esm/selector/match-string.js +1 -19
- package/dist/esm/selector/select.js +1 -17
- package/dist/esm/token-to-xml.js +1 -42
- package/dist/esm/tokenizer/XmlError.js +1 -94
- package/dist/esm/tokenizer/XmlStream.js +1 -445
- package/dist/esm/tokenizer/ascii-constants.js +1 -35
- package/dist/esm/tokenizer/tokenize.js +1 -405
- package/dist/esm/tokenizer/utils.js +1 -40
- package/dist/esm/tokens-to-xml.js +1 -11
- package/dist/esm/xml-to-object.js +1 -66
- package/dist/esm/xml-to-simplified-object.js +1 -83
- package/dist/types/get-q-name.d.ts +0 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/selector/TokenSelectState.d.ts +0 -1
- package/dist/types/selector/TokenSelectStateMachine.d.ts +0 -1
- package/dist/types/selector/TokenSelector.d.ts +0 -1
- package/dist/types/selector/index.d.ts +0 -1
- package/dist/types/selector/match-string.d.ts +0 -1
- package/dist/types/selector/select.d.ts +0 -1
- package/dist/types/selector/types.d.ts +0 -1
- package/dist/types/token-to-xml.d.ts +0 -1
- package/dist/types/tokenizer/XmlError.d.ts +0 -1
- package/dist/types/tokenizer/XmlStream.d.ts +20 -1
- package/dist/types/tokenizer/ascii-constants.d.ts +0 -1
- package/dist/types/tokenizer/index.d.ts +0 -1
- package/dist/types/tokenizer/tokenize.d.ts +0 -1
- package/dist/types/tokenizer/types.d.ts +3 -2
- package/dist/types/tokenizer/utils.d.ts +0 -1
- package/dist/types/tokens-to-xml.d.ts +0 -1
- package/dist/types/xml-to-object.d.ts +0 -1
- package/dist/types/xml-to-simplified-object.d.ts +0 -1
- package/package.json +2 -2
- package/dist/types/get-q-name.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/selector/TokenSelectState.d.ts.map +0 -1
- package/dist/types/selector/TokenSelectStateMachine.d.ts.map +0 -1
- package/dist/types/selector/TokenSelector.d.ts.map +0 -1
- package/dist/types/selector/index.d.ts.map +0 -1
- package/dist/types/selector/match-string.d.ts.map +0 -1
- package/dist/types/selector/select.d.ts.map +0 -1
- package/dist/types/selector/types.d.ts.map +0 -1
- package/dist/types/token-to-xml.d.ts.map +0 -1
- package/dist/types/tokenizer/XmlError.d.ts.map +0 -1
- package/dist/types/tokenizer/XmlStream.d.ts.map +0 -1
- package/dist/types/tokenizer/ascii-constants.d.ts.map +0 -1
- package/dist/types/tokenizer/index.d.ts.map +0 -1
- package/dist/types/tokenizer/tokenize.d.ts.map +0 -1
- package/dist/types/tokenizer/types.d.ts.map +0 -1
- package/dist/types/tokenizer/utils.d.ts.map +0 -1
- package/dist/types/tokens-to-xml.d.ts.map +0 -1
- package/dist/types/xml-to-object.d.ts.map +0 -1
- package/dist/types/xml-to-simplified-object.d.ts.map +0 -1
|
@@ -1,445 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { isXmlChar, isXmlSpaceByte, isAsciiDigit, isXmlNameStart, isXmlName } from './utils.js';
|
|
3
|
-
import { XmlError } from './XmlError.js';
|
|
4
|
-
|
|
5
|
-
var __defProp = Object.defineProperty;
|
|
6
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
9
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
10
|
-
var __spreadValues = (a, b) => {
|
|
11
|
-
for (var prop in b || (b = {}))
|
|
12
|
-
if (__hasOwnProp.call(b, prop))
|
|
13
|
-
__defNormalProp(a, prop, b[prop]);
|
|
14
|
-
if (__getOwnPropSymbols)
|
|
15
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
16
|
-
if (__propIsEnum.call(b, prop))
|
|
17
|
-
__defNormalProp(a, prop, b[prop]);
|
|
18
|
-
}
|
|
19
|
-
return a;
|
|
20
|
-
};
|
|
21
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
22
|
-
class XmlStream {
|
|
23
|
-
constructor(text, options = {}) {
|
|
24
|
-
__publicField(this, "_text");
|
|
25
|
-
__publicField(this, "_pos");
|
|
26
|
-
__publicField(this, "_end");
|
|
27
|
-
__publicField(this, "config");
|
|
28
|
-
const { pos = 0, strictDocument = true, allowDtd = true, rawTextElements = null } = options;
|
|
29
|
-
this._text = text;
|
|
30
|
-
this._pos = pos;
|
|
31
|
-
this._end = this._text.length;
|
|
32
|
-
this.config = {
|
|
33
|
-
strictDocument,
|
|
34
|
-
allowDtd,
|
|
35
|
-
rawTextElements
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Creates a clone of the current stream.
|
|
40
|
-
*
|
|
41
|
-
* @returns A new instance of XmlStream with the same state.
|
|
42
|
-
*/
|
|
43
|
-
clone() {
|
|
44
|
-
return new XmlStream(this._text, __spreadValues({ pos: this._pos }, this.config));
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Gets the current position in the stream.
|
|
48
|
-
*
|
|
49
|
-
* @returns The current position.
|
|
50
|
-
*/
|
|
51
|
-
getPos() {
|
|
52
|
-
return this._pos;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Checks if the stream is at the end.
|
|
56
|
-
*
|
|
57
|
-
* @returns True if at the end, false otherwise.
|
|
58
|
-
*/
|
|
59
|
-
atEnd() {
|
|
60
|
-
return this._pos >= this._end;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Gets the current code unit in the stream.
|
|
64
|
-
*
|
|
65
|
-
* @returns The current code unit.
|
|
66
|
-
* @throws XmlError if at the end of the stream.
|
|
67
|
-
*/
|
|
68
|
-
currCodeUnit() {
|
|
69
|
-
if (this._pos >= this._end) {
|
|
70
|
-
throw new XmlError({ type: "UnexpectedEndOfStream" });
|
|
71
|
-
}
|
|
72
|
-
return this._text.charCodeAt(this._pos);
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Gets the current code unit without checking for the end.
|
|
76
|
-
*
|
|
77
|
-
* @returns The current code unit.
|
|
78
|
-
*/
|
|
79
|
-
currCodeUnitUnchecked() {
|
|
80
|
-
return this._text.charCodeAt(this._pos);
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Gets the next code unit in the stream.
|
|
84
|
-
*
|
|
85
|
-
* @returns The next code unit.
|
|
86
|
-
* @throws XmlError if at the end of the stream.
|
|
87
|
-
*/
|
|
88
|
-
nextCodeUnit() {
|
|
89
|
-
if (this._pos + 1 >= this._end) {
|
|
90
|
-
throw new XmlError({ type: "UnexpectedEndOfStream" });
|
|
91
|
-
}
|
|
92
|
-
return this._text.charCodeAt(this._pos + 1);
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Consumes a specific code unit (character) from the stream.
|
|
96
|
-
*
|
|
97
|
-
* @param codeUnit - The code unit to consume.
|
|
98
|
-
* @throws XmlError if the current code unit doesn't match or if at the end.
|
|
99
|
-
*/
|
|
100
|
-
consumeCodeUnit(codeUnit) {
|
|
101
|
-
const currCodeUnit = this.currCodeUnit();
|
|
102
|
-
if (currCodeUnit !== codeUnit) {
|
|
103
|
-
throw new XmlError(
|
|
104
|
-
{ type: "InvalidChar", expected: codeUnit, actual: currCodeUnit },
|
|
105
|
-
this.genTextPos()
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
this._pos += 1;
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Tries to consume a specific code unit (character) from the stream.
|
|
112
|
-
* Unlike `consumeCodeUnit`, it will not throw any errors.
|
|
113
|
-
*
|
|
114
|
-
* @param codeUnit - The code unit to consume.
|
|
115
|
-
* @returns True if the code unit was consumed, false otherwise.
|
|
116
|
-
*/
|
|
117
|
-
tryConsumeCodeUnit(codeUnit) {
|
|
118
|
-
if (this.currCodeUnit() === codeUnit) {
|
|
119
|
-
this._pos += 1;
|
|
120
|
-
return true;
|
|
121
|
-
}
|
|
122
|
-
return false;
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Consumes code units that satisfy a predicate function.
|
|
126
|
-
*
|
|
127
|
-
* @param predicate - The function to test each code unit.
|
|
128
|
-
* @returns The consumed string.
|
|
129
|
-
*/
|
|
130
|
-
consumeCodeUnitsWhile(predicate) {
|
|
131
|
-
const start = this._pos;
|
|
132
|
-
this.skipCodeUnitsWhile(predicate);
|
|
133
|
-
return this.sliceBack(start);
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Skips code units that satisfy a predicate function.
|
|
137
|
-
*
|
|
138
|
-
* @param predicate - The function to test each code unit.
|
|
139
|
-
*/
|
|
140
|
-
skipCodeUnitsWhile(predicate) {
|
|
141
|
-
while (this._pos < this._end && predicate(this.currCodeUnitUnchecked(), this)) {
|
|
142
|
-
if (!isXmlChar(this.currCodeUnitUnchecked())) {
|
|
143
|
-
throw new XmlError(
|
|
144
|
-
{
|
|
145
|
-
type: "NonXmlChar",
|
|
146
|
-
char: String.fromCodePoint(this.currCodeUnitUnchecked())
|
|
147
|
-
},
|
|
148
|
-
this.genTextPos()
|
|
149
|
-
);
|
|
150
|
-
}
|
|
151
|
-
this._pos += 1;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Advances the stream position by the specified number of characters.
|
|
156
|
-
*
|
|
157
|
-
* @param n - The number of characters to advance.
|
|
158
|
-
*/
|
|
159
|
-
advance(n) {
|
|
160
|
-
this._pos += n;
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Go to a new position in the stream.
|
|
164
|
-
*
|
|
165
|
-
* @param pos - The new position.
|
|
166
|
-
*/
|
|
167
|
-
goTo(pos) {
|
|
168
|
-
this._pos = pos;
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Checks if the stream starts with the given text.
|
|
172
|
-
*
|
|
173
|
-
* @param text - The text to check.
|
|
174
|
-
* @returns True if the stream starts with the text, false otherwise.
|
|
175
|
-
*/
|
|
176
|
-
startsWith(text) {
|
|
177
|
-
return this._text.startsWith(text, this._pos);
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Skips a specific string in the stream.
|
|
181
|
-
*
|
|
182
|
-
* @param text - The string to skip.
|
|
183
|
-
* @throws XmlError if the stream doesn't start with the given string.
|
|
184
|
-
*/
|
|
185
|
-
skipString(text) {
|
|
186
|
-
if (!this.startsWith(text)) {
|
|
187
|
-
throw new XmlError({ type: "InvalidString", expected: text }, this.genTextPos());
|
|
188
|
-
}
|
|
189
|
-
this._pos += text.length;
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Slices the text from the given position to the current position.
|
|
193
|
-
*
|
|
194
|
-
* @param pos - The start position.
|
|
195
|
-
* @returns The sliced string.
|
|
196
|
-
*/
|
|
197
|
-
sliceBack(pos) {
|
|
198
|
-
return this._text.slice(pos, this._pos);
|
|
199
|
-
}
|
|
200
|
-
/**
|
|
201
|
-
* Creates a range from the given start position to the current position.
|
|
202
|
-
*
|
|
203
|
-
* @param start - The start position.
|
|
204
|
-
* @returns The range object.
|
|
205
|
-
*/
|
|
206
|
-
rangeFrom(start) {
|
|
207
|
-
return { start, end: this._pos };
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* Skips whitespace characters in the stream.
|
|
211
|
-
*/
|
|
212
|
-
skipSpaces() {
|
|
213
|
-
while (this.startsWithSpace()) {
|
|
214
|
-
this._pos += 1;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* Checks if the stream starts with a whitespace character.
|
|
219
|
-
*
|
|
220
|
-
* @returns True if it starts with a whitespace, false otherwise.
|
|
221
|
-
*/
|
|
222
|
-
startsWithSpace() {
|
|
223
|
-
return this._pos < this._end && isXmlSpaceByte(this.currCodeUnitUnchecked());
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Consumes whitespace characters in the stream.
|
|
227
|
-
* Like `skipSpaces`, but checks that the first char is actually a space.
|
|
228
|
-
*
|
|
229
|
-
* @throws XmlError if the stream doesn't start with a whitespace or is at the end.
|
|
230
|
-
*/
|
|
231
|
-
consumeSpaces() {
|
|
232
|
-
if (this._pos >= this._end) {
|
|
233
|
-
throw new XmlError({ type: "UnexpectedEndOfStream" }, this.genTextPos());
|
|
234
|
-
}
|
|
235
|
-
if (!this.startsWithSpace()) {
|
|
236
|
-
throw new XmlError(
|
|
237
|
-
{ type: "InvalidChar", expected: "a whitespace", actual: this.currCodeUnitUnchecked() },
|
|
238
|
-
this.genTextPos()
|
|
239
|
-
);
|
|
240
|
-
}
|
|
241
|
-
this.skipSpaces();
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Tries to consume a reference (entity or character reference).
|
|
245
|
-
* Consumes according to: https://www.w3.org/TR/xml/#NT-Reference
|
|
246
|
-
*
|
|
247
|
-
* @returns The consumed reference or null if not a reference.
|
|
248
|
-
*/
|
|
249
|
-
tryConsumeReference() {
|
|
250
|
-
const start = this._pos;
|
|
251
|
-
const subStream = this.clone();
|
|
252
|
-
const result = subStream.consumeReference();
|
|
253
|
-
if (result != null) {
|
|
254
|
-
this._pos += subStream.getPos() - start;
|
|
255
|
-
return result;
|
|
256
|
-
}
|
|
257
|
-
return null;
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* Consumes a reference (entity or character reference).
|
|
261
|
-
*
|
|
262
|
-
* @returns The consumed reference or null if not a reference.
|
|
263
|
-
*/
|
|
264
|
-
consumeReference() {
|
|
265
|
-
if (!this.tryConsumeCodeUnit(AMPERSAND)) {
|
|
266
|
-
return null;
|
|
267
|
-
}
|
|
268
|
-
let reference;
|
|
269
|
-
if (this.tryConsumeCodeUnit(HASH)) {
|
|
270
|
-
let value;
|
|
271
|
-
let radix;
|
|
272
|
-
if (this.tryConsumeCodeUnit(LOWERCASE_X)) {
|
|
273
|
-
value = this.consumeCodeUnitsWhile(
|
|
274
|
-
(codeUnit) => codeUnit >= ZERO && codeUnit <= NINE || codeUnit >= UPPERCASE_A && codeUnit <= UPPERCASE_F || codeUnit >= LOWERCASE_A && codeUnit <= LOWERCASE_F
|
|
275
|
-
);
|
|
276
|
-
radix = 16;
|
|
277
|
-
} else {
|
|
278
|
-
value = this.consumeCodeUnitsWhile((codeUnit) => isAsciiDigit(codeUnit));
|
|
279
|
-
radix = 10;
|
|
280
|
-
}
|
|
281
|
-
const codePoint = parseInt(value, radix);
|
|
282
|
-
if (isNaN(codePoint) || !isXmlChar(codePoint)) {
|
|
283
|
-
reference = null;
|
|
284
|
-
} else {
|
|
285
|
-
reference = { type: "Char", value: String.fromCodePoint(codePoint) };
|
|
286
|
-
}
|
|
287
|
-
} else {
|
|
288
|
-
const name = this.consumeName();
|
|
289
|
-
switch (name) {
|
|
290
|
-
case "quot":
|
|
291
|
-
reference = { type: "Char", value: '"' };
|
|
292
|
-
break;
|
|
293
|
-
case "amp":
|
|
294
|
-
reference = { type: "Char", value: "&" };
|
|
295
|
-
break;
|
|
296
|
-
case "apos":
|
|
297
|
-
reference = { type: "Char", value: "'" };
|
|
298
|
-
break;
|
|
299
|
-
case "lt":
|
|
300
|
-
reference = { type: "Char", value: "<" };
|
|
301
|
-
break;
|
|
302
|
-
case "gt":
|
|
303
|
-
reference = { type: "Char", value: ">" };
|
|
304
|
-
break;
|
|
305
|
-
default:
|
|
306
|
-
reference = { type: "Entity", value: name };
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
if (!this.tryConsumeCodeUnit(SEMICOLON)) {
|
|
310
|
-
return null;
|
|
311
|
-
}
|
|
312
|
-
return reference;
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Consumes a XML name.
|
|
316
|
-
* Consumes according to: https://www.w3.org/TR/xml/#NT-Name
|
|
317
|
-
*
|
|
318
|
-
* @returns The consumed name.
|
|
319
|
-
* @throws XmlError if an invalid name is encountered.
|
|
320
|
-
*/
|
|
321
|
-
consumeName() {
|
|
322
|
-
const start = this._pos;
|
|
323
|
-
this.skipName();
|
|
324
|
-
const name = this.sliceBack(start);
|
|
325
|
-
if (name.length === 0) {
|
|
326
|
-
throw new XmlError({ type: "InvalidName" }, this.genTextPosFrom(start));
|
|
327
|
-
}
|
|
328
|
-
return name;
|
|
329
|
-
}
|
|
330
|
-
/**
|
|
331
|
-
* Skips a XML name.
|
|
332
|
-
* The same as `consumeName`, but does not return the consumed name.
|
|
333
|
-
* @throws XmlError if an invalid name is encountered.
|
|
334
|
-
*/
|
|
335
|
-
skipName() {
|
|
336
|
-
const start = this._pos;
|
|
337
|
-
while (this._pos < this._end) {
|
|
338
|
-
const currCodeUnit = this.currCodeUnitUnchecked();
|
|
339
|
-
if (this._pos === start) {
|
|
340
|
-
if (!isXmlNameStart(currCodeUnit)) {
|
|
341
|
-
throw new XmlError({ type: "InvalidName" }, this.genTextPosFrom(start));
|
|
342
|
-
}
|
|
343
|
-
} else if (!isXmlName(currCodeUnit)) {
|
|
344
|
-
break;
|
|
345
|
-
}
|
|
346
|
-
this._pos += 1;
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Consumes a qualified XML name and returns it.
|
|
351
|
-
* Consumes according to: https://www.w3.org/TR/xml-names/#ns-qualnames
|
|
352
|
-
*
|
|
353
|
-
* @returns The prefix and local name as a tuple.
|
|
354
|
-
* @throws XmlError if an invalid qualified XML name is encountered.
|
|
355
|
-
*/
|
|
356
|
-
consumeQName() {
|
|
357
|
-
var _a, _b;
|
|
358
|
-
const start = this._pos;
|
|
359
|
-
let splitter = null;
|
|
360
|
-
while (this._pos < this._end) {
|
|
361
|
-
const currCodeUnit = this.currCodeUnitUnchecked();
|
|
362
|
-
if (currCodeUnit === COLON) {
|
|
363
|
-
if (splitter == null) {
|
|
364
|
-
splitter = this._pos;
|
|
365
|
-
this._pos += 1;
|
|
366
|
-
} else {
|
|
367
|
-
throw new XmlError({ type: "InvalidName" }, this.genTextPosFrom(start));
|
|
368
|
-
}
|
|
369
|
-
} else if (isXmlName(currCodeUnit)) {
|
|
370
|
-
this._pos += 1;
|
|
371
|
-
} else {
|
|
372
|
-
break;
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
let prefix;
|
|
376
|
-
let local;
|
|
377
|
-
if (splitter != null) {
|
|
378
|
-
prefix = this._text.slice(start, splitter);
|
|
379
|
-
local = this.sliceBack(splitter + 1);
|
|
380
|
-
} else {
|
|
381
|
-
prefix = "";
|
|
382
|
-
local = this.sliceBack(start);
|
|
383
|
-
}
|
|
384
|
-
if (prefix.length > 0 && !isXmlNameStart((_a = prefix[0]) == null ? void 0 : _a.codePointAt(0))) {
|
|
385
|
-
throw new XmlError({ type: "InvalidName" }, this.genTextPosFrom(start));
|
|
386
|
-
}
|
|
387
|
-
if (local.length > 0) {
|
|
388
|
-
if (!isXmlNameStart((_b = local[0]) == null ? void 0 : _b.codePointAt(0))) {
|
|
389
|
-
throw new XmlError({ type: "InvalidName" }, this.genTextPosFrom(start));
|
|
390
|
-
}
|
|
391
|
-
} else {
|
|
392
|
-
throw new XmlError({ type: "InvalidName" }, this.genTextPosFrom(start));
|
|
393
|
-
}
|
|
394
|
-
return [prefix, local];
|
|
395
|
-
}
|
|
396
|
-
/**
|
|
397
|
-
* Consumes a quote character.
|
|
398
|
-
*
|
|
399
|
-
* @returns The consumed quote character.
|
|
400
|
-
* @throws XmlError if an invalid quote character is encountered.
|
|
401
|
-
*/
|
|
402
|
-
consumeQuote() {
|
|
403
|
-
const currCodeUnit = this.currCodeUnit();
|
|
404
|
-
if (currCodeUnit === SINGLE_QUOTE || currCodeUnit === DOUBLE_QUOTE) {
|
|
405
|
-
this._pos += 1;
|
|
406
|
-
return currCodeUnit;
|
|
407
|
-
}
|
|
408
|
-
throw new XmlError(
|
|
409
|
-
{ type: "InvalidChar", expected: "a quote", actual: currCodeUnit },
|
|
410
|
-
this.genTextPos()
|
|
411
|
-
);
|
|
412
|
-
}
|
|
413
|
-
/**
|
|
414
|
-
* Calculates the current absolute position.
|
|
415
|
-
* This operation is very expensive. Use only for errors.
|
|
416
|
-
*
|
|
417
|
-
* @returns The current text position.
|
|
418
|
-
*/
|
|
419
|
-
genTextPos() {
|
|
420
|
-
return this.genTextPosFrom(this._pos);
|
|
421
|
-
}
|
|
422
|
-
/**
|
|
423
|
-
* Calculates an absolute position at the specified position.
|
|
424
|
-
* This operation is very expensive. Use only for errors.
|
|
425
|
-
*
|
|
426
|
-
* @param pos - The position to calculate from.
|
|
427
|
-
* @returns The calculated text position.
|
|
428
|
-
*/
|
|
429
|
-
genTextPosFrom(pos) {
|
|
430
|
-
const clampedPos = Math.min(pos, this._end);
|
|
431
|
-
let row = 1;
|
|
432
|
-
let col = 1;
|
|
433
|
-
for (let i = 0; i < clampedPos; i++) {
|
|
434
|
-
if (this._text.charCodeAt(i) === LINE_FEED) {
|
|
435
|
-
row++;
|
|
436
|
-
col = 1;
|
|
437
|
-
} else {
|
|
438
|
-
col++;
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
return { row, col };
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
export { XmlStream };
|
|
1
|
+
import{AMPERSAND as f,HASH as x,LOWERCASE_X as g,ZERO as U,NINE as w,UPPERCASE_A as y,UPPERCASE_F as S,LOWERCASE_A as E,LOWERCASE_F as v,SEMICOLON as P,COLON as k,SINGLE_QUOTE as N,DOUBLE_QUOTE as T,LINE_FEED as O}from"./ascii-constants.js";import{isXmlChar as d,isXmlSpaceByte as A,isAsciiDigit as b,isXmlNameStart as l,isXmlName as _}from"./utils.js";import{XmlError as o}from"./XmlError.js";var I=Object.defineProperty,m=Object.getOwnPropertySymbols,W=Object.prototype.hasOwnProperty,F=Object.prototype.propertyIsEnumerable,p=(r,t,e)=>t in r?I(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,C=(r,t)=>{for(var e in t||(t={}))W.call(t,e)&&p(r,e,t[e]);if(m)for(var e of m(t))F.call(t,e)&&p(r,e,t[e]);return r},a=(r,t,e)=>p(r,typeof t!="symbol"?t+"":t,e);class u{constructor(t,e={}){a(this,"_text"),a(this,"_pos"),a(this,"_end"),a(this,"config");const{pos:i=0,strictDocument:n=!0,allowDtd:s=!0,rawTextElements:h=null,contextSliceSize:c=30}=e;this._text=t,this._pos=i,this._end=this._text.length,this.config={strictDocument:n,allowDtd:s,rawTextElements:h,contextSliceSize:c}}clone(){return new u(this._text,C({pos:this._pos},this.config))}getPos(){return this._pos}atEnd(){return this._pos>=this._end}currCodeUnit(){if(this._pos>=this._end)throw new o({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos)}currCodeUnitUnchecked(){return this._text.charCodeAt(this._pos)}nextCodeUnit(){if(this._pos+1>=this._end)throw new o({type:"UnexpectedEndOfStream"});return this._text.charCodeAt(this._pos+1)}consumeCodeUnit(t){const e=this.currCodeUnit();if(e!==t)throw new o({type:"InvalidChar",expected:t,actual:e},this.genTextPos());this._pos+=1}tryConsumeCodeUnit(t){return this.currCodeUnit()===t?(this._pos+=1,!0):!1}consumeCodeUnitsWhile(t){const e=this._pos;return this.skipCodeUnitsWhile(t),this.sliceBack(e)}skipCodeUnitsWhile(t){for(;this._pos<this._end&&t(this.currCodeUnitUnchecked(),this);){if(!d(this.currCodeUnitUnchecked()))throw new o({type:"NonXmlChar",char:String.fromCodePoint(this.currCodeUnitUnchecked())},this.genTextPos());this._pos+=1}}advance(t){this._pos+=t}goTo(t){this._pos=t}goToEnd(){this._pos=this._end}startsWith(t){return this._text.startsWith(t,this._pos)}skipString(t){if(!this.startsWith(t))throw new o({type:"InvalidString",expected:t},this.genTextPos());this._pos+=t.length}sliceBack(t){return this._text.slice(t,this._pos)}rangeFrom(t){return{start:t,end:this._pos}}skipSpaces(){for(;this.startsWithSpace();)this._pos+=1}startsWithSpace(){return this._pos<this._end&&A(this.currCodeUnitUnchecked())}consumeSpaces(){if(this._pos>=this._end)throw new o({type:"UnexpectedEndOfStream"},this.genTextPos());if(!this.startsWithSpace())throw new o({type:"InvalidChar",expected:"a whitespace",actual:this.currCodeUnitUnchecked()},this.genTextPos());this.skipSpaces()}tryConsumeReference(){const t=this._pos,e=this.clone(),i=e.consumeReference();return i!=null?(this._pos+=e.getPos()-t,i):null}consumeReference(){if(!this.tryConsumeCodeUnit(f))return null;let t;if(this.tryConsumeCodeUnit(x)){let e,i;this.tryConsumeCodeUnit(g)?(e=this.consumeCodeUnitsWhile(s=>s>=U&&s<=w||s>=y&&s<=S||s>=E&&s<=v),i=16):(e=this.consumeCodeUnitsWhile(s=>b(s)),i=10);const n=parseInt(e,i);isNaN(n)||!d(n)?t=null:t={type:"Char",value:String.fromCodePoint(n)}}else{const e=this.consumeName();switch(e){case"quot":t={type:"Char",value:'"'};break;case"amp":t={type:"Char",value:"&"};break;case"apos":t={type:"Char",value:"'"};break;case"lt":t={type:"Char",value:"<"};break;case"gt":t={type:"Char",value:">"};break;default:t={type:"Entity",value:e}}}return this.tryConsumeCodeUnit(P)?t:null}consumeName(){const t=this._pos;this.skipName();const e=this.sliceBack(t);if(e.length===0)throw new o({type:"InvalidName"},this.genTextPosFrom(t));return e}skipName(){const t=this._pos;for(;this._pos<this._end;){const e=this.currCodeUnitUnchecked();if(this._pos===t){if(!l(e))throw new o({type:"InvalidName"},this.genTextPosFrom(t))}else if(!_(e))break;this._pos+=1}}consumeQName(){var t,e;const i=this._pos;let n=null;for(;this._pos<this._end;){const c=this.currCodeUnitUnchecked();if(c===k)if(n==null)n=this._pos,this._pos+=1;else throw new o({type:"InvalidName"},this.genTextPosFrom(i));else if(_(c))this._pos+=1;else break}let s,h;if(n!=null?(s=this._text.slice(i,n),h=this.sliceBack(n+1)):(s="",h=this.sliceBack(i)),s.length>0&&!l((t=s[0])==null?void 0:t.codePointAt(0)))throw new o({type:"InvalidName"},this.genTextPosFrom(i));if(h.length>0){if(!l((e=h[0])==null?void 0:e.codePointAt(0)))throw new o({type:"InvalidName"},this.genTextPosFrom(i))}else throw new o({type:"InvalidName"},this.genTextPosFrom(i));return[s,h]}consumeQuote(){const t=this.currCodeUnit();if(t===N||t===T)return this._pos+=1,t;throw new o({type:"InvalidChar",expected:"a quote",actual:t},this.genTextPos())}getTextAround(t,e){const i=Math.floor(e/2),n=Math.max(0,t-i),s=Math.min(this._end,t+i+e%2);return this._text.slice(n,s)}genTextPos(){return this.genTextPosFrom(this._pos)}genTextPosFrom(t){const e=Math.min(t,this._end);let i=1,n=1;for(let s=0;s<e;s++)this._text.charCodeAt(s)===O?(i++,n=1):n++;return C({row:i,col:n},this.config.contextSliceSize?{contextSlice:this.getTextAround(t,this.config.contextSliceSize)}:{})}}export{u as XmlStream};
|
|
@@ -1,35 +1 @@
|
|
|
1
|
-
const HORIZONTAL_TAB
|
|
2
|
-
const LINE_FEED = 10;
|
|
3
|
-
const CARRIAGE_RETURN = 13;
|
|
4
|
-
const SPACE = 32;
|
|
5
|
-
const EXCLAMATION_MARK = 33;
|
|
6
|
-
const DOUBLE_QUOTE = 34;
|
|
7
|
-
const HASH = 35;
|
|
8
|
-
const PERCENT = 37;
|
|
9
|
-
const AMPERSAND = 38;
|
|
10
|
-
const SINGLE_QUOTE = 39;
|
|
11
|
-
const HYPHEN = 45;
|
|
12
|
-
const PERIOD = 46;
|
|
13
|
-
const SLASH = 47;
|
|
14
|
-
const ZERO = 48;
|
|
15
|
-
const NINE = 57;
|
|
16
|
-
const COLON = 58;
|
|
17
|
-
const SEMICOLON = 59;
|
|
18
|
-
const LESS_THAN = 60;
|
|
19
|
-
const EQUALS = 61;
|
|
20
|
-
const GREATER_THAN = 62;
|
|
21
|
-
const QUESTION_MARK = 63;
|
|
22
|
-
const UPPERCASE_A = 65;
|
|
23
|
-
const UPPERCASE_F = 70;
|
|
24
|
-
const UPPERCASE_P = 80;
|
|
25
|
-
const UPPERCASE_S = 83;
|
|
26
|
-
const UPPERCASE_Z = 90;
|
|
27
|
-
const OPEN_SQUARE_BRACKET = 91;
|
|
28
|
-
const CLOSE_SQUARE_BRACKET = 93;
|
|
29
|
-
const UNDERSCORE = 95;
|
|
30
|
-
const LOWERCASE_A = 97;
|
|
31
|
-
const LOWERCASE_F = 102;
|
|
32
|
-
const LOWERCASE_X = 120;
|
|
33
|
-
const LOWERCASE_Z = 122;
|
|
34
|
-
|
|
35
|
-
export { AMPERSAND, CARRIAGE_RETURN, CLOSE_SQUARE_BRACKET, COLON, DOUBLE_QUOTE, EQUALS, EXCLAMATION_MARK, GREATER_THAN, HASH, HORIZONTAL_TAB, HYPHEN, LESS_THAN, LINE_FEED, LOWERCASE_A, LOWERCASE_F, LOWERCASE_X, LOWERCASE_Z, NINE, OPEN_SQUARE_BRACKET, PERCENT, PERIOD, QUESTION_MARK, SEMICOLON, SINGLE_QUOTE, SLASH, SPACE, UNDERSCORE, UPPERCASE_A, UPPERCASE_F, UPPERCASE_P, UPPERCASE_S, UPPERCASE_Z, ZERO };
|
|
1
|
+
const E=9,A=10,R=13,S=32,_=33,O=34,C=35,N=37,P=38,L=39,U=45,T=46,I=47,H=48,Q=57,D=58,M=59,B=60,K=61,W=62,Z=63,F=65,G=70,o=80,t=83,X=90,c=91,e=93,n=95,p=97,r=102,s=120,x=122;export{P as AMPERSAND,R as CARRIAGE_RETURN,e as CLOSE_SQUARE_BRACKET,D as COLON,O as DOUBLE_QUOTE,K as EQUALS,_ as EXCLAMATION_MARK,W as GREATER_THAN,C as HASH,E as HORIZONTAL_TAB,U as HYPHEN,B as LESS_THAN,A as LINE_FEED,p as LOWERCASE_A,r as LOWERCASE_F,s as LOWERCASE_X,x as LOWERCASE_Z,Q as NINE,c as OPEN_SQUARE_BRACKET,N as PERCENT,T as PERIOD,Z as QUESTION_MARK,M as SEMICOLON,L as SINGLE_QUOTE,I as SLASH,S as SPACE,n as UNDERSCORE,F as UPPERCASE_A,G as UPPERCASE_F,o as UPPERCASE_P,t as UPPERCASE_S,X as UPPERCASE_Z,H as ZERO};
|