@portone/docx-editor 0.6.3 → 0.6.4
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 +29 -0
- 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/inlineControlEdits.d.ts +20 -0
- package/dist/editor/inlineControlEdits.js +69 -0
- package/dist/editor/plugins/keymap.js +18 -7
- 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 +27 -13
- package/dist/schema/controlAttrs.js +14 -6
- package/dist/schema/docxSchema.d.ts +1 -1
- package/dist/schema/docxSchema.js +60 -5
- package/dist/schema/locks.d.ts +37 -5
- package/dist/schema/locks.js +33 -15
- package/dist/styles/classNames.d.ts +2 -0
- package/dist/styles/classNames.js +2 -0
- package/dist/styles.css +13 -0
- package/package.json +1 -1
|
@@ -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;
|
|
@@ -83,12 +87,22 @@ export declare function controlAttrsFromDom(names: ControlAttrNames, dom: HTMLEl
|
|
|
83
87
|
*/
|
|
84
88
|
export declare function isBlockControl(node: PMNode | null | undefined): boolean;
|
|
85
89
|
/**
|
|
86
|
-
* Whether this node is a content control with nothing inside it
|
|
90
|
+
* Whether this node is a block-level content control with nothing inside it
|
|
91
|
+
* (`docx/importSdtBlock`).
|
|
87
92
|
*
|
|
88
93
|
* Deliberately not folded into `isBlockControl`: every caller of that one asks about the blocks a
|
|
89
94
|
* control holds, and this control holds none, so it is no container and stands as an atom.
|
|
90
95
|
*/
|
|
91
|
-
export declare function
|
|
96
|
+
export declare function isEmptyBlockControl(node: PMNode | null | undefined): boolean;
|
|
97
|
+
/**
|
|
98
|
+
* Whether this node is a content control with nothing inside it standing in a paragraph
|
|
99
|
+
* (`docx/wrappers`).
|
|
100
|
+
*
|
|
101
|
+
* The two shapes are told apart rather than counted together, because where such a control stands
|
|
102
|
+
* is the whole of what a caller does with it: one is a block among blocks and the other an atom
|
|
103
|
+
* among runs, and no caller acts on both.
|
|
104
|
+
*/
|
|
105
|
+
export declare function isEmptyInlineControl(node: PMNode | null | undefined): boolean;
|
|
92
106
|
/**
|
|
93
107
|
* Which attributes the control standing at this node writes what it states in, and null where no
|
|
94
108
|
* control stands there.
|
|
@@ -96,10 +110,10 @@ export declare function isEmptyControl(node: PMNode | null | undefined): boolean
|
|
|
96
110
|
* These are the containers a control stands around whole: a cell or a row the file wrapped
|
|
97
111
|
* (`docx/importTable`), which carries the control's opening XML beside its own attributes, and a
|
|
98
112
|
* 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.
|
|
113
|
+
* well, block or inline, its extent being the node itself rather than anything standing inside it.
|
|
114
|
+
* A cell or a row no control wrapped carries none of this, and counting it as a control would end
|
|
115
|
+
* the walk out of the tree at the first one and open every lock standing around the table
|
|
116
|
+
* (`./locks`). The wrapped nodes are found by their table role, so a schema built beside the
|
|
117
|
+
* editor's own is read as well.
|
|
104
118
|
*/
|
|
105
119
|
export declare function controlAttrsOf(node: PMNode | null | undefined): ControlAttrNames | null;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/schema/controlAttrs.ts
|
|
2
2
|
var SDT_BLOCK_NODE = "sdtBlock";
|
|
3
|
-
var
|
|
3
|
+
var SDT_EMPTY_BLOCK_NODE = "sdtEmpty";
|
|
4
|
+
var SDT_EMPTY_INLINE_NODE = "sdtEmptyInline";
|
|
4
5
|
var OWN_CONTROL_ATTRS = {
|
|
5
6
|
prefix: "sdtPrefix",
|
|
6
7
|
contentsLocked: "contentsLocked",
|
|
@@ -98,12 +99,17 @@ var WRAPPED_TABLE_ROLES = ["cell", "row"];
|
|
|
98
99
|
function isBlockControl(node) {
|
|
99
100
|
return node?.type.name === SDT_BLOCK_NODE;
|
|
100
101
|
}
|
|
101
|
-
function
|
|
102
|
-
return node?.type.name ===
|
|
102
|
+
function isEmptyBlockControl(node) {
|
|
103
|
+
return node?.type.name === SDT_EMPTY_BLOCK_NODE;
|
|
104
|
+
}
|
|
105
|
+
function isEmptyInlineControl(node) {
|
|
106
|
+
return node?.type.name === SDT_EMPTY_INLINE_NODE;
|
|
103
107
|
}
|
|
104
108
|
function controlAttrsOf(node) {
|
|
105
109
|
if (!node) return null;
|
|
106
|
-
if (isBlockControl(node) ||
|
|
110
|
+
if (isBlockControl(node) || isEmptyBlockControl(node) || isEmptyInlineControl(node)) {
|
|
111
|
+
return OWN_CONTROL_ATTRS;
|
|
112
|
+
}
|
|
107
113
|
const role = node.type.spec.tableRole;
|
|
108
114
|
if (typeof role !== "string" || !WRAPPED_TABLE_ROLES.includes(role)) {
|
|
109
115
|
return null;
|
|
@@ -115,7 +121,8 @@ export {
|
|
|
115
121
|
OWN_CONTROL_ATTRS,
|
|
116
122
|
PREFIX_DOM_ATTR,
|
|
117
123
|
SDT_BLOCK_NODE,
|
|
118
|
-
|
|
124
|
+
SDT_EMPTY_BLOCK_NODE,
|
|
125
|
+
SDT_EMPTY_INLINE_NODE,
|
|
119
126
|
WRAPPED_CONTROL_ATTRS,
|
|
120
127
|
controlAttrSpecs,
|
|
121
128
|
controlAttrs,
|
|
@@ -125,5 +132,6 @@ export {
|
|
|
125
132
|
controlFactsOf,
|
|
126
133
|
controlToDom,
|
|
127
134
|
isBlockControl,
|
|
128
|
-
|
|
135
|
+
isEmptyBlockControl,
|
|
136
|
+
isEmptyInlineControl
|
|
129
137
|
};
|
|
@@ -23,4 +23,4 @@ export declare function isPageBreak(brAttrs: unknown): boolean;
|
|
|
23
23
|
* ends the line where it stood, and `chip` draws a small box naming the element.
|
|
24
24
|
*/
|
|
25
25
|
export type PreservedDisplay = "hidden" | "text" | "break" | "chip";
|
|
26
|
-
export declare const docxSchema: Schema<"commentEnd" | "commentReference" | "commentStart" | "doc" | "hardBreak" | "image" | "noteReference" | "paragraph" | "rawBlock" | "rawInline" | "rawRunContent" | "sdtBlock" | "sdtEmpty" | "table" | "tableCell" | "tableRow" | "text", "link" | "run" | "sdt" | "tab">;
|
|
26
|
+
export declare const docxSchema: Schema<"commentEnd" | "commentReference" | "commentStart" | "doc" | "hardBreak" | "image" | "noteReference" | "paragraph" | "rawBlock" | "rawInline" | "rawRunContent" | "sdtBlock" | "sdtEmpty" | "sdtEmptyInline" | "table" | "tableCell" | "tableRow" | "text", "link" | "run" | "sdt" | "tab">;
|
|
@@ -43,7 +43,8 @@ import {
|
|
|
43
43
|
OWN_CONTROL_ATTRS,
|
|
44
44
|
PREFIX_DOM_ATTR,
|
|
45
45
|
SDT_BLOCK_NODE,
|
|
46
|
-
|
|
46
|
+
SDT_EMPTY_BLOCK_NODE,
|
|
47
|
+
SDT_EMPTY_INLINE_NODE,
|
|
47
48
|
WRAPPED_CONTROL_ATTRS
|
|
48
49
|
} from "./controlAttrs.js";
|
|
49
50
|
import { imageNodeSpec, runMarkSpec } from "./rendering.js";
|
|
@@ -608,12 +609,12 @@ var docxSchema = new Schema({
|
|
|
608
609
|
* which is what a server's condition-failed clause is meant to look like, and what it states
|
|
609
610
|
* about itself rides back out in the same attributes the container carries.
|
|
610
611
|
*/
|
|
611
|
-
[
|
|
612
|
+
[SDT_EMPTY_BLOCK_NODE]: {
|
|
612
613
|
group: "block modelled",
|
|
613
614
|
atom: true,
|
|
614
|
-
//
|
|
615
|
-
//
|
|
616
|
-
selectable:
|
|
615
|
+
// It draws nothing, so a node selection would put the user in a state with nothing on screen
|
|
616
|
+
// to show it: it goes with a stretch that covers it instead
|
|
617
|
+
selectable: false,
|
|
617
618
|
attrs: {
|
|
618
619
|
/**
|
|
619
620
|
* The fragment of the session this control was opened from, as the container names its
|
|
@@ -1004,6 +1005,60 @@ var docxSchema = new Schema({
|
|
|
1004
1005
|
}
|
|
1005
1006
|
]
|
|
1006
1007
|
},
|
|
1008
|
+
/**
|
|
1009
|
+
* A content control holding nothing that the file wrote inside a paragraph, which is the
|
|
1010
|
+
* `sdtEmpty` node's inline counterpart (§17.5.2.34).
|
|
1011
|
+
*
|
|
1012
|
+
* The inline control is otherwise a mark on the runs it wrapped (`./wrappers`), and a control
|
|
1013
|
+
* holding no run leaves a mark nothing to sit on. So it stands as an atom of its own, taking
|
|
1014
|
+
* no width between the words either side of it, and what it states about itself rides back out
|
|
1015
|
+
* in the attributes every other carrier uses.
|
|
1016
|
+
*
|
|
1017
|
+
* It wears the wrapper marks rather than carrying a `depth` of its own: what it stands inside
|
|
1018
|
+
* is what has to close around it on export, while the control it is has nothing to nest.
|
|
1019
|
+
*/
|
|
1020
|
+
[SDT_EMPTY_INLINE_NODE]: {
|
|
1021
|
+
group: "inline",
|
|
1022
|
+
inline: true,
|
|
1023
|
+
atom: true,
|
|
1024
|
+
marks: "wrapper",
|
|
1025
|
+
// No selection settles on it, for the reason `sdtEmpty` above states
|
|
1026
|
+
selectable: false,
|
|
1027
|
+
attrs: {
|
|
1028
|
+
/** Which control of the document this is, counted as the file was opened (`docx/wrappers`) */
|
|
1029
|
+
key: { default: 0 },
|
|
1030
|
+
/** What the control states, under the names every carrier holds them by (`./controlAttrs`) */
|
|
1031
|
+
...controlAttrSpecs(OWN_CONTROL_ATTRS)
|
|
1032
|
+
},
|
|
1033
|
+
toDOM(node) {
|
|
1034
|
+
const control = controlFactsOf(OWN_CONTROL_ATTRS, node.attrs);
|
|
1035
|
+
return [
|
|
1036
|
+
"span",
|
|
1037
|
+
{
|
|
1038
|
+
class: controlClassName(
|
|
1039
|
+
control,
|
|
1040
|
+
editorClassNames.sdtEmptyInline,
|
|
1041
|
+
editorClassNames.sdtLocked
|
|
1042
|
+
),
|
|
1043
|
+
"data-key": numberText(node.attrs.key),
|
|
1044
|
+
...controlToDom(control)
|
|
1045
|
+
}
|
|
1046
|
+
];
|
|
1047
|
+
},
|
|
1048
|
+
parseDOM: [
|
|
1049
|
+
{
|
|
1050
|
+
tag: `span.${editorClassNames.sdtEmptyInline}`,
|
|
1051
|
+
getAttrs: (dom) => {
|
|
1052
|
+
const prefix = rawXml(dom, PREFIX_DOM_ATTR, SDT_PREFIX);
|
|
1053
|
+
if (prefix === null || prefix === false) return false;
|
|
1054
|
+
return {
|
|
1055
|
+
key: parseInt10(dom.getAttribute("data-key"), 0),
|
|
1056
|
+
...controlAttrsFromDom(OWN_CONTROL_ATTRS, dom, prefix)
|
|
1057
|
+
};
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
]
|
|
1061
|
+
},
|
|
1007
1062
|
/** The node that carries a non-run element inside a paragraph (a bookmark, for instance) exactly as it came */
|
|
1008
1063
|
rawInline: {
|
|
1009
1064
|
group: "inline",
|