@stll/docx-core 0.27.0 → 0.28.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.
@@ -0,0 +1,92 @@
1
+ //#region 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
+ "&": "&amp;",
55
+ "<": "&lt;",
56
+ ">": "&gt;",
57
+ "\"": "&quot;",
58
+ "'": "&apos;",
59
+ "\r": "&#13;"
60
+ };
61
+ const ATTRIBUTE_ESCAPES = {
62
+ ...TEXT_ESCAPES,
63
+ " ": "&#9;",
64
+ "\n": "&#10;"
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
+ //#endregion
92
+ export { sanitizeXmlCharacters as i, escapeXmlText as n, hasIllegalXmlCharacters as r, escapeXmlAttribute as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stll/docx-core",
3
- "version": "0.27.0",
3
+ "version": "0.28.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",
@@ -40,6 +40,10 @@
40
40
  "types": "./dist/model/document.d.ts",
41
41
  "import": "./dist/model/document.js"
42
42
  },
43
+ "./ops": {
44
+ "types": "./dist/ops/documentOps.d.ts",
45
+ "import": "./dist/ops/documentOps.js"
46
+ },
43
47
  "./projection": {
44
48
  "types": "./dist/projection.d.ts",
45
49
  "import": "./dist/projection.js"