@stll/docx-core 0.43.1 → 0.44.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-CEMwM5On.js → document-BuoCvTcR.js} +44 -50
- package/dist/{document-CaGfolcU.d.ts → document-f4_h6JMT.d.ts} +360 -250
- 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 +1130 -110
- package/dist/ops/documentOps.js +31994 -16525
- 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,3 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* folio's one XML reader: a single forward pass that builds the shared
|
|
3
|
+
* element tree, or scans start tags without building one.
|
|
4
|
+
*
|
|
5
|
+
* Every reader in the workspace goes through it, and each states how much it
|
|
6
|
+
* accepts:
|
|
7
|
+
*
|
|
8
|
+
* - `ooxml`: the ordinary contract the DOCX readers parse with. The five
|
|
9
|
+
* built-in entities and character references to characters XML admits are
|
|
10
|
+
* decoded; a DOCTYPE, any other reference, mismatched or unclosed markup,
|
|
11
|
+
* text outside the root and nesting past {@link FOLIO_XML_RESOURCE_LIMITS}
|
|
12
|
+
* are refused.
|
|
13
|
+
* - `well-formed`: XML 1.0 well-formedness on top of that, for validators. One
|
|
14
|
+
* root element, valid names, no repeated attribute, no `<` in an attribute
|
|
15
|
+
* value, comments and processing instructions in their lexical form, and no
|
|
16
|
+
* characters XML excludes.
|
|
17
|
+
* - `tolerant`: `ooxml`, except that text outside the root is dropped and an
|
|
18
|
+
* `&` that begins no reference is read as text. A reference XML refuses is
|
|
19
|
+
* refused, as in `ooxml`.
|
|
20
|
+
*
|
|
21
|
+
* `references: "raw"` keeps each reference as written instead of decoding it,
|
|
22
|
+
* for readers that compare or re-emit source spellings.
|
|
23
|
+
*/
|
|
1
24
|
import { n as XmlNamespaceScope, t as XmlElement } from "../xmlElement-C-vuKoul.js";
|
|
2
25
|
import { TaggedErrorClass } from "better-result";
|
|
3
26
|
type ParseXmlResult = {
|
|
@@ -16,20 +39,47 @@ export declare const getXmlSourceRange: (element: XmlElement) => XmlSourceRange
|
|
|
16
39
|
export type XmlConformance = "ooxml" | "well-formed" | "tolerant";
|
|
17
40
|
/** Whether references are decoded or kept as written. */
|
|
18
41
|
export type XmlReferenceHandling = "decode" | "raw";
|
|
19
|
-
/**
|
|
20
|
-
* What a reference to an entity no declaration defines does. `refuse` is
|
|
21
|
-
* XML 1.0: without a DTD only the five built-ins exist. `accept` admits any
|
|
22
|
-
* reference that is syntactically one (`&Name;`, `&#digits;`, `&#xhex;`) and
|
|
23
|
-
* keeps it as written, for checks that must accept what earlier releases
|
|
24
|
-
* accepted. Not used by the tolerant reader, which accepts every reference.
|
|
25
|
-
*/
|
|
26
|
-
export type XmlUndeclaredEntities = "refuse" | "accept";
|
|
27
42
|
declare const XmlReadErrorBase: TaggedErrorClass<"XmlReadError">;
|
|
28
43
|
/** An XML part the reader refused under the conformance its caller asked for. */
|
|
29
44
|
export declare class XmlReadError extends XmlReadErrorBase<{
|
|
30
45
|
message: string;
|
|
31
46
|
reason: string;
|
|
32
47
|
}> {}
|
|
48
|
+
/** Why a read refused its input. */
|
|
49
|
+
export type XmlRefusal = {
|
|
50
|
+
type: "markup";
|
|
51
|
+
} | {
|
|
52
|
+
/** A reference XML refuses. */
|
|
53
|
+
type: "malformedReference";
|
|
54
|
+
kind: XmlMalformedReferenceKind;
|
|
55
|
+
/** The `&`'s offset in the input, in UTF-16 code units. */
|
|
56
|
+
offset: number;
|
|
57
|
+
/** What stands between `&` and `;`. */
|
|
58
|
+
reference: string;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Why XML refuses a reference: it names an entity no declaration defines
|
|
62
|
+
* (` `, `©`, any name but XML's five), or it is a character
|
|
63
|
+
* reference XML refuses: to a character XML 1.0 does not allow (`�`,
|
|
64
|
+
* ``, ``, a surrogate, past U+10FFFF), with a capital `X`
|
|
65
|
+
* (`A`), or with digits that are not a number (`ab;`).
|
|
66
|
+
*/
|
|
67
|
+
export type XmlMalformedReferenceKind = "undeclaredEntity" | "invalidCharacterReference";
|
|
68
|
+
declare const MalformedXmlReferenceErrorBase: TaggedErrorClass<"MalformedXmlReferenceError">;
|
|
69
|
+
/**
|
|
70
|
+
* A package part holds a reference XML refuses, so it is not well-formed XML
|
|
71
|
+
* and is not opened.
|
|
72
|
+
*/
|
|
73
|
+
export declare class MalformedXmlReferenceError extends MalformedXmlReferenceErrorBase<{
|
|
74
|
+
message: string;
|
|
75
|
+
kind: XmlMalformedReferenceKind;
|
|
76
|
+
/** The package part, e.g. `word/document.xml`. */
|
|
77
|
+
part: string;
|
|
78
|
+
/** The reference's offset in the part's text, in UTF-16 code units. */
|
|
79
|
+
offset: number;
|
|
80
|
+
/** What stands between `&` and `;`. */
|
|
81
|
+
reference: string;
|
|
82
|
+
}> {}
|
|
33
83
|
type AttributeValueSpan = {
|
|
34
84
|
start: number;
|
|
35
85
|
end: number;
|
|
@@ -50,13 +100,12 @@ export type XmlReadResult = {
|
|
|
50
100
|
} | {
|
|
51
101
|
status: "rejected";
|
|
52
102
|
reason: string;
|
|
103
|
+
refusal: XmlRefusal;
|
|
53
104
|
};
|
|
54
105
|
export type XmlReadOptions = {
|
|
55
106
|
conformance: XmlConformance;
|
|
56
107
|
/** Defaults to `"decode"`. */
|
|
57
108
|
references?: XmlReferenceHandling;
|
|
58
|
-
/** Defaults to `"refuse"`. */
|
|
59
|
-
undeclaredEntities?: XmlUndeclaredEntities;
|
|
60
109
|
/**
|
|
61
110
|
* `"retain"` keeps processing instructions, the XML declaration included,
|
|
62
111
|
* as `{ type: "instruction", name, instruction }` nodes; comments are always
|
|
@@ -67,9 +116,17 @@ export type XmlReadOptions = {
|
|
|
67
116
|
inheritedNamespaceScope?: XmlNamespaceScope;
|
|
68
117
|
};
|
|
69
118
|
/** Read `xml` into a tree under the stated conformance, in one pass. */
|
|
70
|
-
export declare const readXml: (xml: string, { conformance, references,
|
|
119
|
+
export declare const readXml: (xml: string, { conformance, references, instructions, inheritedNamespaceScope }: XmlReadOptions) => XmlReadResult;
|
|
120
|
+
/**
|
|
121
|
+
* The refusal for the first reference in `xml` XML refuses, outside comments,
|
|
122
|
+
* CDATA sections and processing instructions, or `null`. A part with none, or
|
|
123
|
+
* one refused first for its markup, answers `null`.
|
|
124
|
+
*/
|
|
125
|
+
export declare const malformedXmlReferenceError: (part: string, xml: string) => MalformedXmlReferenceError | null;
|
|
71
126
|
/**
|
|
72
127
|
* The reason `xml` is not well-formed XML, or `null` when it is.
|
|
128
|
+
* `require-bound-prefixes` also checks the in-scope bindings of element and
|
|
129
|
+
* attribute names.
|
|
73
130
|
*
|
|
74
131
|
* Stricter than XML 1.0 in one respect: an element or attribute named like a
|
|
75
132
|
* built-in object property (`__proto__`, `constructor`, `toString`, ...) is
|
|
@@ -77,7 +134,9 @@ export declare const readXml: (xml: string, { conformance, references, undeclare
|
|
|
77
134
|
* plain objects by name and refuses it too; a part this accepts is one they
|
|
78
135
|
* can read.
|
|
79
136
|
*/
|
|
80
|
-
export declare const wellFormedXmlIssue: (xml: string,
|
|
137
|
+
export declare const wellFormedXmlIssue: (xml: string, options?: {
|
|
138
|
+
namespaces: "require-bound-prefixes";
|
|
139
|
+
}) => string | null;
|
|
81
140
|
/**
|
|
82
141
|
* @param inheritedNamespaceScope the bindings in scope around `xml`, for a
|
|
83
142
|
* fragment captured out of a part whose root declared them.
|
|
@@ -103,7 +162,10 @@ export declare const scanStreamingXmlNumericIdAttributes: (xml: string, visitOpe
|
|
|
103
162
|
/** Visit namespace-resolved start tags and source spans without building a tree. */
|
|
104
163
|
export declare const scanStreamingXmlElements: (xml: string, visitElement: ElementScanVisitor) => {
|
|
105
164
|
status: "unsupported";
|
|
165
|
+
refusal: XmlRefusal;
|
|
106
166
|
} | {
|
|
107
167
|
status: "scanned";
|
|
108
168
|
};
|
|
169
|
+
/** Restore the namespace of an unbound graphic inside a WordprocessingDrawing anchor or inline. */
|
|
170
|
+
export declare const repairMissingDrawingNamespace: (xml: string) => string;
|
|
109
171
|
export type { XmlElement, XmlNamespaceScope };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { t as
|
|
1
|
+
export { t as MalformedXmlReferenceError, n as XmlReadError, r as getXmlSourceRange, i as malformedXmlReferenceError, a as parseStreamingXml, o as parseStreamingXmlWithIdentityVisitor, s as parseStreamingXmlWithSourceRanges, c as readXml, l as repairMissingDrawingNamespace, u as rewriteStreamingXmlDecimalAttributes, d as scanStreamingXmlElements, f as scanStreamingXmlNumericIdAttributes, p as wellFormedXmlIssue } from "../streamingXmlParser-BqnCIEZd.js";
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Namespace resolution for parsed XML elements, shared by both XML parsers.
|
|
3
|
+
*
|
|
4
|
+
* Kept apart from `xmlParser.ts` so the streaming parser can attach the same
|
|
5
|
+
* context without importing the module that dispatches to it.
|
|
6
|
+
*/
|
|
1
7
|
import { n as XmlNamespaceScope, t as XmlElement } from "../xmlElement-C-vuKoul.js";
|
|
2
8
|
export declare const EMPTY_NAMESPACE_SCOPE: XmlNamespaceScope;
|
|
3
9
|
/**
|
|
@@ -24,6 +30,8 @@ export declare const OOXML_NS: {
|
|
|
24
30
|
readonly ct: "http://schemas.openxmlformats.org/package/2006/content-types";
|
|
25
31
|
readonly pr: "http://schemas.openxmlformats.org/package/2006/relationships";
|
|
26
32
|
};
|
|
33
|
+
/** Bindings supplied by a rebuilt Transitional part to its captured children. */
|
|
34
|
+
export declare const OOXML_NAMESPACE_SCOPE: XmlNamespaceScope;
|
|
27
35
|
export type OoxmlPrefix = keyof typeof OOXML_NS;
|
|
28
36
|
export declare const XML_NAMESPACE_URI = "http://www.w3.org/XML/1998/namespace";
|
|
29
37
|
export declare const resolveNamespaceUri: (scope: XmlNamespaceScope | undefined, prefix: string) => string | undefined;
|
|
@@ -24,6 +24,8 @@ const OOXML_NS = {
|
|
|
24
24
|
ct: "http://schemas.openxmlformats.org/package/2006/content-types",
|
|
25
25
|
pr: "http://schemas.openxmlformats.org/package/2006/relationships"
|
|
26
26
|
};
|
|
27
|
+
/** Bindings supplied by a rebuilt Transitional part to its captured children. */
|
|
28
|
+
const OOXML_NAMESPACE_SCOPE = { bindings: new Map(Object.entries(OOXML_NS)) };
|
|
27
29
|
/**
|
|
28
30
|
* Reserved by the XML Namespaces recommendation (§3): every document binds
|
|
29
31
|
* this prefix to this URI whether or not it declares `xmlns:xml`, and no
|
|
@@ -74,4 +76,4 @@ const attachXmlNamespaceContext = (element, inheritedNamespaceScope = EMPTY_NAME
|
|
|
74
76
|
});
|
|
75
77
|
return namespaceScope;
|
|
76
78
|
};
|
|
77
|
-
export { EMPTY_NAMESPACE_SCOPE, OOXML_NS, XML_NAMESPACE_URI, attachXmlNamespaceContext, resolveNamespaceUri };
|
|
79
|
+
export { EMPTY_NAMESPACE_SCOPE, OOXML_NAMESPACE_SCOPE, OOXML_NS, XML_NAMESPACE_URI, attachXmlNamespaceContext, resolveNamespaceUri };
|
package/dist/zip/archive.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { a as ZipCodec, i as InflateOutcome, n as ZipErrorReason, r as InflateExpectation, t as ZipError } from "../errors-Dm3jsb5q.js";
|
|
2
|
-
import
|
|
2
|
+
import "better-result";
|
|
3
3
|
/** The compression an entry is written with. */
|
|
4
4
|
type ZipCompression = "store" | "deflate";
|
|
5
5
|
/** Whether `path` is a relative, forward-slash name with no `..` segment. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stll/docx-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.44.0",
|
|
4
4
|
"description": "Typed OOXML/DOCX model, validation, serialization, legal-source compilation, and browser-native package projection.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"document-model",
|
|
@@ -101,11 +101,11 @@
|
|
|
101
101
|
"dependencies": {
|
|
102
102
|
"@stll/utf8-boundaries": "0.2.0",
|
|
103
103
|
"better-result": "3.0.1",
|
|
104
|
-
"marked": "^18.
|
|
104
|
+
"marked": "^18.2.0"
|
|
105
105
|
},
|
|
106
106
|
"devDependencies": {
|
|
107
107
|
"bun-types": "1.4.4",
|
|
108
|
-
"tsdown": "0.23.
|
|
108
|
+
"tsdown": "0.23.7"
|
|
109
109
|
},
|
|
110
110
|
"main": "./dist/index.js",
|
|
111
111
|
"types": "./dist/index.d.ts"
|
package/dist/href-ogmJmncc.js
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
// src/serialize/xmlEscape.ts
|
|
2
|
-
/**
|
|
3
|
-
* The one owner of XML character escaping for every part folio writes.
|
|
4
|
-
*
|
|
5
|
-
* Two contexts, two functions. A value going into element content takes
|
|
6
|
-
* {@link escapeXmlText}; a value going between the quotes of an attribute
|
|
7
|
-
* takes {@link escapeXmlAttribute}. Nothing else escapes XML by hand: a
|
|
8
|
-
* second escaper always ends up disagreeing with this one about some
|
|
9
|
-
* character, and the disagreement is a package Word refuses to open.
|
|
10
|
-
*
|
|
11
|
-
* Both functions guarantee the same contract: **the result is always legal
|
|
12
|
-
* XML 1.0 character data.** That is what makes the contract checkable — a
|
|
13
|
-
* caller cannot produce a corrupt part by forgetting something.
|
|
14
|
-
*
|
|
15
|
-
* XML 1.0 §2.2 `Char` admits tab, LF, CR, `#x20`-`#xD7FF`, `#xE000`-`#xFFFD`
|
|
16
|
-
* and the astral planes. Everything else — the rest of C0, `#xFFFE`,
|
|
17
|
-
* `#xFFFF`, an unpaired surrogate — cannot appear in a document at all, and
|
|
18
|
-
* cannot be escaped into one either: `�` is as illegal as a raw NUL
|
|
19
|
-
* (§4.1 requires a character reference to refer to a legal `Char`). The only
|
|
20
|
-
* output that is well-formed is one without them, so they are dropped here.
|
|
21
|
-
* Callers that own an input boundary sanitise before this point, with
|
|
22
|
-
* `sanitizeXmlCharacters`, so a user-visible value loses its illegal
|
|
23
|
-
* characters where something can still report it; the drop below is the last
|
|
24
|
-
* line of defence for a boundary nobody has covered yet.
|
|
25
|
-
*
|
|
26
|
-
* The whitespace rules are where the two contexts part:
|
|
27
|
-
*
|
|
28
|
-
* - §3.3.3 normalises tab, LF and CR in an **attribute value** to a space in
|
|
29
|
-
* every conformant parser, so a literal one does not survive a save. They
|
|
30
|
-
* are written as character references, which the same step leaves alone.
|
|
31
|
-
* - §2.11 normalises a literal CR (and CRLF) in **content** to LF on read, so
|
|
32
|
-
* CR is written as a reference there too. Tab and LF survive as themselves.
|
|
33
|
-
*
|
|
34
|
-
* Both contexts escape all five of `& < > " '`. Only `&` and `<` must be
|
|
35
|
-
* escaped in content, but folio has always written the full set, `>` closes
|
|
36
|
-
* the `]]>` hazard of §2.4 without a special case, and one lexical form
|
|
37
|
-
* across every writer is worth more than the bytes it costs. Every spelling
|
|
38
|
-
* here reads back as the character it stands for, so the rule preserves the
|
|
39
|
-
* value whichever context it lands in.
|
|
40
|
-
*/
|
|
41
|
-
/** XML 1.0 §2.2 `Char`, negated: the characters no XML document may contain. */
|
|
42
|
-
const ILLEGAL_XML_CHARACTER_CLASS = "[^\\u0009\\u000A\\u000D\\u0020-\\uD7FF\\uE000-\\uFFFD\\u{10000}-\\u{10FFFF}]";
|
|
43
|
-
/**
|
|
44
|
-
* Unicode-mode classes match code points, so a well-formed surrogate pair is a
|
|
45
|
-
* single astral code point and cannot match a BMP class: this matches exactly
|
|
46
|
-
* the unpaired surrogates.
|
|
47
|
-
*/
|
|
48
|
-
const LONE_SURROGATE_PATTERN = /[\uD800-\uDFFF]/gu;
|
|
49
|
-
const ILLEGAL_XML_CHARACTER_PATTERN = new RegExp(ILLEGAL_XML_CHARACTER_CLASS, "gu");
|
|
50
|
-
const ILLEGAL_XML_CHARACTER_PROBE = new RegExp(ILLEGAL_XML_CHARACTER_CLASS, "u");
|
|
51
|
-
/** What an unpaired surrogate becomes at an input boundary: U+FFFD. */
|
|
52
|
-
const REPLACEMENT_CHARACTER = "�";
|
|
53
|
-
const TEXT_ESCAPES = {
|
|
54
|
-
"&": "&",
|
|
55
|
-
"<": "<",
|
|
56
|
-
">": ">",
|
|
57
|
-
"\"": """,
|
|
58
|
-
"'": "'",
|
|
59
|
-
"\r": " "
|
|
60
|
-
};
|
|
61
|
-
const ATTRIBUTE_ESCAPES = {
|
|
62
|
-
...TEXT_ESCAPES,
|
|
63
|
-
" ": "	",
|
|
64
|
-
"\n": " "
|
|
65
|
-
};
|
|
66
|
-
const TEXT_PROBE = new RegExp(`[&<>"'\\r]|${ILLEGAL_XML_CHARACTER_CLASS}`, "u");
|
|
67
|
-
const TEXT_PATTERN = new RegExp(`[&<>"'\\r]|${ILLEGAL_XML_CHARACTER_CLASS}`, "gu");
|
|
68
|
-
const ATTRIBUTE_PROBE = new RegExp(`[&<>"'\\t\\n\\r]|${ILLEGAL_XML_CHARACTER_CLASS}`, "u");
|
|
69
|
-
const ATTRIBUTE_PATTERN = new RegExp(`[&<>"'\\t\\n\\r]|${ILLEGAL_XML_CHARACTER_CLASS}`, "gu");
|
|
70
|
-
/** Escape a value for XML element content. Tab and LF survive as themselves. */
|
|
71
|
-
const escapeXmlText = (value) => TEXT_PROBE.test(value) ? value.replace(TEXT_PATTERN, (character) => TEXT_ESCAPES[character] ?? "") : value;
|
|
72
|
-
/**
|
|
73
|
-
* Escape a value for an XML attribute value, of either quoting. Tab, LF and CR
|
|
74
|
-
* become character references so attribute-value normalisation cannot flatten
|
|
75
|
-
* them to spaces.
|
|
76
|
-
*/
|
|
77
|
-
const escapeXmlAttribute = (value) => ATTRIBUTE_PROBE.test(value) ? value.replace(ATTRIBUTE_PATTERN, (character) => ATTRIBUTE_ESCAPES[character] ?? "") : value;
|
|
78
|
-
/** Whether `value` holds a character XML 1.0 cannot represent at all. */
|
|
79
|
-
const hasIllegalXmlCharacters = (value) => ILLEGAL_XML_CHARACTER_PROBE.test(value);
|
|
80
|
-
/**
|
|
81
|
-
* Make a value from outside folio representable in XML: an unpaired surrogate
|
|
82
|
-
* becomes U+FFFD, every other character XML cannot hold is dropped.
|
|
83
|
-
*
|
|
84
|
-
* This is the rule for an input boundary — paste, a document operation, a
|
|
85
|
-
* markdown or legal-source compile, a template fill — where the value is still
|
|
86
|
-
* attached to the request that carried it and a warning still has somewhere to
|
|
87
|
-
* go. `escapeXmlText` and `escapeXmlAttribute` apply the same rule again
|
|
88
|
-
* further down, but by then nobody can say which input lost a character.
|
|
89
|
-
*/
|
|
90
|
-
const sanitizeXmlCharacters = (value) => hasIllegalXmlCharacters(value) ? value.replace(LONE_SURROGATE_PATTERN, REPLACEMENT_CHARACTER).replace(ILLEGAL_XML_CHARACTER_PATTERN, "") : value;
|
|
91
|
-
// src/markdown/href.ts
|
|
92
|
-
const ALLOWED_URL_PROTOCOLS = /* @__PURE__ */ new Set([
|
|
93
|
-
"http:",
|
|
94
|
-
"https:",
|
|
95
|
-
"mailto:",
|
|
96
|
-
"tel:"
|
|
97
|
-
]);
|
|
98
|
-
/**
|
|
99
|
-
* Keep only http(s), mailto, and tel URLs, normalised through the URL parser.
|
|
100
|
-
* Anything else (javascript:, data:, relative paths, malformed input) drops to
|
|
101
|
-
* `undefined` so a markdown link degrades to its text instead of carrying an
|
|
102
|
-
* executable target into the document.
|
|
103
|
-
*/
|
|
104
|
-
const sanitizeExternalUrl = (rawUrl) => {
|
|
105
|
-
if (!rawUrl) return;
|
|
106
|
-
const trimmed = rawUrl.trim();
|
|
107
|
-
if (!trimmed) return;
|
|
108
|
-
const parsed = parseUrl(trimmed);
|
|
109
|
-
if (!parsed || !ALLOWED_URL_PROTOCOLS.has(parsed.protocol)) return;
|
|
110
|
-
if ((parsed.protocol === "mailto:" || parsed.protocol === "tel:") && parsed.pathname.trim() === "") return;
|
|
111
|
-
return parsed.href;
|
|
112
|
-
};
|
|
113
|
-
const parseUrl = (value) => {
|
|
114
|
-
try {
|
|
115
|
-
return new URL(value);
|
|
116
|
-
} catch {
|
|
117
|
-
return null;
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
const hasUnsafeAnchorCharacter = (anchor) => {
|
|
121
|
-
for (const char of anchor) {
|
|
122
|
-
const codePoint = char.codePointAt(0) ?? 0;
|
|
123
|
-
if (codePoint <= 32 || codePoint === 127 || char.trim() === "") return true;
|
|
124
|
-
}
|
|
125
|
-
return false;
|
|
126
|
-
};
|
|
127
|
-
/** A `#anchor` stays a document-internal target; anything else must pass {@link sanitizeExternalUrl}. */
|
|
128
|
-
const sanitizeMarkdownHref = (rawHref) => {
|
|
129
|
-
const trimmed = rawHref.trim();
|
|
130
|
-
if (!trimmed) return;
|
|
131
|
-
if (trimmed.startsWith("#")) {
|
|
132
|
-
const anchor = trimmed.slice(1);
|
|
133
|
-
if (!anchor || hasUnsafeAnchorCharacter(anchor)) return;
|
|
134
|
-
return `#${anchor}`;
|
|
135
|
-
}
|
|
136
|
-
return sanitizeExternalUrl(trimmed);
|
|
137
|
-
};
|
|
138
|
-
export { hasIllegalXmlCharacters as a, escapeXmlText as i, sanitizeMarkdownHref as n, sanitizeXmlCharacters as o, escapeXmlAttribute as r, sanitizeExternalUrl as t };
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The ordered verbatim sink: where a container's unmodelled markup goes.
|
|
3
|
-
*
|
|
4
|
-
* A parser that walks a container's children, models the ones it recognises
|
|
5
|
-
* and lets the rest fall off the end of a `switch` loses them silently, and
|
|
6
|
-
* verbatim replay hides the loss until somebody edits the document. The sink
|
|
7
|
-
* is the other branch of that `switch`. It keeps the markup as bytes together
|
|
8
|
-
* with **where it was**, so the serializer puts it back between the same
|
|
9
|
-
* modelled siblings rather than at the end of the container, which is what the
|
|
10
|
-
* schema's ordered content models require.
|
|
11
|
-
*
|
|
12
|
-
* The position is recorded as an ordinal, not a pointer: `index` is how many
|
|
13
|
-
* modelled children the container had read when the markup arrived. That
|
|
14
|
-
* survives the model being edited — inserting a paragraph shifts what comes
|
|
15
|
-
* after it, which is what a reader would expect — and it needs no identity on
|
|
16
|
-
* the modelled children. A property set counts differently, and
|
|
17
|
-
* `containerChildren.ts` says why.
|
|
18
|
-
*
|
|
19
|
-
* One field, `preserved`, carries both halves for every container that has a
|
|
20
|
-
* sink, so a reader never has to know which of two names a given container
|
|
21
|
-
* used.
|
|
22
|
-
*/
|
|
23
|
-
/** Markup the container's model does not hold, and where it sat. */
|
|
24
|
-
type PreservedChild = {
|
|
25
|
-
/**
|
|
26
|
-
* Where the markup goes back. `0` puts it before the first modelled child;
|
|
27
|
-
* the container's modelled count puts it after the last. In a property set
|
|
28
|
-
* it is the schema ordinal, and the two halves merge by it.
|
|
29
|
-
*/
|
|
30
|
-
index: number;
|
|
31
|
-
/** Replayable markup for one child, as `captureVerbatimXml` wrote it. */
|
|
32
|
-
xml: string;
|
|
33
|
-
};
|
|
34
|
-
/**
|
|
35
|
-
* A container's unmodelled markup. Absent means the parser read every child
|
|
36
|
-
* the source carried, which is the state a fully modelled container is in; an
|
|
37
|
-
* empty record is never written.
|
|
38
|
-
*/
|
|
39
|
-
type PreservedMarkup = {
|
|
40
|
-
/** Ordered by `index`, then by source order within an index. */
|
|
41
|
-
children?: PreservedChild[];
|
|
42
|
-
};
|
|
43
|
-
/**
|
|
44
|
-
* One attribute an element carried that its parser has no field for.
|
|
45
|
-
*
|
|
46
|
-
* The remainder is a sibling of the child sink, not a member of it: an
|
|
47
|
-
* attribute has no position among children to keep, so it rides the element's
|
|
48
|
-
* own model record — `Paragraph.preservedAttributes` and its siblings — and
|
|
49
|
-
* the serializer writes it back into the same start tag.
|
|
50
|
-
*
|
|
51
|
-
* The name is resolved, never the source's spelling. folio reads an attribute
|
|
52
|
-
* by namespace URI plus local name, so a remainder that kept `"w:rsidR"`
|
|
53
|
-
* textually would keep a second copy of the `w:rsidR` a source spelled
|
|
54
|
-
* `altw:rsidR` under a second binding of the same URI. `namespace` is absent
|
|
55
|
-
* for an unprefixed attribute, which has none; a namespace declaration is not
|
|
56
|
-
* an attribute of the element in this sense and is never in the remainder,
|
|
57
|
-
* because the rebuilt part binds its own prefixes.
|
|
58
|
-
*/
|
|
59
|
-
type PreservedAttribute = {
|
|
60
|
-
/** Resolved namespace URI, absent for an unprefixed attribute. */
|
|
61
|
-
namespace?: string;
|
|
62
|
-
/** Local name, without the prefix the source happened to bind. */
|
|
63
|
-
name: string;
|
|
64
|
-
value: string;
|
|
65
|
-
};
|
|
66
|
-
export { PreservedChild as n, PreservedMarkup as r, PreservedAttribute as t };
|