@a200888213/gbdocx 0.6.1 → 0.6.3
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/LICENSE +21 -21
- package/README.md +215 -215
- package/index.html +31 -30
- package/package.json +49 -48
- package/serve.mjs +27 -23
- package/src/app/file-io.js +122 -122
- package/src/app/main.js +9 -9
- package/src/app/word/catalog.js +52 -52
- package/src/app/word/comments.js +112 -112
- package/src/app/word/doc.js +113 -113
- package/src/app/word/edit.js +72 -72
- package/src/app/word/file.js +191 -191
- package/src/app/word/hf.js +76 -76
- package/src/app/word/layout.js +155 -155
- package/src/app/word/menus.js +201 -201
- package/src/app/word/notes.js +110 -110
- package/src/app/word/shared.js +22 -22
- package/src/app/word/view.js +159 -159
- package/src/app/word-app.js +487 -487
- package/src/app/word-sheets.js +101 -101
- package/src/kernel/docx/catalog.js +41 -41
- package/src/kernel/docx/codec/blocks.js +123 -123
- package/src/kernel/docx/codec/carry.js +129 -129
- package/src/kernel/docx/codec/comments.js +25 -25
- package/src/kernel/docx/codec/docx.js +198 -198
- package/src/kernel/docx/codec/docxStyles.js +177 -177
- package/src/kernel/docx/codec/fonts.js +195 -195
- package/src/kernel/docx/codec/helpers.js +161 -161
- package/src/kernel/docx/codec/hfHtml.js +152 -152
- package/src/kernel/docx/codec/html.js +739 -739
- package/src/kernel/docx/codec/losses.js +420 -420
- package/src/kernel/docx/codec/markdown.js +170 -170
- package/src/kernel/docx/codec/parts.js +249 -249
- package/src/kernel/docx/codec/read.js +603 -603
- package/src/kernel/docx/codec/render.js +1156 -1156
- package/src/kernel/docx/codec/roundtrip.js +236 -236
- package/src/kernel/docx/codec/write.js +833 -833
- package/src/kernel/docx/comments.js +14 -14
- package/src/kernel/docx/menus.js +348 -348
- package/src/kernel/docx/pageGeometry.js +123 -123
- package/src/kernel/docx/pagination.js +481 -481
- package/src/kernel/docx/watermark.js +45 -45
- package/src/kernel/docx/wordShortcuts.js +37 -37
- package/src/kernel/editor/constants.js +8 -8
- package/src/kernel/editor/engine/blocks.js +594 -594
- package/src/kernel/editor/engine/blocksEnter.js +109 -109
- package/src/kernel/editor/engine/blocksInsert.js +254 -254
- package/src/kernel/editor/engine/blocksStructure.js +226 -226
- package/src/kernel/editor/engine/commands.js +181 -181
- package/src/kernel/editor/engine/dom.js +646 -646
- package/src/kernel/editor/engine/editor.js +213 -213
- package/src/kernel/editor/engine/history.js +149 -149
- package/src/kernel/editor/engine/inline.js +656 -656
- package/src/kernel/editor/engine/input.js +554 -554
- package/src/kernel/editor/engine/interaction.js +137 -137
- package/src/kernel/editor/engine/pointer.js +39 -39
- package/src/kernel/editor/engine/revision.js +138 -138
- package/src/kernel/editor/engine/schema.js +417 -417
- package/src/kernel/editor/engine/search.js +113 -113
- package/src/kernel/editor/engine/selection.js +119 -119
- package/src/kernel/editor/engine/state.js +118 -118
- package/src/kernel/editor/engine/tableClip.js +154 -154
- package/src/kernel/editor/engine/tableGrid.js +55 -55
- package/src/kernel/editor/engine/tableInteraction.js +388 -388
- package/src/kernel/editor/engine/tableMenu.js +250 -250
- package/src/kernel/editor/engine/tableOps.js +174 -174
- package/src/kernel/editor/engine/tables.js +732 -732
- package/src/kernel/editor/overlayGeometry.js +20 -20
- package/src/kernel/editor/pasteClean.js +293 -293
- package/src/kernel/editor/utils.js +48 -48
- package/src/kernel/keyboard.js +114 -114
- package/src/kernel/notify.js +54 -54
- package/src/kernel/textStyles.js +84 -84
- package/src/kernel/util/clipboard.js +89 -89
- package/src/kernel/util/imageFile.js +91 -91
- package/src/kernel/util/safeValue.js +25 -25
- package/src/kernel/util/shortcutKey.js +33 -33
- package/src/surface/editor-surface.js +603 -600
- package/src/ui/color-panel.js +69 -69
- package/src/ui/context-menu.js +135 -133
- package/src/ui/dialogs.js +249 -249
- package/src/ui/find-bar.js +151 -151
- package/src/ui/icons.js +245 -137
- package/src/ui/menubar.js +28 -28
- package/src/ui/panels.js +115 -115
- package/src/ui/popup.js +79 -79
- package/src/ui/prompt.js +89 -89
- package/src/ui/ribbon.js +485 -485
- package/src/ui/special-chars.js +39 -39
- package/src/ui/statusbar.js +81 -81
- package/src/ui/toolbar.js +32 -32
- package/styles/app.css +970 -970
- package/styles/editor-content.css +440 -440
- package/styles/tokens.css +154 -154
- package/styles/word-panel.css +410 -410
|
@@ -1,161 +1,161 @@
|
|
|
1
|
-
// [文件] web·docx·codec·helpers —— 取值小工具(读/渲染/写出共用:整型/浮点属性、关系属性、制表位编码、页眉页脚清洗)
|
|
2
|
-
import { REL_NS } from 'gbooxml';
|
|
3
|
-
import { isSafeHref, sanitizeFontName } from '../../util/safeValue.js';
|
|
4
|
-
import { HF_FIELD_KINDS } from '../../editor/engine/schema.js';
|
|
5
|
-
|
|
6
|
-
export const attrIntOf = (n, local) => {
|
|
7
|
-
if (!n) return 0;
|
|
8
|
-
const v = Number.parseInt(n.attr(local), 10);
|
|
9
|
-
return Number.isNaN(v) ? 0 : v;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export const attrFloat = (n, local) => {
|
|
13
|
-
if (!n) return 0;
|
|
14
|
-
const v = Number.parseFloat(n.attr(local));
|
|
15
|
-
return Number.isNaN(v) ? 0 : v;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
/** HTML 文本段解实体:页眉页脚受限 HTML 的文本读出时已做过 HTML 转义,
|
|
19
|
-
* 写出端必须先解回原文再按 XML 转义;直接 xmlEscape 会二次转义(& → &) */
|
|
20
|
-
const HTML_ENTITIES = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: '\u00a0' };
|
|
21
|
-
export const decodeHtmlEntities = (text) =>
|
|
22
|
-
String(text ?? '').replace(/&(#x[0-9a-fA-F]+|#[0-9]+|[a-zA-Z]+);/g, (match, ref) => {
|
|
23
|
-
if (ref[0] === '#') {
|
|
24
|
-
const code = ref[1] === 'x' || ref[1] === 'X' ? Number.parseInt(ref.slice(2), 16) : Number.parseInt(ref.slice(1), 10);
|
|
25
|
-
return Number.isFinite(code) && code >= 0 && code <= 0x10ffff ? String.fromCodePoint(code) : match;
|
|
26
|
-
}
|
|
27
|
-
return HTML_ENTITIES[ref] ?? match;
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
/** 取关系属性(r:id / r:embed)。它们在 r: 命名空间里,只按本地名取会撞上同名的非关系属性 */
|
|
31
|
-
export const relAttr = (n, local) => {
|
|
32
|
-
const v = n.attrNS(REL_NS, local);
|
|
33
|
-
if (v !== '') return v;
|
|
34
|
-
for (const a of n.attrs) {
|
|
35
|
-
if (a.local === local && a.value.startsWith('rId')) return a.value;
|
|
36
|
-
}
|
|
37
|
-
return '';
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
/* ---------- 自定义制表位(w:tabs)的 HTML 载体 ---------- */
|
|
41
|
-
|
|
42
|
-
/** 制表位定义的段落属性名(读入编码、写出还原;内核把它当透明属性原样保留) */
|
|
43
|
-
export const TABS_ATTR = 'data-docx-tabs';
|
|
44
|
-
|
|
45
|
-
/** 表外观的编码属性(表样式摊平后的网格线/单元格边距;"none" 或 "single:sz:color" / "t:l:b:r" twips) */
|
|
46
|
-
export const TBL_BDR_ATTR = 'data-docx-tbl-bdr';
|
|
47
|
-
export const TBL_MAR_ATTR = 'data-docx-tbl-mar';
|
|
48
|
-
|
|
49
|
-
/** 制表位对齐 / 前导符白名单(写出时非法值回落,防写坏部件) */
|
|
50
|
-
const TAB_ALIGNS = new Set(['left', 'center', 'right', 'decimal', 'bar', 'num', 'start', 'end']);
|
|
51
|
-
const TAB_LEADERS = new Set(['dot', 'hyphen', 'underscore', 'heavy', 'middleDot']);
|
|
52
|
-
|
|
53
|
-
/** w:tabs 节点 → data 属性编码("pos:align:leader" 空格分隔;显示走默认制表位近似,位置信息只为往返保留) */
|
|
54
|
-
export const encodeTabs = (tabs) =>
|
|
55
|
-
tabs
|
|
56
|
-
.children('tab')
|
|
57
|
-
.map((t) => {
|
|
58
|
-
const pos = attrIntOf(t, 'pos');
|
|
59
|
-
if (pos <= 0) return '';
|
|
60
|
-
const align = t.attr('val');
|
|
61
|
-
const leader = t.attr('leader');
|
|
62
|
-
return align === '' && leader === '' ? String(pos) : `${pos}:${align}:${leader}`;
|
|
63
|
-
})
|
|
64
|
-
.filter(Boolean)
|
|
65
|
-
.join(' ');
|
|
66
|
-
|
|
67
|
-
/** data 属性编码 → 制表位数组 [{pos, align, leader}](白名单外的值直接丢弃) */
|
|
68
|
-
export const decodeTabs = (value) => {
|
|
69
|
-
const out = [];
|
|
70
|
-
for (const item of String(value || '').split(' ')) {
|
|
71
|
-
if (item === '') continue;
|
|
72
|
-
const [posRaw, alignRaw = '', leaderRaw = ''] = item.split(':');
|
|
73
|
-
const pos = Number.parseInt(posRaw, 10);
|
|
74
|
-
if (!(pos > 0)) continue;
|
|
75
|
-
out.push({
|
|
76
|
-
pos,
|
|
77
|
-
align: TAB_ALIGNS.has(alignRaw) ? alignRaw : '',
|
|
78
|
-
leader: TAB_LEADERS.has(leaderRaw) ? leaderRaw : ''
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
return out;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
/* ---------- 页眉页脚受限 HTML 清洗(就地编辑与显示入口共用) ---------- */
|
|
85
|
-
|
|
86
|
-
/** 页眉页脚允许的行内标签(与读端生成器、写端 hfRunsXml 的标签词表三方对齐) */
|
|
87
|
-
const HF_INLINE_TAGS = new Set(['B', 'STRONG', 'I', 'EM', 'U', 'S', 'SUP', 'SUB']);
|
|
88
|
-
|
|
89
|
-
/** 就地编辑里回车/粘贴产出的块级容器:剥壳并在前一个兄弟后补换行 */
|
|
90
|
-
const HF_BLOCKY = new Set(['P', 'DIV', 'LI', 'UL', 'OL', 'TABLE', 'TR', 'TD', 'TH', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'BLOCKQUOTE', 'PRE', 'SECTION', 'ARTICLE', 'HEADER', 'FOOTER']);
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* rPr → 行内标签名数组(从内到外的处理顺序):b / i / u / s / sup|sub。
|
|
94
|
-
* [坑] Word 保真:w:b / w:i / w:strike 存在但 val="0"/"false" 是「显式关掉」,只看节点在不在会误加格式;w:u 的 val="none" 同理。
|
|
95
|
-
* 正文渲染(render.js runHTML)与页眉页脚渲染(hfHtml.js runHtml)共用,禁各处再拼一份标签判据。
|
|
96
|
-
*/
|
|
97
|
-
export const rPrInlineTags = (rPr) => {
|
|
98
|
-
if (!rPr) return [];
|
|
99
|
-
const on = (n) => Boolean(n) && n.attr('val') !== '0' && n.attr('val') !== 'false';
|
|
100
|
-
const tags = [];
|
|
101
|
-
if (on(rPr.child('b'))) tags.push('b');
|
|
102
|
-
if (on(rPr.child('i'))) tags.push('i');
|
|
103
|
-
const u = rPr.child('u');
|
|
104
|
-
if (u && u.attr('val') !== 'none') tags.push('u');
|
|
105
|
-
if (on(rPr.child('strike'))) tags.push('s');
|
|
106
|
-
const va = rPr.child('vertAlign');
|
|
107
|
-
if (va) {
|
|
108
|
-
if (va.attr('val') === 'superscript') tags.push('sup');
|
|
109
|
-
else if (va.attr('val') === 'subscript') tags.push('sub');
|
|
110
|
-
}
|
|
111
|
-
return tags;
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* 页眉页脚受限 HTML 清洗(就地编辑产物入库前 / 显示值兜底过一遍;值是 v-html 的输入,必须在此收口)
|
|
116
|
-
* 白名单外的标签剥壳留文字;span 只留色/字号/字体三项样式与域占位;链接只留安全协议
|
|
117
|
-
*/
|
|
118
|
-
export const cleanHfHtml = (html) => {
|
|
119
|
-
const body = new DOMParser().parseFromString(`<body>${html || ''}</body>`, 'text/html').body;
|
|
120
|
-
for (const el of Array.from(body.querySelectorAll('*'))) {
|
|
121
|
-
const tag = el.tagName;
|
|
122
|
-
if (tag === 'BR') continue;
|
|
123
|
-
if (HF_INLINE_TAGS.has(tag)) {
|
|
124
|
-
for (const attr of Array.from(el.attributes)) el.removeAttribute(attr.name);
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
if (tag === 'SPAN') {
|
|
128
|
-
const fld = (el.getAttribute('data-fld') || '').toUpperCase();
|
|
129
|
-
const kept = [];
|
|
130
|
-
for (const decl of (el.getAttribute('style') || '').split(';')) {
|
|
131
|
-
const idx = decl.indexOf(':');
|
|
132
|
-
if (idx < 0) continue;
|
|
133
|
-
const key = decl.slice(0, idx).trim().toLowerCase();
|
|
134
|
-
const val = decl.slice(idx + 1).trim();
|
|
135
|
-
if (val === '') continue;
|
|
136
|
-
if (key === 'color' && /^#[0-9a-f]{3,6}$/i.test(val)) kept.push(`color:${val}`);
|
|
137
|
-
else if (key === 'font-size' && /^\d+(?:\.\d+)?px$/.test(val)) kept.push(`font-size:${val}`);
|
|
138
|
-
else if (key === 'font-family') {
|
|
139
|
-
const f = sanitizeFontName(val);
|
|
140
|
-
if (f !== '') kept.push(`font-family:${f}`);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
const validFld = HF_FIELD_KINDS.has(fld);
|
|
144
|
-
for (const attr of Array.from(el.attributes)) el.removeAttribute(attr.name);
|
|
145
|
-
if (kept.length) el.setAttribute('style', kept.join(';'));
|
|
146
|
-
if (validFld) el.setAttribute('data-fld', fld);
|
|
147
|
-
if (!el.hasAttributes()) el.replaceWith(...el.childNodes); // 空壳剥掉
|
|
148
|
-
continue;
|
|
149
|
-
}
|
|
150
|
-
if (tag === 'A') {
|
|
151
|
-
const href = el.getAttribute('href') || '';
|
|
152
|
-
for (const attr of Array.from(el.attributes)) el.removeAttribute(attr.name);
|
|
153
|
-
if (isSafeHref(href)) el.setAttribute('href', href);
|
|
154
|
-
continue;
|
|
155
|
-
}
|
|
156
|
-
// 块级容器:剥壳并在兄弟之间补换行
|
|
157
|
-
if (HF_BLOCKY.has(tag) && el.previousSibling) el.before(document.createElement('br'));
|
|
158
|
-
el.replaceWith(...el.childNodes);
|
|
159
|
-
}
|
|
160
|
-
return body.innerHTML;
|
|
161
|
-
};
|
|
1
|
+
// [文件] web·docx·codec·helpers —— 取值小工具(读/渲染/写出共用:整型/浮点属性、关系属性、制表位编码、页眉页脚清洗)
|
|
2
|
+
import { REL_NS } from 'gbooxml';
|
|
3
|
+
import { isSafeHref, sanitizeFontName } from '../../util/safeValue.js';
|
|
4
|
+
import { HF_FIELD_KINDS } from '../../editor/engine/schema.js';
|
|
5
|
+
|
|
6
|
+
export const attrIntOf = (n, local) => {
|
|
7
|
+
if (!n) return 0;
|
|
8
|
+
const v = Number.parseInt(n.attr(local), 10);
|
|
9
|
+
return Number.isNaN(v) ? 0 : v;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const attrFloat = (n, local) => {
|
|
13
|
+
if (!n) return 0;
|
|
14
|
+
const v = Number.parseFloat(n.attr(local));
|
|
15
|
+
return Number.isNaN(v) ? 0 : v;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/** HTML 文本段解实体:页眉页脚受限 HTML 的文本读出时已做过 HTML 转义,
|
|
19
|
+
* 写出端必须先解回原文再按 XML 转义;直接 xmlEscape 会二次转义(& → &amp;) */
|
|
20
|
+
const HTML_ENTITIES = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: '\u00a0' };
|
|
21
|
+
export const decodeHtmlEntities = (text) =>
|
|
22
|
+
String(text ?? '').replace(/&(#x[0-9a-fA-F]+|#[0-9]+|[a-zA-Z]+);/g, (match, ref) => {
|
|
23
|
+
if (ref[0] === '#') {
|
|
24
|
+
const code = ref[1] === 'x' || ref[1] === 'X' ? Number.parseInt(ref.slice(2), 16) : Number.parseInt(ref.slice(1), 10);
|
|
25
|
+
return Number.isFinite(code) && code >= 0 && code <= 0x10ffff ? String.fromCodePoint(code) : match;
|
|
26
|
+
}
|
|
27
|
+
return HTML_ENTITIES[ref] ?? match;
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
/** 取关系属性(r:id / r:embed)。它们在 r: 命名空间里,只按本地名取会撞上同名的非关系属性 */
|
|
31
|
+
export const relAttr = (n, local) => {
|
|
32
|
+
const v = n.attrNS(REL_NS, local);
|
|
33
|
+
if (v !== '') return v;
|
|
34
|
+
for (const a of n.attrs) {
|
|
35
|
+
if (a.local === local && a.value.startsWith('rId')) return a.value;
|
|
36
|
+
}
|
|
37
|
+
return '';
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/* ---------- 自定义制表位(w:tabs)的 HTML 载体 ---------- */
|
|
41
|
+
|
|
42
|
+
/** 制表位定义的段落属性名(读入编码、写出还原;内核把它当透明属性原样保留) */
|
|
43
|
+
export const TABS_ATTR = 'data-docx-tabs';
|
|
44
|
+
|
|
45
|
+
/** 表外观的编码属性(表样式摊平后的网格线/单元格边距;"none" 或 "single:sz:color" / "t:l:b:r" twips) */
|
|
46
|
+
export const TBL_BDR_ATTR = 'data-docx-tbl-bdr';
|
|
47
|
+
export const TBL_MAR_ATTR = 'data-docx-tbl-mar';
|
|
48
|
+
|
|
49
|
+
/** 制表位对齐 / 前导符白名单(写出时非法值回落,防写坏部件) */
|
|
50
|
+
const TAB_ALIGNS = new Set(['left', 'center', 'right', 'decimal', 'bar', 'num', 'start', 'end']);
|
|
51
|
+
const TAB_LEADERS = new Set(['dot', 'hyphen', 'underscore', 'heavy', 'middleDot']);
|
|
52
|
+
|
|
53
|
+
/** w:tabs 节点 → data 属性编码("pos:align:leader" 空格分隔;显示走默认制表位近似,位置信息只为往返保留) */
|
|
54
|
+
export const encodeTabs = (tabs) =>
|
|
55
|
+
tabs
|
|
56
|
+
.children('tab')
|
|
57
|
+
.map((t) => {
|
|
58
|
+
const pos = attrIntOf(t, 'pos');
|
|
59
|
+
if (pos <= 0) return '';
|
|
60
|
+
const align = t.attr('val');
|
|
61
|
+
const leader = t.attr('leader');
|
|
62
|
+
return align === '' && leader === '' ? String(pos) : `${pos}:${align}:${leader}`;
|
|
63
|
+
})
|
|
64
|
+
.filter(Boolean)
|
|
65
|
+
.join(' ');
|
|
66
|
+
|
|
67
|
+
/** data 属性编码 → 制表位数组 [{pos, align, leader}](白名单外的值直接丢弃) */
|
|
68
|
+
export const decodeTabs = (value) => {
|
|
69
|
+
const out = [];
|
|
70
|
+
for (const item of String(value || '').split(' ')) {
|
|
71
|
+
if (item === '') continue;
|
|
72
|
+
const [posRaw, alignRaw = '', leaderRaw = ''] = item.split(':');
|
|
73
|
+
const pos = Number.parseInt(posRaw, 10);
|
|
74
|
+
if (!(pos > 0)) continue;
|
|
75
|
+
out.push({
|
|
76
|
+
pos,
|
|
77
|
+
align: TAB_ALIGNS.has(alignRaw) ? alignRaw : '',
|
|
78
|
+
leader: TAB_LEADERS.has(leaderRaw) ? leaderRaw : ''
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/* ---------- 页眉页脚受限 HTML 清洗(就地编辑与显示入口共用) ---------- */
|
|
85
|
+
|
|
86
|
+
/** 页眉页脚允许的行内标签(与读端生成器、写端 hfRunsXml 的标签词表三方对齐) */
|
|
87
|
+
const HF_INLINE_TAGS = new Set(['B', 'STRONG', 'I', 'EM', 'U', 'S', 'SUP', 'SUB']);
|
|
88
|
+
|
|
89
|
+
/** 就地编辑里回车/粘贴产出的块级容器:剥壳并在前一个兄弟后补换行 */
|
|
90
|
+
const HF_BLOCKY = new Set(['P', 'DIV', 'LI', 'UL', 'OL', 'TABLE', 'TR', 'TD', 'TH', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'BLOCKQUOTE', 'PRE', 'SECTION', 'ARTICLE', 'HEADER', 'FOOTER']);
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* rPr → 行内标签名数组(从内到外的处理顺序):b / i / u / s / sup|sub。
|
|
94
|
+
* [坑] Word 保真:w:b / w:i / w:strike 存在但 val="0"/"false" 是「显式关掉」,只看节点在不在会误加格式;w:u 的 val="none" 同理。
|
|
95
|
+
* 正文渲染(render.js runHTML)与页眉页脚渲染(hfHtml.js runHtml)共用,禁各处再拼一份标签判据。
|
|
96
|
+
*/
|
|
97
|
+
export const rPrInlineTags = (rPr) => {
|
|
98
|
+
if (!rPr) return [];
|
|
99
|
+
const on = (n) => Boolean(n) && n.attr('val') !== '0' && n.attr('val') !== 'false';
|
|
100
|
+
const tags = [];
|
|
101
|
+
if (on(rPr.child('b'))) tags.push('b');
|
|
102
|
+
if (on(rPr.child('i'))) tags.push('i');
|
|
103
|
+
const u = rPr.child('u');
|
|
104
|
+
if (u && u.attr('val') !== 'none') tags.push('u');
|
|
105
|
+
if (on(rPr.child('strike'))) tags.push('s');
|
|
106
|
+
const va = rPr.child('vertAlign');
|
|
107
|
+
if (va) {
|
|
108
|
+
if (va.attr('val') === 'superscript') tags.push('sup');
|
|
109
|
+
else if (va.attr('val') === 'subscript') tags.push('sub');
|
|
110
|
+
}
|
|
111
|
+
return tags;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 页眉页脚受限 HTML 清洗(就地编辑产物入库前 / 显示值兜底过一遍;值是 v-html 的输入,必须在此收口)
|
|
116
|
+
* 白名单外的标签剥壳留文字;span 只留色/字号/字体三项样式与域占位;链接只留安全协议
|
|
117
|
+
*/
|
|
118
|
+
export const cleanHfHtml = (html) => {
|
|
119
|
+
const body = new DOMParser().parseFromString(`<body>${html || ''}</body>`, 'text/html').body;
|
|
120
|
+
for (const el of Array.from(body.querySelectorAll('*'))) {
|
|
121
|
+
const tag = el.tagName;
|
|
122
|
+
if (tag === 'BR') continue;
|
|
123
|
+
if (HF_INLINE_TAGS.has(tag)) {
|
|
124
|
+
for (const attr of Array.from(el.attributes)) el.removeAttribute(attr.name);
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (tag === 'SPAN') {
|
|
128
|
+
const fld = (el.getAttribute('data-fld') || '').toUpperCase();
|
|
129
|
+
const kept = [];
|
|
130
|
+
for (const decl of (el.getAttribute('style') || '').split(';')) {
|
|
131
|
+
const idx = decl.indexOf(':');
|
|
132
|
+
if (idx < 0) continue;
|
|
133
|
+
const key = decl.slice(0, idx).trim().toLowerCase();
|
|
134
|
+
const val = decl.slice(idx + 1).trim();
|
|
135
|
+
if (val === '') continue;
|
|
136
|
+
if (key === 'color' && /^#[0-9a-f]{3,6}$/i.test(val)) kept.push(`color:${val}`);
|
|
137
|
+
else if (key === 'font-size' && /^\d+(?:\.\d+)?px$/.test(val)) kept.push(`font-size:${val}`);
|
|
138
|
+
else if (key === 'font-family') {
|
|
139
|
+
const f = sanitizeFontName(val);
|
|
140
|
+
if (f !== '') kept.push(`font-family:${f}`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const validFld = HF_FIELD_KINDS.has(fld);
|
|
144
|
+
for (const attr of Array.from(el.attributes)) el.removeAttribute(attr.name);
|
|
145
|
+
if (kept.length) el.setAttribute('style', kept.join(';'));
|
|
146
|
+
if (validFld) el.setAttribute('data-fld', fld);
|
|
147
|
+
if (!el.hasAttributes()) el.replaceWith(...el.childNodes); // 空壳剥掉
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (tag === 'A') {
|
|
151
|
+
const href = el.getAttribute('href') || '';
|
|
152
|
+
for (const attr of Array.from(el.attributes)) el.removeAttribute(attr.name);
|
|
153
|
+
if (isSafeHref(href)) el.setAttribute('href', href);
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
// 块级容器:剥壳并在兄弟之间补换行
|
|
157
|
+
if (HF_BLOCKY.has(tag) && el.previousSibling) el.before(document.createElement('br'));
|
|
158
|
+
el.replaceWith(...el.childNodes);
|
|
159
|
+
}
|
|
160
|
+
return body.innerHTML;
|
|
161
|
+
};
|
|
@@ -1,152 +1,152 @@
|
|
|
1
|
-
// [文件] web·docx·codec·hfHtml —— 页眉/页脚部件 → 受限 HTML(格式 + 域占位;与写出端 hfRunsXml、就地编辑清洗器三方对齐)
|
|
2
|
-
import { isHexColor } from 'gbooxml';
|
|
3
|
-
import { escapeHtml } from 'guabiutils';
|
|
4
|
-
import { halfPointsToPx, round2 } from './docx.js';
|
|
5
|
-
import { fontFamilyOf } from './fonts.js';
|
|
6
|
-
import { attrFloat, relAttr, rPrInlineTags } from './helpers.js';
|
|
7
|
-
import { isSafeHref } from '../../util/safeValue.js';
|
|
8
|
-
import { FIELD_ATTR, HF_FIELD_KINDS } from '../../editor/engine/schema.js';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* 页眉/页脚部件 → 受限 HTML(b/i/u/s/sub/sup + 色/字号/字体 span + 域占位 span + 链接)
|
|
12
|
-
* [设计] 只读展示与就地编辑共用这一套受控标签:文本一律转义
|
|
13
|
-
*/
|
|
14
|
-
export const hfHtmlOf = (rd, tree) => {
|
|
15
|
-
const parts = [];
|
|
16
|
-
const collect = (n) => {
|
|
17
|
-
for (const k of n.kids) {
|
|
18
|
-
if (k.name === 'p') parts.push(k);
|
|
19
|
-
else if (k.name === 'sdt') {
|
|
20
|
-
const c = k.child('sdtContent');
|
|
21
|
-
if (c) collect(c);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
collect(tree);
|
|
26
|
-
return parts
|
|
27
|
-
.map((p) => hfParaHtml(rd, p))
|
|
28
|
-
.filter((s) => s !== '')
|
|
29
|
-
.join('<br>');
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
/** 文本框里的段落 → 行内 HTML(wps:txbx 的 w:txbxContent / VML 的 v:textbox;页码与页脚说明常排在这里) */
|
|
33
|
-
const textBoxHtml = (rd, n) => {
|
|
34
|
-
const out = [];
|
|
35
|
-
const walk = (x) => {
|
|
36
|
-
for (const k of x.kids) {
|
|
37
|
-
if (k.name === 'txbxContent') {
|
|
38
|
-
for (const p of k.kids) {
|
|
39
|
-
if (p.name !== 'p') continue;
|
|
40
|
-
const html = hfParaHtml(rd, p);
|
|
41
|
-
if (html !== '') out.push(html);
|
|
42
|
-
}
|
|
43
|
-
} else walk(k);
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
walk(n);
|
|
47
|
-
return out.join('<br>');
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
/** 页眉页脚一个段落 → 受限 HTML(run 格式 + 域占位;未建模的图形/符号静默跳过,对象丢失另由 losses 盘点) */
|
|
51
|
-
const hfParaHtml = (rd, p) => {
|
|
52
|
-
let html = '';
|
|
53
|
-
// 域状态:''=无 | 'target'(占位已出,缓存结果跳过)| 'keep'(保留缓存结果文本)
|
|
54
|
-
let field = '';
|
|
55
|
-
const runHtml = (r) => {
|
|
56
|
-
const rPr = r.child('rPr');
|
|
57
|
-
let open = '';
|
|
58
|
-
let close = '';
|
|
59
|
-
const style = [];
|
|
60
|
-
if (rPr) {
|
|
61
|
-
// 行内标签(b/i/u/s/上下标;Word 保真判据见 helpers.rPrInlineTags)
|
|
62
|
-
for (const t of rPrInlineTags(rPr)) {
|
|
63
|
-
open += `<${t}>`;
|
|
64
|
-
close = `</${t}>${close}`;
|
|
65
|
-
}
|
|
66
|
-
const c = rPr.child('color');
|
|
67
|
-
if (c && isHexColor(c.attr('val'))) style.push(`color:#${c.attr('val').toLowerCase()}`);
|
|
68
|
-
const sz = rPr.child('sz');
|
|
69
|
-
if (sz) {
|
|
70
|
-
const hp = attrFloat(sz, 'val');
|
|
71
|
-
// 与正文同口径:半点折 px 留两位小数,截断会让页眉页脚的字宽偏移
|
|
72
|
-
if (hp > 0) style.push(`font-size:${round2(halfPointsToPx(hp))}px`);
|
|
73
|
-
}
|
|
74
|
-
const f = rPr.child('rFonts');
|
|
75
|
-
if (f) {
|
|
76
|
-
// 与正文同一份字体链口径(ascii + eastAsia 分工),禁各写一份
|
|
77
|
-
const fam = fontFamilyOf(f.attr('ascii') || f.attr('hAnsi'), f.attr('eastAsia'));
|
|
78
|
-
if (fam !== '') style.push(`font-family:${escapeHtml(fam)}`);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
let body = '';
|
|
82
|
-
// mc:AlternateContent 是「新老写作器二选一」的壳:只认 Choice 分支,否则页码文本框整块取不到
|
|
83
|
-
const kids = [];
|
|
84
|
-
for (const k of r.kids) {
|
|
85
|
-
const alt = k.name === 'AlternateContent' ? k.child('Choice') || k.child('Fallback') : null;
|
|
86
|
-
if (alt) kids.push(...alt.kids);
|
|
87
|
-
else kids.push(k);
|
|
88
|
-
}
|
|
89
|
-
for (const k of kids) {
|
|
90
|
-
switch (k.name) {
|
|
91
|
-
case 'fldChar':
|
|
92
|
-
if (k.attr('fldCharType') === 'end') field = '';
|
|
93
|
-
break;
|
|
94
|
-
case 'instrText': {
|
|
95
|
-
const code = k.text.trim().toUpperCase().split(/\s+/)[0] || '';
|
|
96
|
-
if (HF_FIELD_KINDS.has(code)) {
|
|
97
|
-
body += `<span ${FIELD_ATTR}="${code}"></span>`;
|
|
98
|
-
field = 'target';
|
|
99
|
-
} else {
|
|
100
|
-
field = 'keep';
|
|
101
|
-
}
|
|
102
|
-
break;
|
|
103
|
-
}
|
|
104
|
-
case 't':
|
|
105
|
-
// 目标域的缓存结果不进 HTML(显示按页现算,写出时重建域)
|
|
106
|
-
if (field !== 'target') body += escapeHtml(k.text);
|
|
107
|
-
break;
|
|
108
|
-
case 'tab':
|
|
109
|
-
body += ' ';
|
|
110
|
-
break;
|
|
111
|
-
case 'br':
|
|
112
|
-
case 'cr':
|
|
113
|
-
body += '<br>';
|
|
114
|
-
break;
|
|
115
|
-
// [坑] 公文页码常常排在一个文本框里(WPS 存成 wps:txbx / VML v:textbox),
|
|
116
|
-
// 只认段落的纯文字会把「- 1 -」整条丢掉,页面右下角只剩一个裸数字
|
|
117
|
-
case 'drawing':
|
|
118
|
-
case 'pict':
|
|
119
|
-
body += textBoxHtml(rd, k);
|
|
120
|
-
break;
|
|
121
|
-
default:
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
if (body === '') return '';
|
|
125
|
-
if (style.length) {
|
|
126
|
-
open += `<span style="${style.join(';')};">`;
|
|
127
|
-
close = `</span>${close}`;
|
|
128
|
-
}
|
|
129
|
-
return open + body + close;
|
|
130
|
-
};
|
|
131
|
-
const walk = (n) => {
|
|
132
|
-
for (const k of n.kids) {
|
|
133
|
-
if (k.name === 'r') html += runHtml(k);
|
|
134
|
-
else if (k.name === 'hyperlink') {
|
|
135
|
-
const href = rd.docRel.get(relAttr(k, 'id'))?.target || '';
|
|
136
|
-
let inner = '';
|
|
137
|
-
for (const r of k.children('r')) inner += runHtml(r);
|
|
138
|
-
html += inner === '' ? '' : isSafeHref(href) ? `<a href="${escapeHtml(href)}">${inner}</a>` : inner;
|
|
139
|
-
} else if (k.name === 'ins' || k.name === 'del') walk(k);
|
|
140
|
-
else if (k.name === 'sdt') {
|
|
141
|
-
const c = k.child('sdtContent');
|
|
142
|
-
if (c) walk(c);
|
|
143
|
-
} else if (k.name === 'fldSimple') {
|
|
144
|
-
const code = (k.attr('instr') || '').trim().toUpperCase().split(/\s+/)[0] || '';
|
|
145
|
-
if (HF_FIELD_KINDS.has(code)) html += `<span ${FIELD_ATTR}="${code}"></span>`;
|
|
146
|
-
else for (const r of k.children('r')) html += runHtml(r);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
walk(p);
|
|
151
|
-
return html;
|
|
152
|
-
};
|
|
1
|
+
// [文件] web·docx·codec·hfHtml —— 页眉/页脚部件 → 受限 HTML(格式 + 域占位;与写出端 hfRunsXml、就地编辑清洗器三方对齐)
|
|
2
|
+
import { isHexColor } from 'gbooxml';
|
|
3
|
+
import { escapeHtml } from 'guabiutils';
|
|
4
|
+
import { halfPointsToPx, round2 } from './docx.js';
|
|
5
|
+
import { fontFamilyOf } from './fonts.js';
|
|
6
|
+
import { attrFloat, relAttr, rPrInlineTags } from './helpers.js';
|
|
7
|
+
import { isSafeHref } from '../../util/safeValue.js';
|
|
8
|
+
import { FIELD_ATTR, HF_FIELD_KINDS } from '../../editor/engine/schema.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 页眉/页脚部件 → 受限 HTML(b/i/u/s/sub/sup + 色/字号/字体 span + 域占位 span + 链接)
|
|
12
|
+
* [设计] 只读展示与就地编辑共用这一套受控标签:文本一律转义
|
|
13
|
+
*/
|
|
14
|
+
export const hfHtmlOf = (rd, tree) => {
|
|
15
|
+
const parts = [];
|
|
16
|
+
const collect = (n) => {
|
|
17
|
+
for (const k of n.kids) {
|
|
18
|
+
if (k.name === 'p') parts.push(k);
|
|
19
|
+
else if (k.name === 'sdt') {
|
|
20
|
+
const c = k.child('sdtContent');
|
|
21
|
+
if (c) collect(c);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
collect(tree);
|
|
26
|
+
return parts
|
|
27
|
+
.map((p) => hfParaHtml(rd, p))
|
|
28
|
+
.filter((s) => s !== '')
|
|
29
|
+
.join('<br>');
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** 文本框里的段落 → 行内 HTML(wps:txbx 的 w:txbxContent / VML 的 v:textbox;页码与页脚说明常排在这里) */
|
|
33
|
+
const textBoxHtml = (rd, n) => {
|
|
34
|
+
const out = [];
|
|
35
|
+
const walk = (x) => {
|
|
36
|
+
for (const k of x.kids) {
|
|
37
|
+
if (k.name === 'txbxContent') {
|
|
38
|
+
for (const p of k.kids) {
|
|
39
|
+
if (p.name !== 'p') continue;
|
|
40
|
+
const html = hfParaHtml(rd, p);
|
|
41
|
+
if (html !== '') out.push(html);
|
|
42
|
+
}
|
|
43
|
+
} else walk(k);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
walk(n);
|
|
47
|
+
return out.join('<br>');
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** 页眉页脚一个段落 → 受限 HTML(run 格式 + 域占位;未建模的图形/符号静默跳过,对象丢失另由 losses 盘点) */
|
|
51
|
+
const hfParaHtml = (rd, p) => {
|
|
52
|
+
let html = '';
|
|
53
|
+
// 域状态:''=无 | 'target'(占位已出,缓存结果跳过)| 'keep'(保留缓存结果文本)
|
|
54
|
+
let field = '';
|
|
55
|
+
const runHtml = (r) => {
|
|
56
|
+
const rPr = r.child('rPr');
|
|
57
|
+
let open = '';
|
|
58
|
+
let close = '';
|
|
59
|
+
const style = [];
|
|
60
|
+
if (rPr) {
|
|
61
|
+
// 行内标签(b/i/u/s/上下标;Word 保真判据见 helpers.rPrInlineTags)
|
|
62
|
+
for (const t of rPrInlineTags(rPr)) {
|
|
63
|
+
open += `<${t}>`;
|
|
64
|
+
close = `</${t}>${close}`;
|
|
65
|
+
}
|
|
66
|
+
const c = rPr.child('color');
|
|
67
|
+
if (c && isHexColor(c.attr('val'))) style.push(`color:#${c.attr('val').toLowerCase()}`);
|
|
68
|
+
const sz = rPr.child('sz');
|
|
69
|
+
if (sz) {
|
|
70
|
+
const hp = attrFloat(sz, 'val');
|
|
71
|
+
// 与正文同口径:半点折 px 留两位小数,截断会让页眉页脚的字宽偏移
|
|
72
|
+
if (hp > 0) style.push(`font-size:${round2(halfPointsToPx(hp))}px`);
|
|
73
|
+
}
|
|
74
|
+
const f = rPr.child('rFonts');
|
|
75
|
+
if (f) {
|
|
76
|
+
// 与正文同一份字体链口径(ascii + eastAsia 分工),禁各写一份
|
|
77
|
+
const fam = fontFamilyOf(f.attr('ascii') || f.attr('hAnsi'), f.attr('eastAsia'));
|
|
78
|
+
if (fam !== '') style.push(`font-family:${escapeHtml(fam)}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
let body = '';
|
|
82
|
+
// mc:AlternateContent 是「新老写作器二选一」的壳:只认 Choice 分支,否则页码文本框整块取不到
|
|
83
|
+
const kids = [];
|
|
84
|
+
for (const k of r.kids) {
|
|
85
|
+
const alt = k.name === 'AlternateContent' ? k.child('Choice') || k.child('Fallback') : null;
|
|
86
|
+
if (alt) kids.push(...alt.kids);
|
|
87
|
+
else kids.push(k);
|
|
88
|
+
}
|
|
89
|
+
for (const k of kids) {
|
|
90
|
+
switch (k.name) {
|
|
91
|
+
case 'fldChar':
|
|
92
|
+
if (k.attr('fldCharType') === 'end') field = '';
|
|
93
|
+
break;
|
|
94
|
+
case 'instrText': {
|
|
95
|
+
const code = k.text.trim().toUpperCase().split(/\s+/)[0] || '';
|
|
96
|
+
if (HF_FIELD_KINDS.has(code)) {
|
|
97
|
+
body += `<span ${FIELD_ATTR}="${code}"></span>`;
|
|
98
|
+
field = 'target';
|
|
99
|
+
} else {
|
|
100
|
+
field = 'keep';
|
|
101
|
+
}
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
case 't':
|
|
105
|
+
// 目标域的缓存结果不进 HTML(显示按页现算,写出时重建域)
|
|
106
|
+
if (field !== 'target') body += escapeHtml(k.text);
|
|
107
|
+
break;
|
|
108
|
+
case 'tab':
|
|
109
|
+
body += ' ';
|
|
110
|
+
break;
|
|
111
|
+
case 'br':
|
|
112
|
+
case 'cr':
|
|
113
|
+
body += '<br>';
|
|
114
|
+
break;
|
|
115
|
+
// [坑] 公文页码常常排在一个文本框里(WPS 存成 wps:txbx / VML v:textbox),
|
|
116
|
+
// 只认段落的纯文字会把「- 1 -」整条丢掉,页面右下角只剩一个裸数字
|
|
117
|
+
case 'drawing':
|
|
118
|
+
case 'pict':
|
|
119
|
+
body += textBoxHtml(rd, k);
|
|
120
|
+
break;
|
|
121
|
+
default:
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (body === '') return '';
|
|
125
|
+
if (style.length) {
|
|
126
|
+
open += `<span style="${style.join(';')};">`;
|
|
127
|
+
close = `</span>${close}`;
|
|
128
|
+
}
|
|
129
|
+
return open + body + close;
|
|
130
|
+
};
|
|
131
|
+
const walk = (n) => {
|
|
132
|
+
for (const k of n.kids) {
|
|
133
|
+
if (k.name === 'r') html += runHtml(k);
|
|
134
|
+
else if (k.name === 'hyperlink') {
|
|
135
|
+
const href = rd.docRel.get(relAttr(k, 'id'))?.target || '';
|
|
136
|
+
let inner = '';
|
|
137
|
+
for (const r of k.children('r')) inner += runHtml(r);
|
|
138
|
+
html += inner === '' ? '' : isSafeHref(href) ? `<a href="${escapeHtml(href)}">${inner}</a>` : inner;
|
|
139
|
+
} else if (k.name === 'ins' || k.name === 'del') walk(k);
|
|
140
|
+
else if (k.name === 'sdt') {
|
|
141
|
+
const c = k.child('sdtContent');
|
|
142
|
+
if (c) walk(c);
|
|
143
|
+
} else if (k.name === 'fldSimple') {
|
|
144
|
+
const code = (k.attr('instr') || '').trim().toUpperCase().split(/\s+/)[0] || '';
|
|
145
|
+
if (HF_FIELD_KINDS.has(code)) html += `<span ${FIELD_ATTR}="${code}"></span>`;
|
|
146
|
+
else for (const r of k.children('r')) html += runHtml(r);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
walk(p);
|
|
151
|
+
return html;
|
|
152
|
+
};
|