@stll/docx-core 0.43.1 → 0.45.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/dist/{document-CaGfolcU.d.ts → document-BKejiFuk.d.ts} +383 -250
- package/dist/{document-CEMwM5On.js → document-BuoCvTcR.js} +44 -50
- package/dist/docx_kernel_bg.wasm +0 -0
- package/dist/index.d.ts +15 -2
- package/dist/index.js +202 -53
- package/dist/listItems-bPpOKnLB.js +116 -0
- package/dist/model/document.d.ts +16 -3
- package/dist/model/document.js +1 -1
- package/dist/ops/documentOps.d.ts +1183 -111
- package/dist/ops/documentOps.js +34619 -16484
- package/dist/preservedMarkup-BWpGhchw.d.ts +79 -0
- package/dist/projection.js +77 -78
- package/dist/schema/sequenceChildren.d.ts +1 -1
- package/dist/schema/sequenceChildren.js +1 -1
- package/dist/{sequenceChildren-BEcOm3tR.js → sequenceChildren-ESF-Ei5K.js} +1 -1
- package/dist/{streamingXmlParser-GnKxUuw_.js → streamingXmlParser-BqnCIEZd.js} +288 -156
- package/dist/xml/streamingXmlParser.d.ts +74 -12
- package/dist/xml/streamingXmlParser.js +1 -1
- package/dist/xml/xmlNamespaceContext.d.ts +8 -0
- package/dist/xml/xmlNamespaceContext.js +3 -1
- package/dist/zip/archive.d.ts +1 -1
- package/package.json +3 -3
- package/dist/href-ogmJmncc.js +0 -138
- package/dist/preservedMarkup-9L4lbGnE.d.ts +0 -66
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EMPTY_NAMESPACE_SCOPE, attachXmlNamespaceContext } from "./xml/xmlNamespaceContext.js";
|
|
1
|
+
import { EMPTY_NAMESPACE_SCOPE, OOXML_NS, attachXmlNamespaceContext, resolveNamespaceUri } from "./xml/xmlNamespaceContext.js";
|
|
2
2
|
import { FOLIO_XML_RESOURCE_LIMITS } from "./xml/xmlResourceLimits.js";
|
|
3
3
|
import { TaggedError } from "better-result";
|
|
4
4
|
// src/serialize/numericIdNames.ts
|
|
@@ -26,6 +26,96 @@ const isOoxmlNumericIdAttributeName = ({ elementName, attributeName }) => {
|
|
|
26
26
|
const attribute = localName(attributeName);
|
|
27
27
|
return attribute === "id" || NUMBERING_ATTRIBUTES.has(attribute) || attribute === "val" && ID_VALUE_ELEMENTS.has(localName(elementName));
|
|
28
28
|
};
|
|
29
|
+
// src/serialize/xmlEscape.ts
|
|
30
|
+
/**
|
|
31
|
+
* The one owner of XML character escaping for every part folio writes.
|
|
32
|
+
*
|
|
33
|
+
* Two contexts, two functions. A value going into element content takes
|
|
34
|
+
* {@link escapeXmlText}; a value going between the quotes of an attribute
|
|
35
|
+
* takes {@link escapeXmlAttribute}. Nothing else escapes XML by hand: a
|
|
36
|
+
* second escaper always ends up disagreeing with this one about some
|
|
37
|
+
* character, and the disagreement is a package Word refuses to open.
|
|
38
|
+
*
|
|
39
|
+
* Both functions guarantee the same contract: **the result is always legal
|
|
40
|
+
* XML 1.0 character data.** That is what makes the contract checkable — a
|
|
41
|
+
* caller cannot produce a corrupt part by forgetting something.
|
|
42
|
+
*
|
|
43
|
+
* XML 1.0 §2.2 `Char` admits tab, LF, CR, `#x20`-`#xD7FF`, `#xE000`-`#xFFFD`
|
|
44
|
+
* and the astral planes. Everything else — the rest of C0, `#xFFFE`,
|
|
45
|
+
* `#xFFFF`, an unpaired surrogate — cannot appear in a document at all, and
|
|
46
|
+
* cannot be escaped into one either: `�` is as illegal as a raw NUL
|
|
47
|
+
* (§4.1 requires a character reference to refer to a legal `Char`). The only
|
|
48
|
+
* output that is well-formed is one without them, so they are dropped here.
|
|
49
|
+
* Callers that own an input boundary sanitise before this point, with
|
|
50
|
+
* `sanitizeXmlCharacters`, so a user-visible value loses its illegal
|
|
51
|
+
* characters where something can still report it; the drop below is the last
|
|
52
|
+
* line of defence for a boundary nobody has covered yet.
|
|
53
|
+
*
|
|
54
|
+
* The whitespace rules are where the two contexts part:
|
|
55
|
+
*
|
|
56
|
+
* - §3.3.3 normalises tab, LF and CR in an **attribute value** to a space in
|
|
57
|
+
* every conformant parser, so a literal one does not survive a save. They
|
|
58
|
+
* are written as character references, which the same step leaves alone.
|
|
59
|
+
* - §2.11 normalises a literal CR (and CRLF) in **content** to LF on read, so
|
|
60
|
+
* CR is written as a reference there too. Tab and LF survive as themselves.
|
|
61
|
+
*
|
|
62
|
+
* Both contexts escape all five of `& < > " '`. Only `&` and `<` must be
|
|
63
|
+
* escaped in content, but folio has always written the full set, `>` closes
|
|
64
|
+
* the `]]>` hazard of §2.4 without a special case, and one lexical form
|
|
65
|
+
* across every writer is worth more than the bytes it costs. Every spelling
|
|
66
|
+
* here reads back as the character it stands for, so the rule preserves the
|
|
67
|
+
* value whichever context it lands in.
|
|
68
|
+
*/
|
|
69
|
+
/** XML 1.0 §2.2 `Char`, negated: the characters no XML document may contain. */
|
|
70
|
+
const ILLEGAL_XML_CHARACTER_CLASS = "[^\\u0009\\u000A\\u000D\\u0020-\\uD7FF\\uE000-\\uFFFD\\u{10000}-\\u{10FFFF}]";
|
|
71
|
+
/**
|
|
72
|
+
* Unicode-mode classes match code points, so a well-formed surrogate pair is a
|
|
73
|
+
* single astral code point and cannot match a BMP class: this matches exactly
|
|
74
|
+
* the unpaired surrogates.
|
|
75
|
+
*/
|
|
76
|
+
const LONE_SURROGATE_PATTERN = /[\uD800-\uDFFF]/gu;
|
|
77
|
+
const ILLEGAL_XML_CHARACTER_PATTERN = new RegExp(ILLEGAL_XML_CHARACTER_CLASS, "gu");
|
|
78
|
+
const ILLEGAL_XML_CHARACTER_PROBE = new RegExp(ILLEGAL_XML_CHARACTER_CLASS, "u");
|
|
79
|
+
/** What an unpaired surrogate becomes at an input boundary: U+FFFD. */
|
|
80
|
+
const REPLACEMENT_CHARACTER = "�";
|
|
81
|
+
const TEXT_ESCAPES = {
|
|
82
|
+
"&": "&",
|
|
83
|
+
"<": "<",
|
|
84
|
+
">": ">",
|
|
85
|
+
"\"": """,
|
|
86
|
+
"'": "'",
|
|
87
|
+
"\r": " "
|
|
88
|
+
};
|
|
89
|
+
const ATTRIBUTE_ESCAPES = {
|
|
90
|
+
...TEXT_ESCAPES,
|
|
91
|
+
" ": "	",
|
|
92
|
+
"\n": " "
|
|
93
|
+
};
|
|
94
|
+
const TEXT_PROBE = new RegExp(`[&<>"'\\r]|${ILLEGAL_XML_CHARACTER_CLASS}`, "u");
|
|
95
|
+
const TEXT_PATTERN = new RegExp(`[&<>"'\\r]|${ILLEGAL_XML_CHARACTER_CLASS}`, "gu");
|
|
96
|
+
const ATTRIBUTE_PROBE = new RegExp(`[&<>"'\\t\\n\\r]|${ILLEGAL_XML_CHARACTER_CLASS}`, "u");
|
|
97
|
+
const ATTRIBUTE_PATTERN = new RegExp(`[&<>"'\\t\\n\\r]|${ILLEGAL_XML_CHARACTER_CLASS}`, "gu");
|
|
98
|
+
/** Escape a value for XML element content. Tab and LF survive as themselves. */
|
|
99
|
+
const escapeXmlText = (value) => TEXT_PROBE.test(value) ? value.replace(TEXT_PATTERN, (character) => TEXT_ESCAPES[character] ?? "") : value;
|
|
100
|
+
/**
|
|
101
|
+
* Escape a value for an XML attribute value, of either quoting. Tab, LF and CR
|
|
102
|
+
* become character references so attribute-value normalisation cannot flatten
|
|
103
|
+
* them to spaces.
|
|
104
|
+
*/
|
|
105
|
+
const escapeXmlAttribute = (value) => ATTRIBUTE_PROBE.test(value) ? value.replace(ATTRIBUTE_PATTERN, (character) => ATTRIBUTE_ESCAPES[character] ?? "") : value;
|
|
106
|
+
/** Whether `value` holds a character XML 1.0 cannot represent at all. */
|
|
107
|
+
const hasIllegalXmlCharacters = (value) => ILLEGAL_XML_CHARACTER_PROBE.test(value);
|
|
108
|
+
/**
|
|
109
|
+
* Make a value from outside folio representable in XML: an unpaired surrogate
|
|
110
|
+
* becomes U+FFFD, every other character XML cannot hold is dropped.
|
|
111
|
+
*
|
|
112
|
+
* This is the rule for an input boundary — paste, a document operation, a
|
|
113
|
+
* markdown or legal-source compile, a template fill — where the value is still
|
|
114
|
+
* attached to the request that carried it and a warning still has somewhere to
|
|
115
|
+
* go. `escapeXmlText` and `escapeXmlAttribute` apply the same rule again
|
|
116
|
+
* further down, but by then nobody can say which input lost a character.
|
|
117
|
+
*/
|
|
118
|
+
const sanitizeXmlCharacters = (value) => hasIllegalXmlCharacters(value) ? value.replace(LONE_SURROGATE_PATTERN, REPLACEMENT_CHARACTER).replace(ILLEGAL_XML_CHARACTER_PATTERN, "") : value;
|
|
29
119
|
// src/xml/streamingXmlParser.ts
|
|
30
120
|
/**
|
|
31
121
|
* folio's one XML reader: a single forward pass that builds the shared
|
|
@@ -35,17 +125,17 @@ const isOoxmlNumericIdAttributeName = ({ elementName, attributeName }) => {
|
|
|
35
125
|
* accepts:
|
|
36
126
|
*
|
|
37
127
|
* - `ooxml`: the ordinary contract the DOCX readers parse with. The five
|
|
38
|
-
* built-in entities and character references
|
|
39
|
-
* other
|
|
40
|
-
* nesting past {@link FOLIO_XML_RESOURCE_LIMITS}
|
|
128
|
+
* built-in entities and character references to characters XML admits are
|
|
129
|
+
* decoded; a DOCTYPE, any other reference, mismatched or unclosed markup,
|
|
130
|
+
* text outside the root and nesting past {@link FOLIO_XML_RESOURCE_LIMITS}
|
|
131
|
+
* are refused.
|
|
41
132
|
* - `well-formed`: XML 1.0 well-formedness on top of that, for validators. One
|
|
42
133
|
* root element, valid names, no repeated attribute, no `<` in an attribute
|
|
43
|
-
* value,
|
|
44
|
-
*
|
|
45
|
-
* - `tolerant`: `ooxml`, except that text outside the root is dropped and
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* literal), so a part those releases opened still opens with the same text.
|
|
134
|
+
* value, comments and processing instructions in their lexical form, and no
|
|
135
|
+
* characters XML excludes.
|
|
136
|
+
* - `tolerant`: `ooxml`, except that text outside the root is dropped and an
|
|
137
|
+
* `&` that begins no reference is read as text. A reference XML refuses is
|
|
138
|
+
* refused, as in `ooxml`.
|
|
49
139
|
*
|
|
50
140
|
* `references: "raw"` keeps each reference as written instead of decoding it,
|
|
51
141
|
* for readers that compare or re-emit source spellings.
|
|
@@ -63,18 +153,38 @@ const BUILT_IN_ENTITIES = {
|
|
|
63
153
|
const XmlReadErrorBase = TaggedError("XmlReadError");
|
|
64
154
|
/** An XML part the reader refused under the conformance its caller asked for. */
|
|
65
155
|
var XmlReadError = class extends XmlReadErrorBase {};
|
|
156
|
+
const MalformedXmlReferenceErrorBase = TaggedError("MalformedXmlReferenceError");
|
|
157
|
+
/**
|
|
158
|
+
* A package part holds a reference XML refuses, so it is not well-formed XML
|
|
159
|
+
* and is not opened.
|
|
160
|
+
*/
|
|
161
|
+
var MalformedXmlReferenceError = class extends MalformedXmlReferenceErrorBase {};
|
|
66
162
|
const reject = (reason) => ({
|
|
67
163
|
status: "unsupported",
|
|
68
|
-
reason
|
|
164
|
+
reason,
|
|
165
|
+
refusal: { type: "markup" }
|
|
69
166
|
});
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if (
|
|
74
|
-
|
|
75
|
-
return
|
|
167
|
+
/** A refused text or attribute value, naming the reference XML refuses when that is why. */
|
|
168
|
+
const refuseValue = ({ raw, start, reason }) => {
|
|
169
|
+
const malformed = firstMalformedReference(raw);
|
|
170
|
+
if (malformed === null) return reject(reason);
|
|
171
|
+
const offset = start + malformed.index;
|
|
172
|
+
return {
|
|
173
|
+
status: "unsupported",
|
|
174
|
+
reason: `${describeMalformedReference(malformed)} at offset ${String(offset)}`,
|
|
175
|
+
refusal: {
|
|
176
|
+
type: "malformedReference",
|
|
177
|
+
kind: malformed.kind,
|
|
178
|
+
offset,
|
|
179
|
+
reference: malformed.reference
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
const valueReaderFor = ({ conformance, references }) => {
|
|
184
|
+
if (conformance === "tolerant") return references === "decode" ? (value) => firstMalformedReference(value) === null ? decodeTolerantReferences(normalizeLineEndings(value)) : null : (value) => firstMalformedReference(value) === null ? normalizeLineEndings(value) : null;
|
|
185
|
+
return references === "decode" ? (value) => decodeReferences(normalizeLineEndings(value)) : (value) => {
|
|
76
186
|
const normalized = normalizeLineEndings(value);
|
|
77
|
-
return decodeReferences(normalized
|
|
187
|
+
return decodeReferences(normalized) === null ? null : normalized;
|
|
78
188
|
};
|
|
79
189
|
};
|
|
80
190
|
const readRulesFor = (policy) => ({
|
|
@@ -83,20 +193,36 @@ const readRulesFor = (policy) => ({
|
|
|
83
193
|
validNames: policy.conformance === "well-formed" ? /* @__PURE__ */ new Set() : void 0
|
|
84
194
|
});
|
|
85
195
|
let defaultRules;
|
|
196
|
+
const unboundPrefixOf = (name, scope) => {
|
|
197
|
+
const colon = name.indexOf(":");
|
|
198
|
+
if (colon <= 0) return void 0;
|
|
199
|
+
const prefix = name.slice(0, colon);
|
|
200
|
+
return resolveNamespaceUri(scope, prefix) ? void 0 : prefix;
|
|
201
|
+
};
|
|
202
|
+
const firstUnboundPrefix = ({ name, namespaceUri, attributes }, scope) => {
|
|
203
|
+
if (!namespaceUri && name !== void 0) {
|
|
204
|
+
const colon = name.indexOf(":");
|
|
205
|
+
if (colon > 0) return name.slice(0, colon);
|
|
206
|
+
}
|
|
207
|
+
for (const attributeName of Object.keys(attributes ?? {})) {
|
|
208
|
+
if (attributeName.startsWith("xmlns:")) continue;
|
|
209
|
+
const prefix = unboundPrefixOf(attributeName, scope);
|
|
210
|
+
if (prefix !== void 0) return prefix;
|
|
211
|
+
}
|
|
212
|
+
};
|
|
86
213
|
const parseStreamingXmlInternal = (options) => {
|
|
87
214
|
const { xml, inheritedNamespaceScope = EMPTY_NAMESPACE_SCOPE } = options;
|
|
88
|
-
const rules = options.conformance === void 0 && options.references === void 0
|
|
215
|
+
const rules = options.conformance === void 0 && options.references === void 0 ? defaultRules ??= readRulesFor({
|
|
89
216
|
conformance: "ooxml",
|
|
90
|
-
references: "decode"
|
|
91
|
-
undeclaredEntities: "refuse"
|
|
217
|
+
references: "decode"
|
|
92
218
|
}) : readRulesFor({
|
|
93
219
|
conformance: options.conformance ?? "ooxml",
|
|
94
|
-
references: options.references ?? "decode"
|
|
95
|
-
undeclaredEntities: options.undeclaredEntities ?? "refuse"
|
|
220
|
+
references: options.references ?? "decode"
|
|
96
221
|
});
|
|
97
222
|
const wellFormed = rules.conformance === "well-formed";
|
|
223
|
+
const requireBoundPrefixes = options.mode === "verdict" && options.namespaces === "require-bound-prefixes";
|
|
98
224
|
const retainInstructions = options.instructions === "retain";
|
|
99
|
-
if (wellFormed &&
|
|
225
|
+
if (wellFormed && hasIllegalXmlCharacters(xml)) return reject("a character XML does not allow");
|
|
100
226
|
const visitOpenTag = options.visitOpenTag;
|
|
101
227
|
let spanMode = "none";
|
|
102
228
|
if (visitOpenTag !== void 0) spanMode = options.mode === "tree" ? "identities" : "all";
|
|
@@ -122,13 +248,22 @@ const parseStreamingXmlInternal = (options) => {
|
|
|
122
248
|
const decoded = rules.readValue(text);
|
|
123
249
|
return decoded !== null && (stack.length > 0 || isXmlWhitespaceText(decoded) || rules.conformance === "tolerant");
|
|
124
250
|
};
|
|
251
|
+
const refuseText = (raw, start) => rules.readValue(raw) === null ? refuseValue({
|
|
252
|
+
raw,
|
|
253
|
+
start,
|
|
254
|
+
reason: "text the reader does not accept"
|
|
255
|
+
}) : reject("text the reader does not accept");
|
|
125
256
|
while (cursor < xml.length) {
|
|
126
257
|
const open = xml.indexOf("<", cursor);
|
|
127
258
|
if (open === -1) {
|
|
128
|
-
|
|
259
|
+
const text = xml.slice(cursor);
|
|
260
|
+
if (!acceptText(text, mergeAdjacentText)) return refuseText(text, cursor);
|
|
129
261
|
break;
|
|
130
262
|
}
|
|
131
|
-
if (open > cursor
|
|
263
|
+
if (open > cursor) {
|
|
264
|
+
const text = xml.slice(cursor, open);
|
|
265
|
+
if (!acceptText(text, mergeAdjacentText)) return refuseText(text, cursor);
|
|
266
|
+
}
|
|
132
267
|
if (open > cursor) mergeAdjacentText = true;
|
|
133
268
|
if (xml.startsWith("<!--", open)) {
|
|
134
269
|
const close = xml.indexOf("-->", open + 4);
|
|
@@ -198,8 +333,14 @@ const parseStreamingXmlInternal = (options) => {
|
|
|
198
333
|
}
|
|
199
334
|
const parent = stack.at(-1)?.element;
|
|
200
335
|
const scope = parent?.namespaceScope ?? inheritedNamespaceScope;
|
|
201
|
-
if (options.mode === "verdict") {} else if (options.mode === "attributes" && options.retainAttribute !== void 0 && parsedTag.element.attributes === void 0) parsedTag.element.namespaceScope = scope;
|
|
202
|
-
else
|
|
336
|
+
if (options.mode === "verdict" && !requireBoundPrefixes) {} else if (options.mode === "attributes" && options.retainAttribute !== void 0 && parsedTag.element.attributes === void 0) parsedTag.element.namespaceScope = scope;
|
|
337
|
+
else {
|
|
338
|
+
const elementScope = attachXmlNamespaceContext(parsedTag.element, scope);
|
|
339
|
+
if (requireBoundPrefixes) {
|
|
340
|
+
const prefix = firstUnboundPrefix(parsedTag.element, elementScope);
|
|
341
|
+
if (prefix !== void 0) return reject(`an unbound namespace prefix ${prefix}`);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
203
344
|
options.visitElement?.({
|
|
204
345
|
element: parsedTag.element,
|
|
205
346
|
attributeValueSpans: parsedTag.attributeValueSpans ?? /* @__PURE__ */ new Map(),
|
|
@@ -241,13 +382,12 @@ const parseStreamingXmlInternal = (options) => {
|
|
|
241
382
|
};
|
|
242
383
|
};
|
|
243
384
|
/** Read `xml` into a tree under the stated conformance, in one pass. */
|
|
244
|
-
const readXml = (xml, { conformance, references = "decode",
|
|
385
|
+
const readXml = (xml, { conformance, references = "decode", instructions = "skip", inheritedNamespaceScope = EMPTY_NAMESPACE_SCOPE }) => {
|
|
245
386
|
const parsed = parseStreamingXmlInternal({
|
|
246
387
|
xml,
|
|
247
388
|
mode: "tree",
|
|
248
389
|
conformance,
|
|
249
390
|
references,
|
|
250
|
-
undeclaredEntities,
|
|
251
391
|
instructions,
|
|
252
392
|
inheritedNamespaceScope
|
|
253
393
|
});
|
|
@@ -256,11 +396,40 @@ const readXml = (xml, { conformance, references = "decode", undeclaredEntities =
|
|
|
256
396
|
value: parsed.value
|
|
257
397
|
} : {
|
|
258
398
|
status: "rejected",
|
|
259
|
-
reason: parsed.reason
|
|
399
|
+
reason: parsed.reason,
|
|
400
|
+
refusal: parsed.refusal
|
|
260
401
|
};
|
|
261
402
|
};
|
|
262
403
|
/**
|
|
404
|
+
* The refusal for the first reference in `xml` XML refuses, outside comments,
|
|
405
|
+
* CDATA sections and processing instructions, or `null`. A part with none, or
|
|
406
|
+
* one refused first for its markup, answers `null`.
|
|
407
|
+
*/
|
|
408
|
+
const malformedXmlReferenceError = (part, xml) => {
|
|
409
|
+
if (firstMalformedReference(xml) === null) return null;
|
|
410
|
+
const parsed = parseStreamingXmlInternal({
|
|
411
|
+
xml,
|
|
412
|
+
mode: "verdict",
|
|
413
|
+
conformance: "tolerant",
|
|
414
|
+
references: "raw"
|
|
415
|
+
});
|
|
416
|
+
if (parsed.status === "parsed" || parsed.refusal.type !== "malformedReference") return null;
|
|
417
|
+
const { kind, offset, reference } = parsed.refusal;
|
|
418
|
+
return new MalformedXmlReferenceError({
|
|
419
|
+
message: `${part} refers to ${describeMalformedReference({
|
|
420
|
+
kind,
|
|
421
|
+
reference
|
|
422
|
+
})} at offset ${String(offset)}; it is not well-formed XML.`,
|
|
423
|
+
kind,
|
|
424
|
+
part,
|
|
425
|
+
offset,
|
|
426
|
+
reference
|
|
427
|
+
});
|
|
428
|
+
};
|
|
429
|
+
/**
|
|
263
430
|
* The reason `xml` is not well-formed XML, or `null` when it is.
|
|
431
|
+
* `require-bound-prefixes` also checks the in-scope bindings of element and
|
|
432
|
+
* attribute names.
|
|
264
433
|
*
|
|
265
434
|
* Stricter than XML 1.0 in one respect: an element or attribute named like a
|
|
266
435
|
* built-in object property (`__proto__`, `constructor`, `toString`, ...) is
|
|
@@ -268,13 +437,13 @@ const readXml = (xml, { conformance, references = "decode", undeclaredEntities =
|
|
|
268
437
|
* plain objects by name and refuses it too; a part this accepts is one they
|
|
269
438
|
* can read.
|
|
270
439
|
*/
|
|
271
|
-
const wellFormedXmlIssue = (xml,
|
|
440
|
+
const wellFormedXmlIssue = (xml, options) => {
|
|
272
441
|
const parsed = parseStreamingXmlInternal({
|
|
273
442
|
xml,
|
|
274
443
|
mode: "verdict",
|
|
275
444
|
conformance: "well-formed",
|
|
276
445
|
references: "raw",
|
|
277
|
-
|
|
446
|
+
...options
|
|
278
447
|
});
|
|
279
448
|
return parsed.status === "parsed" ? null : parsed.reason;
|
|
280
449
|
};
|
|
@@ -356,12 +525,37 @@ const scanStreamingXmlNumericIdAttributes = (xml, visitOpenTag) => {
|
|
|
356
525
|
};
|
|
357
526
|
/** Visit namespace-resolved start tags and source spans without building a tree. */
|
|
358
527
|
const scanStreamingXmlElements = (xml, visitElement) => {
|
|
359
|
-
|
|
528
|
+
const parsed = parseStreamingXmlInternal({
|
|
360
529
|
xml,
|
|
361
530
|
mode: "attributes",
|
|
362
531
|
visitOpenTag: () => null,
|
|
363
532
|
visitElement
|
|
364
|
-
})
|
|
533
|
+
});
|
|
534
|
+
return parsed.status === "parsed" ? { status: "scanned" } : {
|
|
535
|
+
status: "unsupported",
|
|
536
|
+
refusal: parsed.refusal
|
|
537
|
+
};
|
|
538
|
+
};
|
|
539
|
+
const DRAWING_MAIN_BY_WORDPROCESSING_DRAWING = /* @__PURE__ */ new Map([[OOXML_NS.wp, OOXML_NS.a], ["http://purl.oclc.org/ooxml/drawingml/wordprocessingDrawing", "http://purl.oclc.org/ooxml/drawingml/main"]]);
|
|
540
|
+
/** Restore the namespace of an unbound graphic inside a WordprocessingDrawing anchor or inline. */
|
|
541
|
+
const repairMissingDrawingNamespace = (xml) => {
|
|
542
|
+
const chunks = [];
|
|
543
|
+
let cursor = 0;
|
|
544
|
+
if (scanStreamingXmlElements(xml, ({ element, parent, nameEnd }) => {
|
|
545
|
+
if (element.name === void 0 || element.namespaceUri !== void 0 || parent?.name === void 0 || parent.namespaceUri === void 0) return;
|
|
546
|
+
const separator = element.name.indexOf(":");
|
|
547
|
+
if (separator < 1 || element.name.slice(separator + 1) !== "graphic") return;
|
|
548
|
+
const prefix = element.name.slice(0, separator);
|
|
549
|
+
if (prefix === "xmlns" || !XML_NAME.test(prefix)) return;
|
|
550
|
+
const localName = parent.name.slice(parent.name.indexOf(":") + 1);
|
|
551
|
+
if (localName !== "anchor" && localName !== "inline") return;
|
|
552
|
+
const namespace = DRAWING_MAIN_BY_WORDPROCESSING_DRAWING.get(parent.namespaceUri);
|
|
553
|
+
if (namespace === void 0) return;
|
|
554
|
+
chunks.push(xml.slice(cursor, nameEnd), ` xmlns:${prefix}="${namespace}"`);
|
|
555
|
+
cursor = nameEnd;
|
|
556
|
+
}).status !== "scanned" || chunks.length === 0) return xml;
|
|
557
|
+
chunks.push(xml.slice(cursor));
|
|
558
|
+
return chunks.join("");
|
|
365
559
|
};
|
|
366
560
|
const parseOpenTag = ({ xml, start, close, spanMode, rules, retainAttribute }) => {
|
|
367
561
|
const { validNames } = rules;
|
|
@@ -406,7 +600,11 @@ const parseOpenTag = ({ xml, start, close, spanMode, rules, retainAttribute }) =
|
|
|
406
600
|
const raw = xml.slice(valueStart, cursor);
|
|
407
601
|
if (validNames !== void 0 && raw.includes("<")) return reject(`a < in the attribute ${attributeName}`);
|
|
408
602
|
const decoded = rules.readValue(raw);
|
|
409
|
-
if (decoded === null) return
|
|
603
|
+
if (decoded === null) return refuseValue({
|
|
604
|
+
raw,
|
|
605
|
+
start: valueStart,
|
|
606
|
+
reason: `a reference the reader does not accept in ${attributeName}`
|
|
607
|
+
});
|
|
410
608
|
if (retainAttribute === void 0 || retainAttribute({
|
|
411
609
|
attributeName,
|
|
412
610
|
elementName: name
|
|
@@ -483,15 +681,11 @@ const instructionIssue = ({ instruction: { target, body }, open, documentStart,
|
|
|
483
681
|
return XML_DECLARATION_VERSION.test(body) ? null : "an XML declaration without a version";
|
|
484
682
|
};
|
|
485
683
|
const findTagClose = (xml, start) => {
|
|
486
|
-
let quote = 0;
|
|
487
684
|
for (let cursor = start; cursor < xml.length; cursor += 1) {
|
|
488
685
|
const code = xml.charCodeAt(cursor);
|
|
489
|
-
if (quote !== 0) {
|
|
490
|
-
if (code === quote) quote = 0;
|
|
491
|
-
continue;
|
|
492
|
-
}
|
|
493
686
|
if (code === 34 || code === 39) {
|
|
494
|
-
|
|
687
|
+
cursor = xml.indexOf(code === 34 ? "\"" : "'", cursor + 1);
|
|
688
|
+
if (cursor === -1) return -1;
|
|
495
689
|
continue;
|
|
496
690
|
}
|
|
497
691
|
if (code === 62) return cursor;
|
|
@@ -534,30 +728,13 @@ const isValidName = (name, validNames) => {
|
|
|
534
728
|
};
|
|
535
729
|
/** XML 1.0 §2.2 `Char`. */
|
|
536
730
|
const isXmlCharacter = (codePoint) => codePoint === 9 || codePoint === 10 || codePoint === 13 || codePoint >= 32 && codePoint <= 55295 || codePoint >= 57344 && codePoint <= 65533 || codePoint >= 65536 && codePoint <= 1114111;
|
|
537
|
-
const containsOnlyXmlCharacters = (xml) => {
|
|
538
|
-
for (let index = 0; index < xml.length; index += 1) {
|
|
539
|
-
const code = xml.charCodeAt(index);
|
|
540
|
-
if (code >= 32 && code < 55296) continue;
|
|
541
|
-
if (code === 9 || code === 10 || code === 13) continue;
|
|
542
|
-
if (code >= 57344 && code <= 65533) continue;
|
|
543
|
-
if (code >= 55296 && code <= 56319) {
|
|
544
|
-
const next = xml.charCodeAt(index + 1);
|
|
545
|
-
if (next >= 56320 && next <= 57343) {
|
|
546
|
-
index += 1;
|
|
547
|
-
continue;
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
return false;
|
|
551
|
-
}
|
|
552
|
-
return true;
|
|
553
|
-
};
|
|
554
731
|
/**
|
|
555
732
|
* Decode every reference in `value`; `null` when one is refused.
|
|
556
733
|
*
|
|
557
|
-
*
|
|
558
|
-
* refuses the whole value.
|
|
734
|
+
* {@link decodeReference} answers for the text between `&` and `;`, and a
|
|
735
|
+
* `null` there refuses the whole value.
|
|
559
736
|
*/
|
|
560
|
-
const decodeReferences = (value
|
|
737
|
+
const decodeReferences = (value) => {
|
|
561
738
|
const firstEntity = value.indexOf("&");
|
|
562
739
|
if (firstEntity === -1) return value;
|
|
563
740
|
const parts = [];
|
|
@@ -590,109 +767,64 @@ const builtInEntity = (entity) => {
|
|
|
590
767
|
default: return null;
|
|
591
768
|
}
|
|
592
769
|
};
|
|
770
|
+
/**
|
|
771
|
+
* The character an `&#...;` body refers to, or `null`. XML 1.0 §4.1: decimal
|
|
772
|
+
* digits after `#`, or hexadecimal digits after a lower-case `#x` (`A`
|
|
773
|
+
* refers to nothing), to a character §2.2 admits.
|
|
774
|
+
*/
|
|
593
775
|
const parseCharacterReference = (entity) => {
|
|
594
776
|
if (entity.charCodeAt(0) !== 35) return null;
|
|
595
|
-
const
|
|
596
|
-
const
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
return
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
};
|
|
604
|
-
/** The `ooxml` reader's references: the built-ins and any scalar value. */
|
|
605
|
-
const decodeOoxmlReference = (entity) => {
|
|
777
|
+
const hexadecimal = entity.charAt(1) === "x";
|
|
778
|
+
const digits = entity.slice(hexadecimal ? 2 : 1);
|
|
779
|
+
if (digits.length === 0 || !hasOnlyDigits(digits, hexadecimal)) return null;
|
|
780
|
+
const codePoint = Number.parseInt(digits, hexadecimal ? 16 : 10);
|
|
781
|
+
return Number.isInteger(codePoint) && isXmlCharacter(codePoint) ? codePoint : null;
|
|
782
|
+
};
|
|
783
|
+
/** The built-in entities and references to characters XML allows. */
|
|
784
|
+
const decodeReference = (entity) => {
|
|
606
785
|
const builtIn = builtInEntity(entity);
|
|
607
786
|
if (builtIn !== null) return builtIn;
|
|
608
|
-
const
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
if (builtIn !== null) return builtIn;
|
|
618
|
-
const reference = parseCharacterReference(entity);
|
|
619
|
-
if (reference === null || reference.hexMarker === "X" || !Number.isInteger(reference.codePoint) || !isXmlCharacter(reference.codePoint)) return null;
|
|
620
|
-
return String.fromCodePoint(reference.codePoint);
|
|
787
|
+
const codePoint = parseCharacterReference(entity);
|
|
788
|
+
return codePoint === null ? null : String.fromCodePoint(codePoint);
|
|
789
|
+
};
|
|
790
|
+
const describeMalformedReference = ({ kind, reference }) => {
|
|
791
|
+
switch (kind) {
|
|
792
|
+
case "undeclaredEntity": return `the undeclared entity &${reference};`;
|
|
793
|
+
case "invalidCharacterReference": return `the character reference &${reference};, which XML does not allow,`;
|
|
794
|
+
default: return kind;
|
|
795
|
+
}
|
|
621
796
|
};
|
|
797
|
+
/** What a numeric reference spells: `#`, digits or letters, nothing else. */
|
|
798
|
+
const NUMERIC_REFERENCE_SHAPE = /^#[0-9A-Za-z]+$/u;
|
|
622
799
|
/**
|
|
623
|
-
*
|
|
624
|
-
*
|
|
800
|
+
* The first reference in `value` XML refuses, or `null`: an `&Name;` naming
|
|
801
|
+
* none of XML's five built-in entities, or an `&#...;` that is no reference to
|
|
802
|
+
* a character XML 1.0 allows. An `&` that does not begin a syntactic
|
|
803
|
+
* reference is neither.
|
|
625
804
|
*/
|
|
626
|
-
const
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
805
|
+
const firstMalformedReference = (value) => {
|
|
806
|
+
let cursor = value.indexOf("&");
|
|
807
|
+
while (cursor !== -1) {
|
|
808
|
+
const end = value.indexOf(";", cursor + 1);
|
|
809
|
+
if (end === -1) return null;
|
|
810
|
+
const reference = value.slice(cursor + 1, end);
|
|
811
|
+
if (reference.charCodeAt(0) === 35) {
|
|
812
|
+
if (NUMERIC_REFERENCE_SHAPE.test(reference) && parseCharacterReference(reference) === null) return {
|
|
813
|
+
index: cursor,
|
|
814
|
+
kind: "invalidCharacterReference",
|
|
815
|
+
reference
|
|
816
|
+
};
|
|
817
|
+
} else if (builtInEntity(reference) === null && XML_NAME.test(reference)) return {
|
|
818
|
+
index: cursor,
|
|
819
|
+
kind: "undeclaredEntity",
|
|
820
|
+
reference
|
|
821
|
+
};
|
|
822
|
+
cursor = value.indexOf("&", cursor + 1);
|
|
632
823
|
}
|
|
633
|
-
return
|
|
824
|
+
return null;
|
|
634
825
|
};
|
|
635
|
-
/**
|
|
636
|
-
* Names earlier folio releases decoded in DOCX text, beyond XML's five. A part
|
|
637
|
-
* that used one opened with the character it names, and still does.
|
|
638
|
-
*/
|
|
639
|
-
const LEGACY_NAMED_REFERENCES = /* @__PURE__ */ new Map([
|
|
640
|
-
["nbsp", String.fromCodePoint(160)],
|
|
641
|
-
["copy", String.fromCodePoint(169)],
|
|
642
|
-
["reg", String.fromCodePoint(174)],
|
|
643
|
-
["trade", String.fromCodePoint(8482)],
|
|
644
|
-
["mdash", String.fromCodePoint(8212)],
|
|
645
|
-
["ndash", String.fromCodePoint(8211)],
|
|
646
|
-
["hellip", String.fromCodePoint(8230)],
|
|
647
|
-
["laquo", String.fromCodePoint(171)],
|
|
648
|
-
["raquo", String.fromCodePoint(187)],
|
|
649
|
-
["lsquo", String.fromCodePoint(8216)],
|
|
650
|
-
["rsquo", String.fromCodePoint(8217)],
|
|
651
|
-
["ldquo", String.fromCodePoint(8220)],
|
|
652
|
-
["rdquo", String.fromCodePoint(8221)],
|
|
653
|
-
["bull", String.fromCodePoint(8226)],
|
|
654
|
-
["para", String.fromCodePoint(182)],
|
|
655
|
-
["sect", String.fromCodePoint(167)],
|
|
656
|
-
["deg", String.fromCodePoint(176)],
|
|
657
|
-
["frac12", String.fromCodePoint(189)],
|
|
658
|
-
["frac14", String.fromCodePoint(188)],
|
|
659
|
-
["frac34", String.fromCodePoint(190)],
|
|
660
|
-
["cent", String.fromCodePoint(162)],
|
|
661
|
-
["pound", String.fromCodePoint(163)],
|
|
662
|
-
["curren", String.fromCodePoint(164)],
|
|
663
|
-
["yen", String.fromCodePoint(165)],
|
|
664
|
-
["euro", String.fromCodePoint(8364)],
|
|
665
|
-
["dollar", String.fromCodePoint(36)],
|
|
666
|
-
["fnof", String.fromCodePoint(402)],
|
|
667
|
-
["inr", String.fromCodePoint(8377)],
|
|
668
|
-
["af", String.fromCodePoint(1547)],
|
|
669
|
-
["birr", String.fromCodePoint(4709, 4653)],
|
|
670
|
-
["peso", String.fromCodePoint(8369)],
|
|
671
|
-
["rub", String.fromCodePoint(8381)],
|
|
672
|
-
["won", String.fromCodePoint(8361)],
|
|
673
|
-
["yuan", String.fromCodePoint(165)],
|
|
674
|
-
["cedil", String.fromCodePoint(184)]
|
|
675
|
-
]);
|
|
676
826
|
/** The longest reference body earlier releases looked for a `;` within. */
|
|
677
827
|
const LEGACY_REFERENCE_WINDOW = 32;
|
|
678
|
-
/**
|
|
679
|
-
* A reference the `ooxml` reader refuses, read the way earlier releases read
|
|
680
|
-
* it: a character reference to a control character, a surrogate or U+0000
|
|
681
|
-
* reads as nothing, one beyond U+10FFFF and an unknown name stay as written,
|
|
682
|
-
* and the digits of a character reference are read up to the first that is
|
|
683
|
-
* not one.
|
|
684
|
-
*/
|
|
685
|
-
const legacyReference = (entity) => {
|
|
686
|
-
const named = LEGACY_NAMED_REFERENCES.get(entity);
|
|
687
|
-
if (named !== void 0) return named;
|
|
688
|
-
if (entity.charCodeAt(0) !== 35) return null;
|
|
689
|
-
const marker = entity.charAt(1);
|
|
690
|
-
const hexadecimal = marker === "x" || marker === "X";
|
|
691
|
-
const codePoint = Number.parseInt(entity.slice(hexadecimal ? 2 : 1), hexadecimal ? 16 : 10);
|
|
692
|
-
if (Number.isNaN(codePoint) || codePoint < 0 || codePoint > 1114111) return null;
|
|
693
|
-
if (codePoint === 0 || codePoint >= 55296 && codePoint <= 57343 || codePoint < 32 && codePoint !== 9 && codePoint !== 10 && codePoint !== 13) return "";
|
|
694
|
-
return String.fromCodePoint(codePoint);
|
|
695
|
-
};
|
|
696
828
|
const decodeTolerantReferences = (value) => {
|
|
697
829
|
const parts = [];
|
|
698
830
|
let literalStart = 0;
|
|
@@ -705,7 +837,7 @@ const decodeTolerantReferences = (value) => {
|
|
|
705
837
|
continue;
|
|
706
838
|
}
|
|
707
839
|
const entity = value.slice(cursor + 1, end);
|
|
708
|
-
const decoded = entity.length === 0 ? null :
|
|
840
|
+
const decoded = entity.length === 0 ? null : decodeReference(entity);
|
|
709
841
|
if (decoded === null) {
|
|
710
842
|
cursor = value.indexOf("&", cursor + 1);
|
|
711
843
|
continue;
|
|
@@ -740,4 +872,4 @@ const isUnsafePropertyName = (name) => {
|
|
|
740
872
|
default: return false;
|
|
741
873
|
}
|
|
742
874
|
};
|
|
743
|
-
export {
|
|
875
|
+
export { sanitizeXmlCharacters as _, parseStreamingXml as a, isOoxmlNumericIdAttributeName as b, readXml as c, scanStreamingXmlElements as d, scanStreamingXmlNumericIdAttributes as f, hasIllegalXmlCharacters as g, escapeXmlText as h, malformedXmlReferenceError as i, repairMissingDrawingNamespace as l, escapeXmlAttribute as m, XmlReadError as n, parseStreamingXmlWithIdentityVisitor as o, wellFormedXmlIssue as p, getXmlSourceRange as r, parseStreamingXmlWithSourceRanges as s, MalformedXmlReferenceError as t, rewriteStreamingXmlDecimalAttributes as u, ID_VALUE_ELEMENTS as v, localName as x, NUMBERING_ATTRIBUTES as y };
|