@portone/docx-editor 0.6.3 → 0.6.5
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 +62 -0
- package/dist/DocxEditor.d.ts +11 -0
- package/dist/DocxEditor.js +7 -3
- package/dist/docx/comments/writing.d.ts +8 -0
- package/dist/docx/comments/writing.js +9 -2
- package/dist/docx/headersFooters.d.ts +7 -0
- package/dist/docx/headersFooters.js +4 -1
- package/dist/docx/identities.d.ts +4 -3
- package/dist/docx/identities.js +14 -1
- package/dist/docx/importDocx.js +2 -0
- package/dist/docx/importParagraph.d.ts +4 -0
- package/dist/docx/importParagraph.js +2 -0
- package/dist/docx/importSdtBlock.d.ts +2 -3
- package/dist/docx/importSdtBlock.js +7 -16
- package/dist/docx/invariants.js +60 -3
- package/dist/docx/numberingPlanner.d.ts +3 -0
- package/dist/docx/numberingPlanner.js +8 -2
- package/dist/docx/packageParts.d.ts +9 -0
- package/dist/docx/packageParts.js +16 -0
- package/dist/docx/packagePrefixes.d.ts +12 -0
- package/dist/docx/packagePrefixes.js +18 -0
- package/dist/docx/partPlan.js +3 -5
- package/dist/docx/sdt.d.ts +27 -0
- package/dist/docx/sdt.js +14 -1
- package/dist/docx/serializeBlock.js +4 -3
- package/dist/docx/serializeParagraph.d.ts +2 -1
- package/dist/docx/serializeParagraph.js +6 -1
- package/dist/docx/serializeSdtBlock.d.ts +0 -8
- package/dist/docx/serializeSdtBlock.js +1 -5
- package/dist/docx/storyParts.d.ts +3 -0
- package/dist/docx/storyParts.js +4 -1
- package/dist/docx/wrappers.d.ts +23 -8
- package/dist/docx/wrappers.js +33 -2
- package/dist/editor/blockControlEdits.d.ts +2 -2
- package/dist/editor/blockControlEdits.js +6 -6
- package/dist/editor/commands/lockCommands.js +25 -5
- package/dist/editor/createEditor.d.ts +4 -1
- package/dist/editor/createEditor.js +14 -2
- package/dist/editor/editRefusal.d.ts +68 -0
- package/dist/editor/editRefusal.js +177 -0
- package/dist/editor/inlineControlEdits.d.ts +20 -0
- package/dist/editor/inlineControlEdits.js +69 -0
- package/dist/editor/plugins/controlContents.d.ts +24 -0
- package/dist/editor/plugins/controlContents.js +260 -0
- package/dist/editor/plugins/controlLifecycle.js +3 -5
- package/dist/editor/plugins/keymap.js +18 -7
- package/dist/editor/stories/storyState.js +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/ooxml/element.js +3 -2
- package/dist/ooxml/errors.d.ts +11 -0
- package/dist/ooxml/fragment.js +3 -2
- package/dist/ooxml/names.d.ts +4 -0
- package/dist/ooxml/names.js +8 -2
- package/dist/ooxml/partPrefixes.d.ts +21 -0
- package/dist/ooxml/partPrefixes.js +164 -0
- package/dist/ooxml/partSplice.d.ts +6 -1
- package/dist/ooxml/partSplice.js +30 -10
- package/dist/ooxml/tagScan.d.ts +24 -2
- package/dist/ooxml/tagScan.js +26 -7
- package/dist/ooxml/xml.d.ts +8 -0
- package/dist/ooxml/xml.js +27 -5
- package/dist/schema/attrRoles.js +11 -0
- package/dist/schema/clipboard.js +2 -0
- package/dist/schema/controlAttrs.d.ts +33 -13
- package/dist/schema/controlAttrs.js +18 -6
- package/dist/schema/docxSchema.d.ts +1 -1
- package/dist/schema/docxSchema.js +60 -5
- package/dist/schema/guards.d.ts +8 -2
- package/dist/schema/guards.js +14 -8
- package/dist/schema/locks.d.ts +62 -7
- package/dist/schema/locks.js +99 -25
- package/dist/styles/classNames.d.ts +4 -0
- package/dist/styles/classNames.js +4 -0
- package/dist/styles.css +78 -14
- package/dist/ui/MenuNote.d.ts +12 -0
- package/dist/ui/MenuNote.js +14 -0
- package/dist/ui/TableMenu.js +38 -19
- package/dist/ui/TextMenu.js +31 -25
- package/package.json +2 -2
package/dist/ooxml/errors.d.ts
CHANGED
|
@@ -94,6 +94,17 @@ export type ExportProblemReason =
|
|
|
94
94
|
readonly kind: "unwritable-part-root";
|
|
95
95
|
readonly part: ExportPartName;
|
|
96
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* A part a change is written into binds a prefix the writer spells to a namespace of its own, so
|
|
99
|
+
* the declaration the write adds would contradict the part (`unsupported-content`)
|
|
100
|
+
*/
|
|
101
|
+
| {
|
|
102
|
+
readonly kind: "conflicting-part-prefix";
|
|
103
|
+
/** Where the part sits in the package */
|
|
104
|
+
readonly path: string;
|
|
105
|
+
/** The prefix the part binds to something other than what the writer spells it for */
|
|
106
|
+
readonly prefix: string;
|
|
107
|
+
}
|
|
97
108
|
/** A cell's vertical merge covers rows its table does not have (`invalid-table`) */
|
|
98
109
|
| {
|
|
99
110
|
readonly kind: "vertical-merge-past-table";
|
package/dist/ooxml/fragment.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/ooxml/fragment.ts
|
|
2
|
+
import { XMLNS, xmlnsName } from "./names.js";
|
|
2
3
|
import {
|
|
3
4
|
elementChildren,
|
|
4
5
|
namespaceDecls,
|
|
@@ -29,7 +30,7 @@ function loneElement(xml) {
|
|
|
29
30
|
}
|
|
30
31
|
function isNamed(el, names) {
|
|
31
32
|
if (!names.includes(el.localName)) return false;
|
|
32
|
-
const declaration = el.prefix === null ?
|
|
33
|
+
const declaration = el.prefix === null ? XMLNS : xmlnsName(el.prefix);
|
|
33
34
|
for (let scope = el; scope.parentElement !== null; scope = scope.parentElement) {
|
|
34
35
|
if (scope.hasAttribute(declaration)) return el.namespaceURI === W_NS;
|
|
35
36
|
}
|
|
@@ -47,7 +48,7 @@ function holdsHeadAlone(el, shape) {
|
|
|
47
48
|
}
|
|
48
49
|
function rebindsReservedPrefix(el) {
|
|
49
50
|
return Array.from(el.attributes).some((attr) => {
|
|
50
|
-
if (attr.prefix !==
|
|
51
|
+
if (attr.prefix !== XMLNS) return false;
|
|
51
52
|
const reserved = RESERVED_PREFIXES.get(attr.localName);
|
|
52
53
|
return reserved !== void 0 && reserved !== attr.value;
|
|
53
54
|
}) || elementChildren(el).some(rebindsReservedPrefix);
|
package/dist/ooxml/names.d.ts
CHANGED
|
@@ -38,5 +38,9 @@ export declare function isKnownPrefix(prefix: string): prefix is KnownPrefix;
|
|
|
38
38
|
export declare function qualify(prefix: KnownPrefix, local: string): string;
|
|
39
39
|
/** `wName("val")` -> `w:val`. The one place the written WordprocessingML prefix is decided */
|
|
40
40
|
export declare function wName(local: string): string;
|
|
41
|
+
/** The attribute name a namespace declaration stands under, on its own for the default one */
|
|
42
|
+
export declare const XMLNS = "xmlns";
|
|
43
|
+
/** The name a declaration of one prefix is written under (`xmlnsName("w")` -> `xmlns:w`) */
|
|
44
|
+
export declare function xmlnsName(prefix: string): string;
|
|
41
45
|
/** The declaration a part root carries for one prefix, ready to write inside an opening tag */
|
|
42
46
|
export declare function xmlnsDecl(prefix: KnownPrefix): string;
|
package/dist/ooxml/names.js
CHANGED
|
@@ -22,8 +22,12 @@ function qualify(prefix, local) {
|
|
|
22
22
|
function wName(local) {
|
|
23
23
|
return qualify(W_PREFIX, local);
|
|
24
24
|
}
|
|
25
|
+
var XMLNS = "xmlns";
|
|
26
|
+
function xmlnsName(prefix) {
|
|
27
|
+
return `${XMLNS}:${prefix}`;
|
|
28
|
+
}
|
|
25
29
|
function xmlnsDecl(prefix) {
|
|
26
|
-
return
|
|
30
|
+
return `${xmlnsName(prefix)}="${NAMESPACES[prefix]}"`;
|
|
27
31
|
}
|
|
28
32
|
export {
|
|
29
33
|
M_NS,
|
|
@@ -31,8 +35,10 @@ export {
|
|
|
31
35
|
R_NS,
|
|
32
36
|
W_NS,
|
|
33
37
|
W_PREFIX,
|
|
38
|
+
XMLNS,
|
|
34
39
|
isKnownPrefix,
|
|
35
40
|
qualify,
|
|
36
41
|
wName,
|
|
37
|
-
xmlnsDecl
|
|
42
|
+
xmlnsDecl,
|
|
43
|
+
xmlnsName
|
|
38
44
|
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Spelling the namespaces of an arrived part under the prefixes this package writes.
|
|
3
|
+
* `spec/notes/conformance.md` holds the decision and what it rests on.
|
|
4
|
+
*
|
|
5
|
+
* Names alone are rewritten: element names, attribute names, the namespace declarations
|
|
6
|
+
* themselves, and the prefix tokens of the markup-compatibility attributes that carry a prefix or
|
|
7
|
+
* QName list (§17.2.1-2, §17.2.4-5). No WordprocessingML attribute value holds a QName, so no
|
|
8
|
+
* other value is looked at, and text, CDATA, comments and processing instructions are copied byte
|
|
9
|
+
* for byte - a `w:p` a document wrote inside a `w:t` is its text, not its markup.
|
|
10
|
+
*
|
|
11
|
+
* Nothing here turns a part down. One whose rewrite would not be unambiguous is handed back as it
|
|
12
|
+
* arrived, and what refuses it then is what refused it before this rewrite existed: the reader
|
|
13
|
+
* that needs the prefix (`docx/importDocx`) or the write about to spell it (`ooxml/partSplice`).
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* The part text with every namespace this package knows spelled the way it writes it.
|
|
17
|
+
*
|
|
18
|
+
* A part that comes back unrewritten comes back as the same string by identity, so a package no
|
|
19
|
+
* producer wrote foreign prefixes into is never re-encoded and still exports byte for byte.
|
|
20
|
+
*/
|
|
21
|
+
export declare function withEditorPrefixes(xml: string): string;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// src/ooxml/partPrefixes.ts
|
|
2
|
+
import {
|
|
3
|
+
isKnownPrefix,
|
|
4
|
+
NAMESPACES,
|
|
5
|
+
qualify,
|
|
6
|
+
XMLNS,
|
|
7
|
+
xmlnsName
|
|
8
|
+
} from "./names.js";
|
|
9
|
+
import {
|
|
10
|
+
attrSpans,
|
|
11
|
+
parseAttrs,
|
|
12
|
+
readTag,
|
|
13
|
+
rootTagAt
|
|
14
|
+
} from "./tagScan.js";
|
|
15
|
+
var CANONICAL_PREFIX = new Map(
|
|
16
|
+
Object.keys(NAMESPACES).filter(isKnownPrefix).map((prefix) => [NAMESPACES[prefix], prefix])
|
|
17
|
+
);
|
|
18
|
+
var MCE_PREFIX_LISTS = new Set(
|
|
19
|
+
[
|
|
20
|
+
"Ignorable",
|
|
21
|
+
"MustUnderstand",
|
|
22
|
+
"ProcessContent",
|
|
23
|
+
"PreserveElements",
|
|
24
|
+
"PreserveAttributes"
|
|
25
|
+
].map((local) => qualify("mc", local))
|
|
26
|
+
);
|
|
27
|
+
var MCE_ALTERNATE_CONTENT = new Set(
|
|
28
|
+
["Choice", "Fallback"].map((local) => qualify("mc", local))
|
|
29
|
+
);
|
|
30
|
+
var DECL = `${XMLNS}:`;
|
|
31
|
+
function prefixMapOf(rootAttrs) {
|
|
32
|
+
const bound = /* @__PURE__ */ new Map();
|
|
33
|
+
const mapped = /* @__PURE__ */ new Map();
|
|
34
|
+
let defaultTo = null;
|
|
35
|
+
for (const [name, namespace] of rootAttrs) {
|
|
36
|
+
if (name === XMLNS) {
|
|
37
|
+
defaultTo = CANONICAL_PREFIX.get(namespace) ?? null;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (!name.startsWith(DECL)) continue;
|
|
41
|
+
const prefix = name.slice(DECL.length);
|
|
42
|
+
bound.set(prefix, namespace);
|
|
43
|
+
const canonical = CANONICAL_PREFIX.get(namespace);
|
|
44
|
+
if (canonical !== void 0 && canonical !== prefix)
|
|
45
|
+
mapped.set(prefix, canonical);
|
|
46
|
+
}
|
|
47
|
+
const targets = new Set(mapped.values());
|
|
48
|
+
if (defaultTo !== null) targets.add(defaultTo);
|
|
49
|
+
for (const target of targets) {
|
|
50
|
+
const already = bound.get(target);
|
|
51
|
+
if (already !== void 0 && already !== NAMESPACES[target]) return null;
|
|
52
|
+
}
|
|
53
|
+
return { mapped, defaultTo, targets };
|
|
54
|
+
}
|
|
55
|
+
var underDefault = (name, map) => map.defaultTo === null ? name : qualify(map.defaultTo, name);
|
|
56
|
+
var inNoNamespace = (name) => name;
|
|
57
|
+
var asPrefix = (token, map) => map.mapped.get(token) ?? token;
|
|
58
|
+
function mappedName(name, map, unprefixed) {
|
|
59
|
+
const colon = name.indexOf(":");
|
|
60
|
+
if (colon === -1) return unprefixed(name, map);
|
|
61
|
+
const canonical = map.mapped.get(name.slice(0, colon));
|
|
62
|
+
return canonical === void 0 ? name : canonical + name.slice(colon);
|
|
63
|
+
}
|
|
64
|
+
function rewriteAttrName(name, map) {
|
|
65
|
+
if (name === XMLNS) {
|
|
66
|
+
return map.defaultTo === null ? name : xmlnsName(map.defaultTo);
|
|
67
|
+
}
|
|
68
|
+
if (name.startsWith(DECL)) {
|
|
69
|
+
const canonical = map.mapped.get(name.slice(DECL.length));
|
|
70
|
+
return canonical === void 0 ? name : xmlnsName(canonical);
|
|
71
|
+
}
|
|
72
|
+
if (name.startsWith("xml:")) return name;
|
|
73
|
+
return mappedName(name, map, inNoNamespace);
|
|
74
|
+
}
|
|
75
|
+
function isDeclaration(name) {
|
|
76
|
+
return name === XMLNS || name.startsWith(DECL);
|
|
77
|
+
}
|
|
78
|
+
function namesPrefixes(elementName, attrName) {
|
|
79
|
+
return MCE_PREFIX_LISTS.has(attrName) || attrName === "Requires" && MCE_ALTERNATE_CONTENT.has(elementName);
|
|
80
|
+
}
|
|
81
|
+
function rewriteTokens(value, map) {
|
|
82
|
+
return value.replace(/\S+/g, (token) => mappedName(token, map, asPrefix));
|
|
83
|
+
}
|
|
84
|
+
function spelledNamespace(prefix, map) {
|
|
85
|
+
const canonical = map.mapped.get(prefix);
|
|
86
|
+
if (canonical !== void 0) return NAMESPACES[canonical];
|
|
87
|
+
if (!isKnownPrefix(prefix) || !map.targets.has(prefix)) return void 0;
|
|
88
|
+
return NAMESPACES[prefix];
|
|
89
|
+
}
|
|
90
|
+
function rebindsSpelledPrefix(raw, spans, map) {
|
|
91
|
+
return spans.some((span) => {
|
|
92
|
+
const name = raw.slice(span.nameStart, span.nameEnd);
|
|
93
|
+
if (!isDeclaration(name)) return false;
|
|
94
|
+
const namespace = raw.slice(span.valueStart, span.valueEnd);
|
|
95
|
+
if (name === XMLNS) {
|
|
96
|
+
return map.defaultTo !== null && namespace !== NAMESPACES[map.defaultTo];
|
|
97
|
+
}
|
|
98
|
+
const spelled = spelledNamespace(name.slice(DECL.length), map);
|
|
99
|
+
return spelled !== void 0 && namespace !== spelled;
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
function rewriteAttrs(raw, elementName, map) {
|
|
103
|
+
const spans = attrSpans(raw);
|
|
104
|
+
if (spans === null) return null;
|
|
105
|
+
if (raw.includes(XMLNS) && rebindsSpelledPrefix(raw, spans, map)) return null;
|
|
106
|
+
const declared = new Set(
|
|
107
|
+
spans.map((span) => raw.slice(span.nameStart, span.nameEnd))
|
|
108
|
+
);
|
|
109
|
+
const written = /* @__PURE__ */ new Set();
|
|
110
|
+
let out = "";
|
|
111
|
+
let at = 0;
|
|
112
|
+
for (const span of spans) {
|
|
113
|
+
const name = raw.slice(span.nameStart, span.nameEnd);
|
|
114
|
+
const rewritten = rewriteAttrName(name, map);
|
|
115
|
+
if (rewritten !== name && isDeclaration(rewritten) && (declared.has(rewritten) || written.has(rewritten))) {
|
|
116
|
+
at = span.valueEnd + 1;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
written.add(rewritten);
|
|
120
|
+
const value = raw.slice(span.valueStart, span.valueEnd);
|
|
121
|
+
out += raw.slice(at, span.nameStart) + rewritten + raw.slice(span.nameEnd, span.valueStart) + (namesPrefixes(elementName, rewritten) ? rewriteTokens(value, map) : value) + span.quote;
|
|
122
|
+
at = span.valueEnd + 1;
|
|
123
|
+
}
|
|
124
|
+
return out + raw.slice(at);
|
|
125
|
+
}
|
|
126
|
+
function rewriteTag(xml, tag, map) {
|
|
127
|
+
const closeLength = tag.kind === "empty" ? 2 : 1;
|
|
128
|
+
const name = mappedName(tag.name, map, underDefault);
|
|
129
|
+
const raw = xml.slice(tag.nameEnd, tag.end - closeLength);
|
|
130
|
+
const attrs = tag.kind === "close" ? raw : rewriteAttrs(raw, name, map);
|
|
131
|
+
if (attrs === null) return null;
|
|
132
|
+
return (tag.kind === "close" ? "</" : "<") + name + attrs + xml.slice(tag.end - closeLength, tag.end);
|
|
133
|
+
}
|
|
134
|
+
function withEditorPrefixes(xml) {
|
|
135
|
+
const rootAt = rootTagAt(xml);
|
|
136
|
+
if (rootAt === -1) return xml;
|
|
137
|
+
const root = readTag(xml, rootAt);
|
|
138
|
+
if (root === null || root.kind === "close") return xml;
|
|
139
|
+
const rootAttrs = parseAttrs(
|
|
140
|
+
xml.slice(root.nameEnd, root.end - (root.kind === "empty" ? 2 : 1))
|
|
141
|
+
);
|
|
142
|
+
if (rootAttrs === null) return xml;
|
|
143
|
+
const map = prefixMapOf(rootAttrs);
|
|
144
|
+
if (map === null) return xml;
|
|
145
|
+
if (map.mapped.size === 0 && map.defaultTo === null) return xml;
|
|
146
|
+
const out = [];
|
|
147
|
+
let at = 0;
|
|
148
|
+
for (; ; ) {
|
|
149
|
+
const lt = xml.indexOf("<", at);
|
|
150
|
+
if (lt === -1) break;
|
|
151
|
+
const tag = readTag(xml, lt);
|
|
152
|
+
if (tag === null) return xml;
|
|
153
|
+
const rewritten = tag.kind === "other" ? xml.slice(lt, tag.end) : rewriteTag(xml, tag, map);
|
|
154
|
+
if (rewritten === null) return xml;
|
|
155
|
+
out.push(xml.slice(at, lt));
|
|
156
|
+
out.push(rewritten);
|
|
157
|
+
at = tag.end;
|
|
158
|
+
}
|
|
159
|
+
out.push(xml.slice(at));
|
|
160
|
+
return out.join("");
|
|
161
|
+
}
|
|
162
|
+
export {
|
|
163
|
+
withEditorPrefixes
|
|
164
|
+
};
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* comment for the end of the part, and miss the root altogether when it is written under a
|
|
9
9
|
* prefix or closes on itself.
|
|
10
10
|
*/
|
|
11
|
-
import type
|
|
11
|
+
import { type KnownPrefix } from "./names";
|
|
12
12
|
/** One child to put into a part, named by the local part of its tag so the child order can place it */
|
|
13
13
|
export interface PartChild {
|
|
14
14
|
name: string;
|
|
@@ -60,6 +60,11 @@ export interface RootDeclarations {
|
|
|
60
60
|
/** The tokens `mc:Ignorable` has to name, added to the ones it names already */
|
|
61
61
|
ignorable?: readonly string[];
|
|
62
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Which prefix `ensureRootDeclarations` would refuse this part over, and null for a part it would
|
|
65
|
+
* write into, so an invariant can ask before anything is written what the write would answer.
|
|
66
|
+
*/
|
|
67
|
+
export declare function rootBindingConflict(xml: string, declarations: RootDeclarations): string | null;
|
|
63
68
|
/**
|
|
64
69
|
* The part with its root binding every prefix named and its `mc:Ignorable` naming every token
|
|
65
70
|
* asked for, each declared once: a matching declaration is preserved, and a conflicting binding is refused.
|
package/dist/ooxml/partSplice.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { childOrderOf } from "./childOrder.js";
|
|
3
3
|
import { attrsText } from "./element.js";
|
|
4
4
|
import { DocxExportError } from "./errors.js";
|
|
5
|
+
import { xmlnsName } from "./names.js";
|
|
5
6
|
import { parseAttrs, readTag, rootTagAt } from "./tagScan.js";
|
|
6
7
|
import { localPart } from "./xml.js";
|
|
7
8
|
function closingTagAt(xml, open) {
|
|
@@ -111,7 +112,25 @@ function rootPrefixOf(xml) {
|
|
|
111
112
|
return colon === -1 ? "" : name.slice(0, colon + 1);
|
|
112
113
|
}
|
|
113
114
|
var IGNORABLE = /\smc:Ignorable\s*=\s*(["'])([^"']*)\1/;
|
|
114
|
-
function
|
|
115
|
+
function conflictingPrefix(declared, { namespaces }) {
|
|
116
|
+
for (const [prefix, namespace] of Object.entries(namespaces)) {
|
|
117
|
+
if (namespace === void 0) continue;
|
|
118
|
+
const existing = declared.get(xmlnsName(prefix));
|
|
119
|
+
if (existing !== void 0 && existing !== namespace) return prefix;
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
function rootBindingConflict(xml, declarations) {
|
|
124
|
+
const at = rootTagAt(xml);
|
|
125
|
+
const tag = at === -1 ? null : readTag(xml, at);
|
|
126
|
+
if (tag === null || tag.kind === "close") return null;
|
|
127
|
+
const attrs = parseAttrs(
|
|
128
|
+
xml.slice(tag.nameEnd, tag.end - (tag.kind === "empty" ? 2 : 1))
|
|
129
|
+
);
|
|
130
|
+
return attrs === null ? null : conflictingPrefix(new Map(attrs), declarations);
|
|
131
|
+
}
|
|
132
|
+
function withDeclarations(openTag, declarations) {
|
|
133
|
+
const { namespaces, ignorable } = declarations;
|
|
115
134
|
const tag = readTag(openTag, 0);
|
|
116
135
|
const closeLength = tag?.kind === "empty" ? 2 : 1;
|
|
117
136
|
const attrs = tag === null ? null : parseAttrs(openTag.slice(tag.nameEnd, tag.end - closeLength));
|
|
@@ -122,18 +141,18 @@ function withDeclarations(openTag, { namespaces, ignorable }) {
|
|
|
122
141
|
);
|
|
123
142
|
}
|
|
124
143
|
const declared = new Map(attrs);
|
|
144
|
+
const conflict = conflictingPrefix(declared, declarations);
|
|
145
|
+
if (conflict !== null) {
|
|
146
|
+
throw new DocxExportError(
|
|
147
|
+
"unsupported-content",
|
|
148
|
+
`the part root binds ${conflict} to a namespace the writer cannot use`
|
|
149
|
+
);
|
|
150
|
+
}
|
|
125
151
|
const additions = [];
|
|
126
152
|
for (const [prefix, namespace] of Object.entries(namespaces)) {
|
|
127
153
|
if (namespace === void 0) continue;
|
|
128
|
-
const name =
|
|
129
|
-
|
|
130
|
-
if (existing !== void 0 && existing !== namespace) {
|
|
131
|
-
throw new DocxExportError(
|
|
132
|
-
"unsupported-content",
|
|
133
|
-
`the part root binds ${prefix} to a namespace the writer cannot use`
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
if (existing === void 0) additions.push([name, namespace]);
|
|
154
|
+
const name = xmlnsName(prefix);
|
|
155
|
+
if (!declared.has(name)) additions.push([name, namespace]);
|
|
137
156
|
}
|
|
138
157
|
const ignoring = IGNORABLE.exec(openTag);
|
|
139
158
|
let tokens = null;
|
|
@@ -171,6 +190,7 @@ function ensureRootDeclarations(xml, declarations) {
|
|
|
171
190
|
export {
|
|
172
191
|
ensureRootDeclarations,
|
|
173
192
|
partRootProblem,
|
|
193
|
+
rootBindingConflict,
|
|
174
194
|
rootPrefixOf,
|
|
175
195
|
splicePart
|
|
176
196
|
};
|
package/dist/ooxml/tagScan.d.ts
CHANGED
|
@@ -26,9 +26,31 @@ export declare function readTag(source: string, lt: number): Tag | null;
|
|
|
26
26
|
* instruction is not taken for the root. -1 when the source holds no element at all.
|
|
27
27
|
*/
|
|
28
28
|
export declare function rootTagAt(xml: string): number;
|
|
29
|
+
/**
|
|
30
|
+
* Where one attribute stands inside the attribute text of a tag.
|
|
31
|
+
*
|
|
32
|
+
* The spans are what a rewriter that has to leave the rest of the tag alone works from: the quote
|
|
33
|
+
* a value was written in, the spacing around the `=`, and the spelling of a reference inside the
|
|
34
|
+
* value all stand between the spans and are copied rather than written again.
|
|
35
|
+
*/
|
|
36
|
+
export interface AttrSpan {
|
|
37
|
+
nameStart: number;
|
|
38
|
+
nameEnd: number;
|
|
39
|
+
/** The first character inside the quotes, which equals `valueEnd` for an empty value */
|
|
40
|
+
valueStart: number;
|
|
41
|
+
valueEnd: number;
|
|
42
|
+
/** The quote the value was written in, `"` or `'` */
|
|
43
|
+
quote: string;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The attributes of an opening tag as the spans they occupy, in the order they were written.
|
|
47
|
+
* null for a shape a parser would refuse: an unquoted value, a missing name, or text between the
|
|
48
|
+
* attributes that is not whitespace.
|
|
49
|
+
*/
|
|
50
|
+
export declare function attrSpans(raw: string): AttrSpan[] | null;
|
|
29
51
|
/**
|
|
30
52
|
* The attribute text of an opening tag as pairs, references decoded, each name spelled as it was
|
|
31
|
-
* written. null for a shape a parser would refuse:
|
|
32
|
-
*
|
|
53
|
+
* written. null for a shape a parser would refuse: one `attrSpans` turns down, one name given
|
|
54
|
+
* twice, or a reference nothing declares.
|
|
33
55
|
*/
|
|
34
56
|
export declare function parseAttrs(raw: string): XmlAttr[] | null;
|
package/dist/ooxml/tagScan.js
CHANGED
|
@@ -83,25 +83,44 @@ function decodeReferences(value) {
|
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
var ATTRIBUTE = /(\s*)([^\s=/><"']+)\s*=\s*(?:"([^"<]*)"|'([^'<]*)')/y;
|
|
86
|
-
function
|
|
87
|
-
const
|
|
88
|
-
const names = /* @__PURE__ */ new Set();
|
|
86
|
+
function attrSpans(raw) {
|
|
87
|
+
const spans = [];
|
|
89
88
|
let at = 0;
|
|
90
89
|
for (; ; ) {
|
|
91
90
|
ATTRIBUTE.lastIndex = at;
|
|
92
91
|
const matched = ATTRIBUTE.exec(raw);
|
|
93
|
-
if (!matched) return raw.slice(at).trim() === "" ?
|
|
94
|
-
const [, separator, name, doubled, singled] = matched;
|
|
92
|
+
if (!matched) return raw.slice(at).trim() === "" ? spans : null;
|
|
93
|
+
const [whole, separator, name, doubled, singled] = matched;
|
|
95
94
|
if (at > 0 && separator === "") return null;
|
|
95
|
+
const nameStart = at + separator.length;
|
|
96
|
+
const valueEnd = at + whole.length - 1;
|
|
97
|
+
spans.push({
|
|
98
|
+
nameStart,
|
|
99
|
+
nameEnd: nameStart + name.length,
|
|
100
|
+
valueStart: valueEnd - (doubled ?? singled).length,
|
|
101
|
+
valueEnd,
|
|
102
|
+
quote: doubled === void 0 ? "'" : '"'
|
|
103
|
+
});
|
|
104
|
+
at = ATTRIBUTE.lastIndex;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function parseAttrs(raw) {
|
|
108
|
+
const spans = attrSpans(raw);
|
|
109
|
+
if (spans === null) return null;
|
|
110
|
+
const attrs = [];
|
|
111
|
+
const names = /* @__PURE__ */ new Set();
|
|
112
|
+
for (const span of spans) {
|
|
113
|
+
const name = raw.slice(span.nameStart, span.nameEnd);
|
|
96
114
|
if (names.has(name)) return null;
|
|
97
115
|
names.add(name);
|
|
98
|
-
const value = decodeReferences(
|
|
116
|
+
const value = decodeReferences(raw.slice(span.valueStart, span.valueEnd));
|
|
99
117
|
if (value === null) return null;
|
|
100
118
|
attrs.push([name, value]);
|
|
101
|
-
at = ATTRIBUTE.lastIndex;
|
|
102
119
|
}
|
|
120
|
+
return attrs;
|
|
103
121
|
}
|
|
104
122
|
export {
|
|
123
|
+
attrSpans,
|
|
105
124
|
parseAttrs,
|
|
106
125
|
readTag,
|
|
107
126
|
rootTagAt
|
package/dist/ooxml/xml.d.ts
CHANGED
|
@@ -23,6 +23,14 @@ export declare function decodeUtf8(bytes: Uint8Array): {
|
|
|
23
23
|
text: string;
|
|
24
24
|
hadBom: boolean;
|
|
25
25
|
};
|
|
26
|
+
/**
|
|
27
|
+
* The same, and null for bytes no UTF-8 decoder reads: a byte it cannot read decodes to U+FFFD,
|
|
28
|
+
* and writing that text back would put the replacement character in the file in its place.
|
|
29
|
+
*/
|
|
30
|
+
export declare function decodeUtf8Strictly(bytes: Uint8Array): {
|
|
31
|
+
text: string;
|
|
32
|
+
hadBom: boolean;
|
|
33
|
+
} | null;
|
|
26
34
|
export declare function encodeUtf8(text: string, withBom: boolean): Uint8Array;
|
|
27
35
|
/**
|
|
28
36
|
* What reading a package part asks of a runtime: an XML string in, a document out.
|
package/dist/ooxml/xml.js
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
// src/ooxml/xml.ts
|
|
2
2
|
import { DocxImportError } from "./errors.js";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
isKnownPrefix,
|
|
5
|
+
NAMESPACES,
|
|
6
|
+
R_NS,
|
|
7
|
+
W_NS,
|
|
8
|
+
W_PREFIX,
|
|
9
|
+
XMLNS,
|
|
10
|
+
xmlnsName
|
|
11
|
+
} from "./names.js";
|
|
4
12
|
var RESERVED_PREFIXES = /* @__PURE__ */ new Map([
|
|
5
13
|
["w", W_NS],
|
|
6
14
|
["r", R_NS]
|
|
@@ -15,9 +23,22 @@ function escapeXml(value) {
|
|
|
15
23
|
const escaped = value.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll('"', """);
|
|
16
24
|
return FORBIDDEN.test(escaped) ? escaped.replace(ALL_FORBIDDEN, "") : escaped;
|
|
17
25
|
}
|
|
26
|
+
function hasBom(bytes) {
|
|
27
|
+
return bytes.length >= 3 && bytes[0] === 239 && bytes[1] === 187 && bytes[2] === 191;
|
|
28
|
+
}
|
|
18
29
|
function decodeUtf8(bytes) {
|
|
19
|
-
|
|
20
|
-
|
|
30
|
+
return {
|
|
31
|
+
text: new TextDecoder("utf-8").decode(bytes),
|
|
32
|
+
hadBom: hasBom(bytes)
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function decodeUtf8Strictly(bytes) {
|
|
36
|
+
try {
|
|
37
|
+
const text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
38
|
+
return { text, hadBom: hasBom(bytes) };
|
|
39
|
+
} catch {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
21
42
|
}
|
|
22
43
|
function encodeUtf8(text, withBom) {
|
|
23
44
|
return new TextEncoder().encode(withBom ? "\uFEFF" + text : text);
|
|
@@ -83,10 +104,10 @@ function namespaceDecls(xml) {
|
|
|
83
104
|
prefixes.add(prefix);
|
|
84
105
|
}
|
|
85
106
|
prefixes.delete("xml");
|
|
86
|
-
prefixes.delete(
|
|
107
|
+
prefixes.delete(XMLNS);
|
|
87
108
|
return Array.from(prefixes).map((prefix) => {
|
|
88
109
|
const namespace = isKnownPrefix(prefix) ? NAMESPACES[prefix] : `urn:docx-editor:${prefix}`;
|
|
89
|
-
return
|
|
110
|
+
return `${xmlnsName(prefix)}="${namespace}"`;
|
|
90
111
|
}).join(" ");
|
|
91
112
|
}
|
|
92
113
|
function elementChildren(el) {
|
|
@@ -136,6 +157,7 @@ export {
|
|
|
136
157
|
attributeByLocalName,
|
|
137
158
|
childByLocalName,
|
|
138
159
|
decodeUtf8,
|
|
160
|
+
decodeUtf8Strictly,
|
|
139
161
|
elementChildren,
|
|
140
162
|
encodeUtf8,
|
|
141
163
|
escapeXml,
|
package/dist/schema/attrRoles.js
CHANGED
|
@@ -102,6 +102,17 @@ var NODE_ATTR_ROLES = {
|
|
|
102
102
|
temporary: { role: "source", class: "derived" },
|
|
103
103
|
showingPlaceholder: { role: "source", class: "derived" }
|
|
104
104
|
},
|
|
105
|
+
// The same rows again for the control standing inside a paragraph, less the `srcId`: it is no
|
|
106
|
+
// block of the story, so it names no fragment of the session to be written back from.
|
|
107
|
+
sdtEmptyInline: {
|
|
108
|
+
sdtPrefix: { role: "source", class: "preserved" },
|
|
109
|
+
key: { role: "session", class: "identity" },
|
|
110
|
+
contentsLocked: { role: "source", class: "derived" },
|
|
111
|
+
deletionLocked: { role: "source", class: "derived" },
|
|
112
|
+
group: { role: "source", class: "derived" },
|
|
113
|
+
temporary: { role: "source", class: "derived" },
|
|
114
|
+
showingPlaceholder: { role: "source", class: "derived" }
|
|
115
|
+
},
|
|
105
116
|
rawBlock: {
|
|
106
117
|
xml: { role: "source", class: "preserved" },
|
|
107
118
|
srcId: { role: "session", class: "identity" },
|
package/dist/schema/clipboard.js
CHANGED
|
@@ -137,6 +137,8 @@ var CLIPBOARD_NODES = {
|
|
|
137
137
|
// A control holding nothing says only that something is not there, which a copy carries by
|
|
138
138
|
// leaving it out
|
|
139
139
|
sdtEmpty: WRITES_NOTHING,
|
|
140
|
+
// The same of the one standing inside a paragraph, which says nothing the words around it do not
|
|
141
|
+
sdtEmptyInline: WRITES_NOTHING,
|
|
140
142
|
rawBlock: WRITES_NOTHING,
|
|
141
143
|
hardBreak: {
|
|
142
144
|
toClipboardDOM: () => ["br"],
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The attributes each carrier of a content control writes what the control states in.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
4
|
+
* Six carriers state the same facts: the `sdt` mark, the `sdtBlock` container and the two nodes a
|
|
5
|
+
* control holding nothing stands as - `sdtEmpty` between blocks, `sdtEmptyInline` inside a
|
|
6
|
+
* paragraph - under the control's own names, and a cell or a row the file wrapped
|
|
7
|
+
* (`docx/importTable`) under `sdt`-prefixed ones, beside that node's own attributes. The names
|
|
8
|
+
* stand here once, so that a fact the editor reads off a control is declared, drawn, parsed,
|
|
9
|
+
* imported, merged and cleared from one table rather than from a list written out afresh in every
|
|
10
|
+
* module that touches one.
|
|
9
11
|
*
|
|
10
12
|
* Nothing is imported here: the schema itself reads this table (`./docxSchema`), and so does the
|
|
11
13
|
* export, which must reach no editor code at all (`core.test`).
|
|
@@ -14,7 +16,9 @@ import type { Node as PMNode } from "prosemirror-model";
|
|
|
14
16
|
/** The node a block-level content control is read as, which `./docxSchema` declares it under */
|
|
15
17
|
export declare const SDT_BLOCK_NODE = "sdtBlock";
|
|
16
18
|
/** The node a control holding nothing at all is read as, which `./docxSchema` declares beside it */
|
|
17
|
-
export declare const
|
|
19
|
+
export declare const SDT_EMPTY_BLOCK_NODE = "sdtEmpty";
|
|
20
|
+
/** The same control standing inside a paragraph, which is an inline node rather than a block one */
|
|
21
|
+
export declare const SDT_EMPTY_INLINE_NODE = "sdtEmptyInline";
|
|
18
22
|
/** Which attribute one carrier writes each fact under */
|
|
19
23
|
export interface ControlAttrNames {
|
|
20
24
|
prefix: string;
|
|
@@ -39,6 +43,12 @@ export interface ControlFacts {
|
|
|
39
43
|
/** Whether `w:showingPlcHdr` says what stands inside is placeholder text (§17.5.2.39) */
|
|
40
44
|
showingPlaceholder: boolean;
|
|
41
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Whether the first edit of its contents takes the control away. `w:temporary` (§17.5.2.43) says
|
|
48
|
+
* it goes and a lock against deletion (§17.5.2.23) that it may not, and the lock wins
|
|
49
|
+
* (`spec/notes/contentControls.md`).
|
|
50
|
+
*/
|
|
51
|
+
export declare function goesWithEdit(control: ControlFacts): boolean;
|
|
42
52
|
/** Everything a control states other than the opening XML the carrier preserves */
|
|
43
53
|
export type ControlFlags = Omit<ControlFacts, "prefix">;
|
|
44
54
|
/** The control's own names, which the mark and the block-level container both carry */
|
|
@@ -83,12 +93,22 @@ export declare function controlAttrsFromDom(names: ControlAttrNames, dom: HTMLEl
|
|
|
83
93
|
*/
|
|
84
94
|
export declare function isBlockControl(node: PMNode | null | undefined): boolean;
|
|
85
95
|
/**
|
|
86
|
-
* Whether this node is a content control with nothing inside it
|
|
96
|
+
* Whether this node is a block-level content control with nothing inside it
|
|
97
|
+
* (`docx/importSdtBlock`).
|
|
87
98
|
*
|
|
88
99
|
* Deliberately not folded into `isBlockControl`: every caller of that one asks about the blocks a
|
|
89
100
|
* control holds, and this control holds none, so it is no container and stands as an atom.
|
|
90
101
|
*/
|
|
91
|
-
export declare function
|
|
102
|
+
export declare function isEmptyBlockControl(node: PMNode | null | undefined): boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Whether this node is a content control with nothing inside it standing in a paragraph
|
|
105
|
+
* (`docx/wrappers`).
|
|
106
|
+
*
|
|
107
|
+
* The two shapes are told apart rather than counted together, because where such a control stands
|
|
108
|
+
* is the whole of what a caller does with it: one is a block among blocks and the other an atom
|
|
109
|
+
* among runs, and no caller acts on both.
|
|
110
|
+
*/
|
|
111
|
+
export declare function isEmptyInlineControl(node: PMNode | null | undefined): boolean;
|
|
92
112
|
/**
|
|
93
113
|
* Which attributes the control standing at this node writes what it states in, and null where no
|
|
94
114
|
* control stands there.
|
|
@@ -96,10 +116,10 @@ export declare function isEmptyControl(node: PMNode | null | undefined): boolean
|
|
|
96
116
|
* These are the containers a control stands around whole: a cell or a row the file wrapped
|
|
97
117
|
* (`docx/importTable`), which carries the control's opening XML beside its own attributes, and a
|
|
98
118
|
* block-level control, which is the wrapper itself. A control holding nothing is a carrier as
|
|
99
|
-
* well, its extent being the node itself rather than anything standing inside it.
|
|
100
|
-
* no control wrapped carries none of this, and counting it as a control would end
|
|
101
|
-
* the tree at the first one and open every lock standing around the table
|
|
102
|
-
* The wrapped nodes are found by their table role, so a schema built beside the
|
|
103
|
-
* read as well.
|
|
119
|
+
* well, block or inline, its extent being the node itself rather than anything standing inside it.
|
|
120
|
+
* A cell or a row no control wrapped carries none of this, and counting it as a control would end
|
|
121
|
+
* the walk out of the tree at the first one and open every lock standing around the table
|
|
122
|
+
* (`./locks`). The wrapped nodes are found by their table role, so a schema built beside the
|
|
123
|
+
* editor's own is read as well.
|
|
104
124
|
*/
|
|
105
125
|
export declare function controlAttrsOf(node: PMNode | null | undefined): ControlAttrNames | null;
|