@folijs/docx 0.0.0-stage → 0.4.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 +56 -0
- package/LICENSE +21 -0
- package/README.md +151 -2
- package/dist/export/content.d.ts +17 -0
- package/dist/export/content.d.ts.map +1 -0
- package/dist/export/content.js +165 -0
- package/dist/export/content.js.map +1 -0
- package/dist/export/index.d.ts +21 -0
- package/dist/export/index.d.ts.map +1 -0
- package/dist/export/index.js +360 -0
- package/dist/export/index.js.map +1 -0
- package/dist/export/parts.d.ts +24 -0
- package/dist/export/parts.d.ts.map +1 -0
- package/dist/export/parts.js +413 -0
- package/dist/export/parts.js.map +1 -0
- package/dist/export/properties.d.ts +39 -0
- package/dist/export/properties.d.ts.map +1 -0
- package/dist/export/properties.js +506 -0
- package/dist/export/properties.js.map +1 -0
- package/dist/export/xmlout.d.ts +63 -0
- package/dist/export/xmlout.d.ts.map +1 -0
- package/dist/export/xmlout.js +194 -0
- package/dist/export/xmlout.js.map +1 -0
- package/dist/import/content.d.ts +35 -0
- package/dist/import/content.d.ts.map +1 -0
- package/dist/import/content.js +370 -0
- package/dist/import/content.js.map +1 -0
- package/dist/import/index.d.ts +31 -0
- package/dist/import/index.d.ts.map +1 -0
- package/dist/import/index.js +106 -0
- package/dist/import/index.js.map +1 -0
- package/dist/import/ooxml.d.ts +39 -0
- package/dist/import/ooxml.d.ts.map +1 -0
- package/dist/import/ooxml.js +71 -0
- package/dist/import/ooxml.js.map +1 -0
- package/dist/import/parts.d.ts +35 -0
- package/dist/import/parts.d.ts.map +1 -0
- package/dist/import/parts.js +365 -0
- package/dist/import/parts.js.map +1 -0
- package/dist/import/properties.d.ts +50 -0
- package/dist/import/properties.d.ts.map +1 -0
- package/dist/import/properties.js +404 -0
- package/dist/import/properties.js.map +1 -0
- package/dist/import/report.d.ts +28 -0
- package/dist/import/report.d.ts.map +1 -0
- package/dist/import/report.js +42 -0
- package/dist/import/report.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/package.d.ts +70 -0
- package/dist/package.d.ts.map +1 -0
- package/dist/package.js +259 -0
- package/dist/package.js.map +1 -0
- package/dist/settings.d.ts +8 -0
- package/dist/settings.d.ts.map +1 -0
- package/dist/settings.js +72 -0
- package/dist/settings.js.map +1 -0
- package/dist/xml.d.ts +79 -0
- package/dist/xml.d.ts.map +1 -0
- package/dist/xml.js +384 -0
- package/dist/xml.js.map +1 -0
- package/dist/zip.d.ts +66 -0
- package/dist/zip.d.ts.map +1 -0
- package/dist/zip.js +356 -0
- package/dist/zip.js.map +1 -0
- package/package.json +36 -3
- package/src/export/content.ts +229 -0
- package/src/export/index.ts +473 -0
- package/src/export/parts.ts +545 -0
- package/src/export/properties.ts +608 -0
- package/src/export/xmlout.ts +225 -0
- package/src/import/content.ts +413 -0
- package/src/import/index.ts +139 -0
- package/src/import/ooxml.ts +78 -0
- package/src/import/parts.ts +423 -0
- package/src/import/properties.ts +507 -0
- package/src/import/report.ts +64 -0
- package/src/index.ts +24 -0
- package/src/package.ts +328 -0
- package/src/settings.ts +75 -0
- package/src/xml.ts +443 -0
- package/src/zip.ts +401 -0
package/src/xml.ts
ADDED
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An XML reader that keeps the exact source text: every node records where it is in the source
|
|
3
|
+
* string, so anything the importer does not understand is written back as the very characters
|
|
4
|
+
* it was read from (`source.slice(node.start, node.end)`), never re-serialized.
|
|
5
|
+
*
|
|
6
|
+
* XML 1.0 with namespaces, as OOXML uses it. No DTD (OOXML forbids one, ECMA-376 Part 2), so
|
|
7
|
+
* the only entities are the five predefined ones and character references.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export interface XmlName {
|
|
11
|
+
/** As written: `w:p`. */
|
|
12
|
+
readonly name: string;
|
|
13
|
+
/** `w`, or "" without a prefix. */
|
|
14
|
+
readonly prefix: string;
|
|
15
|
+
/** `p`. */
|
|
16
|
+
readonly local: string;
|
|
17
|
+
/** The namespace URI the prefix is bound to; "" for none (an unprefixed attribute, say). */
|
|
18
|
+
readonly ns: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface XmlAttribute extends XmlName {
|
|
22
|
+
/** The value with references decoded and whitespace normalized, as XML defines it. */
|
|
23
|
+
readonly value: string;
|
|
24
|
+
/** From the name to the closing quote. */
|
|
25
|
+
readonly start: number;
|
|
26
|
+
readonly end: number;
|
|
27
|
+
/** The raw value between the quotes. */
|
|
28
|
+
readonly valueStart: number;
|
|
29
|
+
readonly valueEnd: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface XmlElement extends XmlName {
|
|
33
|
+
readonly kind: "element";
|
|
34
|
+
readonly attributes: readonly XmlAttribute[];
|
|
35
|
+
readonly children: readonly XmlNode[];
|
|
36
|
+
/** `<` of the start tag. */
|
|
37
|
+
readonly start: number;
|
|
38
|
+
/** Just after the start tag's `>`. */
|
|
39
|
+
readonly openEnd: number;
|
|
40
|
+
/** `<` of the end tag; `openEnd` for an empty-element tag. */
|
|
41
|
+
readonly closeStart: number;
|
|
42
|
+
/** Just after the end tag (or the empty-element tag). */
|
|
43
|
+
readonly end: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface XmlText {
|
|
47
|
+
readonly kind: "text";
|
|
48
|
+
/** Character data with references decoded and line ends normalized. */
|
|
49
|
+
readonly text: string;
|
|
50
|
+
/** A `<![CDATA[...]]>` section. */
|
|
51
|
+
readonly cdata: boolean;
|
|
52
|
+
readonly start: number;
|
|
53
|
+
readonly end: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface XmlOther {
|
|
57
|
+
/** `<?xml ...?>`, `<?target ...?>`, `<!-- ... -->`. */
|
|
58
|
+
readonly kind: "declaration" | "instruction" | "comment";
|
|
59
|
+
readonly start: number;
|
|
60
|
+
readonly end: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export type XmlNode = XmlElement | XmlText | XmlOther;
|
|
64
|
+
|
|
65
|
+
export interface XmlDocument {
|
|
66
|
+
readonly source: string;
|
|
67
|
+
/** Everything at the top level in order: declaration, comments, whitespace, the root. */
|
|
68
|
+
readonly children: readonly XmlNode[];
|
|
69
|
+
readonly root: XmlElement;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Source that is not well-formed XML; `offset` is where in the source. */
|
|
73
|
+
export class XmlError extends Error {
|
|
74
|
+
override name = "XmlError";
|
|
75
|
+
constructor(
|
|
76
|
+
message: string,
|
|
77
|
+
readonly offset: number,
|
|
78
|
+
) {
|
|
79
|
+
super(`${message} (at ${offset})`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const XML_NS = "http://www.w3.org/XML/1998/namespace";
|
|
84
|
+
const XMLNS_NS = "http://www.w3.org/2000/xmlns/";
|
|
85
|
+
const ENTITIES: Record<string, string> = { lt: "<", gt: ">", amp: "&", quot: '"', apos: "'" };
|
|
86
|
+
|
|
87
|
+
interface Open {
|
|
88
|
+
readonly name: string;
|
|
89
|
+
readonly start: number;
|
|
90
|
+
readonly children: XmlNode[];
|
|
91
|
+
readonly scope: ReadonlyMap<string, string>;
|
|
92
|
+
readonly element: Omit<XmlElement, "closeStart" | "end" | "children">;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const ROOT_SCOPE: ReadonlyMap<string, string> = new Map([
|
|
96
|
+
["xml", XML_NS],
|
|
97
|
+
["xmlns", XMLNS_NS],
|
|
98
|
+
]);
|
|
99
|
+
|
|
100
|
+
export interface ParseXmlOptions {
|
|
101
|
+
/**
|
|
102
|
+
* Deepest element nesting accepted. Default 1000: Word's deepest structures (tables in
|
|
103
|
+
* tables, content controls) stay far below, and code walking the tree recursively is safe.
|
|
104
|
+
*/
|
|
105
|
+
readonly maxDepth?: number;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The first character XML 1.0 does not allow (outside its Char production), or -1. */
|
|
109
|
+
function illegalCharacter(source: string): number {
|
|
110
|
+
for (let i = 0; i < source.length; i++) {
|
|
111
|
+
const c = source.charCodeAt(i);
|
|
112
|
+
if (c < 0x20) {
|
|
113
|
+
if (c !== 0x09 && c !== 0x0a && c !== 0x0d) return i;
|
|
114
|
+
} else if (c >= 0xd800 && c <= 0xdbff) {
|
|
115
|
+
const next = source.charCodeAt(i + 1);
|
|
116
|
+
if (!(next >= 0xdc00 && next <= 0xdfff)) return i;
|
|
117
|
+
i++;
|
|
118
|
+
} else if ((c >= 0xdc00 && c <= 0xdfff) || c === 0xfffe || c === 0xffff) {
|
|
119
|
+
return i;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return -1;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Parses `source`; throws {@link XmlError} where it is not well-formed. */
|
|
126
|
+
export function parseXml(source: string, options: ParseXmlOptions = {}): XmlDocument {
|
|
127
|
+
const maxDepth = options.maxDepth ?? 1000;
|
|
128
|
+
if (source.charCodeAt(0) === 0xfeff) {
|
|
129
|
+
throw new XmlError("Byte order mark in the text: decode the bytes without it", 0);
|
|
130
|
+
}
|
|
131
|
+
const illegal = illegalCharacter(source);
|
|
132
|
+
if (illegal !== -1) {
|
|
133
|
+
const code = source.charCodeAt(illegal).toString(16).toUpperCase().padStart(4, "0");
|
|
134
|
+
throw new XmlError(`Character U+${code} not allowed in XML`, illegal);
|
|
135
|
+
}
|
|
136
|
+
const top: XmlNode[] = [];
|
|
137
|
+
const stack: Open[] = [];
|
|
138
|
+
let root: XmlElement | undefined;
|
|
139
|
+
let at = 0;
|
|
140
|
+
const length = source.length;
|
|
141
|
+
|
|
142
|
+
const siblings = (): XmlNode[] => stack[stack.length - 1]?.children ?? top;
|
|
143
|
+
|
|
144
|
+
while (at < length) {
|
|
145
|
+
const lt = source.indexOf("<", at);
|
|
146
|
+
const textEnd = lt === -1 ? length : lt;
|
|
147
|
+
if (textEnd > at) {
|
|
148
|
+
const raw = source.slice(at, textEnd);
|
|
149
|
+
if (stack.length === 0) {
|
|
150
|
+
if (!/^[ \t\r\n]*$/.test(raw)) throw new XmlError("Text outside the root element", at);
|
|
151
|
+
}
|
|
152
|
+
const cdataEnd = raw.indexOf("]]>");
|
|
153
|
+
if (cdataEnd !== -1) throw new XmlError('"]]>" in text', at + cdataEnd);
|
|
154
|
+
siblings().push({
|
|
155
|
+
kind: "text",
|
|
156
|
+
text: decode(raw, false, at),
|
|
157
|
+
cdata: false,
|
|
158
|
+
start: at,
|
|
159
|
+
end: textEnd,
|
|
160
|
+
});
|
|
161
|
+
at = textEnd;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (source.startsWith("<!--", at)) {
|
|
166
|
+
const close = source.indexOf("-->", at + 4);
|
|
167
|
+
if (close === -1) throw new XmlError("Unclosed comment", at);
|
|
168
|
+
const dashes = source.indexOf("--", at + 4);
|
|
169
|
+
if (dashes < close) throw new XmlError('"--" in a comment', dashes);
|
|
170
|
+
siblings().push({ kind: "comment", start: at, end: close + 3 });
|
|
171
|
+
at = close + 3;
|
|
172
|
+
} else if (source.startsWith("<![CDATA[", at)) {
|
|
173
|
+
if (stack.length === 0) throw new XmlError("CDATA outside the root element", at);
|
|
174
|
+
const close = source.indexOf("]]>", at + 9);
|
|
175
|
+
if (close === -1) throw new XmlError("Unclosed CDATA section", at);
|
|
176
|
+
siblings().push({
|
|
177
|
+
kind: "text",
|
|
178
|
+
text: source.slice(at + 9, close).replace(/\r\n?/g, "\n"),
|
|
179
|
+
cdata: true,
|
|
180
|
+
start: at,
|
|
181
|
+
end: close + 3,
|
|
182
|
+
});
|
|
183
|
+
at = close + 3;
|
|
184
|
+
} else if (source.startsWith("<!", at)) {
|
|
185
|
+
throw new XmlError("Document type declarations are not allowed", at);
|
|
186
|
+
} else if (source.startsWith("<?", at)) {
|
|
187
|
+
const close = source.indexOf("?>", at + 2);
|
|
188
|
+
if (close === -1) throw new XmlError("Unclosed processing instruction", at);
|
|
189
|
+
const target = /^[^\s?]*/.exec(source.slice(at + 2, close))?.[0] ?? "";
|
|
190
|
+
if (target === "") throw new XmlError("Processing instruction without a target", at);
|
|
191
|
+
const declaration = target.toLowerCase() === "xml";
|
|
192
|
+
if (declaration && (at !== 0 || target !== "xml")) {
|
|
193
|
+
throw new XmlError("XML declaration not at the start", at);
|
|
194
|
+
}
|
|
195
|
+
siblings().push({
|
|
196
|
+
kind: declaration ? "declaration" : "instruction",
|
|
197
|
+
start: at,
|
|
198
|
+
end: close + 2,
|
|
199
|
+
});
|
|
200
|
+
at = close + 2;
|
|
201
|
+
} else if (source.startsWith("</", at)) {
|
|
202
|
+
const nameEnd = scanName(source, at + 2);
|
|
203
|
+
const name = source.slice(at + 2, nameEnd);
|
|
204
|
+
const close = skipSpace(source, nameEnd);
|
|
205
|
+
if (source[close] !== ">") throw new XmlError("Malformed end tag", at);
|
|
206
|
+
const open = stack.pop();
|
|
207
|
+
if (!open) throw new XmlError(`End tag </${name}> without a start tag`, at);
|
|
208
|
+
if (open.name !== name)
|
|
209
|
+
throw new XmlError(`End tag </${name}> does not match <${open.name}>`, at);
|
|
210
|
+
finish(
|
|
211
|
+
{ ...open.element, children: open.children, closeStart: at, end: close + 1 },
|
|
212
|
+
open.start,
|
|
213
|
+
);
|
|
214
|
+
at = close + 1;
|
|
215
|
+
} else {
|
|
216
|
+
at = startTag(at);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
const unclosed = stack[stack.length - 1];
|
|
220
|
+
if (unclosed) throw new XmlError(`Unclosed element <${unclosed.name}>`, unclosed.start);
|
|
221
|
+
if (!root) throw new XmlError("No root element", 0);
|
|
222
|
+
return { source, children: top, root };
|
|
223
|
+
|
|
224
|
+
function finish(element: XmlElement, start: number): void {
|
|
225
|
+
if (stack.length === 0) {
|
|
226
|
+
if (root) throw new XmlError("More than one root element", start);
|
|
227
|
+
root = element;
|
|
228
|
+
}
|
|
229
|
+
siblings().push(element);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Reads a start tag at `start` (`<`); returns the offset after it. */
|
|
233
|
+
function startTag(start: number): number {
|
|
234
|
+
const nameEnd = scanName(source, start + 1);
|
|
235
|
+
const name = source.slice(start + 1, nameEnd);
|
|
236
|
+
if (name === "") throw new XmlError("Malformed start tag", start);
|
|
237
|
+
const raw: { name: string; value: string; start: number; end: number; open: number }[] = [];
|
|
238
|
+
const names = new Set<string>();
|
|
239
|
+
let at = nameEnd;
|
|
240
|
+
let empty = false;
|
|
241
|
+
for (;;) {
|
|
242
|
+
const next = skipSpace(source, at);
|
|
243
|
+
if (source[next] === ">") {
|
|
244
|
+
at = next + 1;
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
if (source.startsWith("/>", next)) {
|
|
248
|
+
at = next + 2;
|
|
249
|
+
empty = true;
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
if (next === at) throw new XmlError(`Malformed start tag <${name}>`, next);
|
|
253
|
+
const attrEnd = scanName(source, next);
|
|
254
|
+
const attrName = source.slice(next, attrEnd);
|
|
255
|
+
if (attrName === "") throw new XmlError(`Malformed attribute in <${name}>`, next);
|
|
256
|
+
const eq = skipSpace(source, attrEnd);
|
|
257
|
+
if (source[eq] !== "=") throw new XmlError(`Attribute ${attrName} without a value`, next);
|
|
258
|
+
const open = skipSpace(source, eq + 1);
|
|
259
|
+
const quote = source[open];
|
|
260
|
+
if (quote !== '"' && quote !== "'")
|
|
261
|
+
throw new XmlError(`Unquoted attribute ${attrName}`, open);
|
|
262
|
+
const close = source.indexOf(quote, open + 1);
|
|
263
|
+
if (close === -1) throw new XmlError(`Unclosed attribute ${attrName}`, open);
|
|
264
|
+
const value = source.slice(open + 1, close);
|
|
265
|
+
if (value.includes("<")) throw new XmlError(`"<" in attribute ${attrName}`, open);
|
|
266
|
+
if (names.has(attrName)) throw new XmlError(`Duplicate attribute ${attrName}`, next);
|
|
267
|
+
names.add(attrName);
|
|
268
|
+
raw.push({
|
|
269
|
+
name: attrName,
|
|
270
|
+
value: decode(value, true, open + 1),
|
|
271
|
+
start: next,
|
|
272
|
+
end: close + 1,
|
|
273
|
+
open,
|
|
274
|
+
});
|
|
275
|
+
at = close + 1;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const parentScope = stack[stack.length - 1]?.scope ?? ROOT_SCOPE;
|
|
279
|
+
let scope = parentScope;
|
|
280
|
+
for (const a of raw) {
|
|
281
|
+
const declared =
|
|
282
|
+
a.name === "xmlns" ? "" : a.name.startsWith("xmlns:") ? a.name.slice(6) : undefined;
|
|
283
|
+
if (declared === undefined) continue;
|
|
284
|
+
// The reserved prefixes and their namespaces (Namespaces in XML 1.0, section 3).
|
|
285
|
+
const reserved =
|
|
286
|
+
declared === "xml"
|
|
287
|
+
? a.value !== XML_NS
|
|
288
|
+
: declared === "xmlns" || a.value === XML_NS || a.value === XMLNS_NS;
|
|
289
|
+
if (reserved) throw new XmlError(`Reserved namespace declaration ${a.name}`, a.start);
|
|
290
|
+
if (scope === parentScope) scope = new Map(parentScope);
|
|
291
|
+
(scope as Map<string, string>).set(declared, a.value);
|
|
292
|
+
}
|
|
293
|
+
const expanded = new Set<string>();
|
|
294
|
+
const attributes = raw.map((a) => {
|
|
295
|
+
const qualified = splitName(a.name, a.start);
|
|
296
|
+
const ns =
|
|
297
|
+
a.name === "xmlns" || qualified.prefix === "xmlns"
|
|
298
|
+
? XMLNS_NS
|
|
299
|
+
: qualified.prefix === ""
|
|
300
|
+
? ""
|
|
301
|
+
: resolve(scope, qualified.prefix, a.start);
|
|
302
|
+
if (ns !== "") {
|
|
303
|
+
const key = `${ns}\u0000${qualified.local}`;
|
|
304
|
+
if (expanded.has(key)) throw new XmlError(`Duplicate attribute ${a.name}`, a.start);
|
|
305
|
+
expanded.add(key);
|
|
306
|
+
}
|
|
307
|
+
return {
|
|
308
|
+
...qualified,
|
|
309
|
+
ns,
|
|
310
|
+
value: a.value,
|
|
311
|
+
start: a.start,
|
|
312
|
+
end: a.end,
|
|
313
|
+
valueStart: a.open + 1,
|
|
314
|
+
valueEnd: a.end - 1,
|
|
315
|
+
};
|
|
316
|
+
});
|
|
317
|
+
const qualified = splitName(name, start);
|
|
318
|
+
if (qualified.prefix === "xmlns") throw new XmlError(`Reserved prefix in <${name}>`, start);
|
|
319
|
+
if (stack.length >= maxDepth) {
|
|
320
|
+
throw new XmlError(`Elements nested deeper than ${maxDepth}`, start);
|
|
321
|
+
}
|
|
322
|
+
const element = {
|
|
323
|
+
kind: "element" as const,
|
|
324
|
+
...qualified,
|
|
325
|
+
ns: resolve(scope, qualified.prefix, start),
|
|
326
|
+
attributes,
|
|
327
|
+
start,
|
|
328
|
+
openEnd: at,
|
|
329
|
+
};
|
|
330
|
+
if (empty) {
|
|
331
|
+
finish({ ...element, children: [], closeStart: at, end: at }, start);
|
|
332
|
+
} else {
|
|
333
|
+
stack.push({ name, start, children: [], scope, element });
|
|
334
|
+
}
|
|
335
|
+
return at;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function splitName(name: string, at: number): { name: string; prefix: string; local: string } {
|
|
340
|
+
const colon = name.indexOf(":");
|
|
341
|
+
const prefix = colon === -1 ? "" : name.slice(0, colon);
|
|
342
|
+
const local = colon === -1 ? name : name.slice(colon + 1);
|
|
343
|
+
// A name part cannot start with a digit, "-" or "." (XML's NameStartChar), nor be empty.
|
|
344
|
+
const bad = (part: string) => part === "" || /^[-.0-9·]/.test(part);
|
|
345
|
+
if ((colon !== -1 && bad(prefix)) || bad(local) || local.includes(":")) {
|
|
346
|
+
throw new XmlError(`Malformed qualified name ${name}`, at);
|
|
347
|
+
}
|
|
348
|
+
return { name, prefix, local };
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function resolve(scope: ReadonlyMap<string, string>, prefix: string, at: number): string {
|
|
352
|
+
const ns = scope.get(prefix);
|
|
353
|
+
if (ns === undefined) {
|
|
354
|
+
if (prefix === "") return "";
|
|
355
|
+
throw new XmlError(`Unbound namespace prefix ${prefix}`, at);
|
|
356
|
+
}
|
|
357
|
+
if (ns === "" && prefix !== "") throw new XmlError(`Unbound namespace prefix ${prefix}`, at);
|
|
358
|
+
return ns;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** The end of the name starting at `at` (lenient: anything but delimiters). */
|
|
362
|
+
function scanName(source: string, at: number): number {
|
|
363
|
+
let i = at;
|
|
364
|
+
while (i < source.length) {
|
|
365
|
+
const c = source.charCodeAt(i);
|
|
366
|
+
// whitespace / > = < " ' & ? !
|
|
367
|
+
if (
|
|
368
|
+
c === 0x20 ||
|
|
369
|
+
c === 0x09 ||
|
|
370
|
+
c === 0x0a ||
|
|
371
|
+
c === 0x0d ||
|
|
372
|
+
c === 0x2f ||
|
|
373
|
+
c === 0x3e ||
|
|
374
|
+
c === 0x3d ||
|
|
375
|
+
c === 0x3c ||
|
|
376
|
+
c === 0x22 ||
|
|
377
|
+
c === 0x27 ||
|
|
378
|
+
c === 0x26 ||
|
|
379
|
+
c === 0x3f ||
|
|
380
|
+
c === 0x21
|
|
381
|
+
) {
|
|
382
|
+
break;
|
|
383
|
+
}
|
|
384
|
+
i++;
|
|
385
|
+
}
|
|
386
|
+
return i;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function skipSpace(source: string, at: number): number {
|
|
390
|
+
let i = at;
|
|
391
|
+
while (i < source.length) {
|
|
392
|
+
const c = source.charCodeAt(i);
|
|
393
|
+
if (c !== 0x20 && c !== 0x09 && c !== 0x0a && c !== 0x0d) break;
|
|
394
|
+
i++;
|
|
395
|
+
}
|
|
396
|
+
return i;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Decodes references and normalizes line ends (`\r\n` and `\r` to `\n`); in an attribute value
|
|
401
|
+
* literal tabs and line ends become spaces (references to them stay).
|
|
402
|
+
*/
|
|
403
|
+
function decode(raw: string, attribute: boolean, offset: number): string {
|
|
404
|
+
if (!raw.includes("&") && !raw.includes("\r") && !(attribute && /[\t\n]/.test(raw))) return raw;
|
|
405
|
+
let out = "";
|
|
406
|
+
let i = 0;
|
|
407
|
+
while (i < raw.length) {
|
|
408
|
+
const c = raw[i] as string;
|
|
409
|
+
if (c === "&") {
|
|
410
|
+
const semi = raw.indexOf(";", i + 1);
|
|
411
|
+
if (semi === -1) throw new XmlError('"&" without a reference', offset + i);
|
|
412
|
+
out += reference(raw.slice(i + 1, semi), offset + i);
|
|
413
|
+
i = semi + 1;
|
|
414
|
+
} else if (c === "\r") {
|
|
415
|
+
out += attribute ? " " : "\n";
|
|
416
|
+
i += raw[i + 1] === "\n" ? 2 : 1;
|
|
417
|
+
} else {
|
|
418
|
+
out += attribute && (c === "\t" || c === "\n") ? " " : c;
|
|
419
|
+
i++;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
return out;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function reference(name: string, at: number): string {
|
|
426
|
+
const entity = ENTITIES[name];
|
|
427
|
+
if (entity !== undefined) return entity;
|
|
428
|
+
const code = /^#x[0-9a-fA-F]+$/.test(name)
|
|
429
|
+
? Number.parseInt(name.slice(2), 16)
|
|
430
|
+
: /^#[0-9]+$/.test(name)
|
|
431
|
+
? Number.parseInt(name.slice(1), 10)
|
|
432
|
+
: -1;
|
|
433
|
+
if (code === -1) throw new XmlError(`Unknown entity &${name};`, at);
|
|
434
|
+
const legal =
|
|
435
|
+
code === 0x9 ||
|
|
436
|
+
code === 0xa ||
|
|
437
|
+
code === 0xd ||
|
|
438
|
+
(code >= 0x20 && code <= 0xd7ff) ||
|
|
439
|
+
(code >= 0xe000 && code <= 0xfffd) ||
|
|
440
|
+
(code >= 0x10000 && code <= 0x10ffff);
|
|
441
|
+
if (!legal) throw new XmlError(`Character reference &${name}; to an illegal character`, at);
|
|
442
|
+
return String.fromCodePoint(code);
|
|
443
|
+
}
|