@a200888213/gbdocx 0.5.2 → 0.6.1
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/README.md +215 -207
- package/index.html +3 -2
- package/package.json +48 -48
- package/serve.mjs +3 -2
- 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 +30 -1
- package/src/app/word/hf.js +76 -76
- package/src/app/word/layout.js +155 -155
- package/src/app/word/notes.js +110 -110
- package/src/app/word/shared.js +22 -22
- package/src/app/word/view.js +9 -2
- package/src/app/word-app.js +487 -469
- package/src/app/word-sheets.js +101 -101
- 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/pageGeometry.js +123 -123
- package/src/kernel/docx/pagination.js +481 -481
- package/src/kernel/docx/watermark.js +45 -45
- 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/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/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/utils.js +48 -48
- package/src/kernel/keyboard.js +114 -114
- package/src/kernel/textStyles.js +84 -84
- package/src/kernel/util/imageFile.js +91 -91
- package/src/kernel/util/shortcutKey.js +33 -33
- package/src/surface/editor-surface.js +600 -600
- package/src/ui/color-panel.js +69 -69
- package/src/ui/context-menu.js +133 -133
- package/src/ui/find-bar.js +151 -151
- package/src/ui/icons.js +137 -135
- package/src/ui/menubar.js +28 -28
- package/src/ui/panels.js +115 -115
- 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/editor-content.css +440 -440
- package/styles/tokens.css +154 -154
|
@@ -1,417 +1,417 @@
|
|
|
1
|
-
// [文件] web·editor-kernel·engine·schema —— 正文规范化与序列化(HTML ↔ DOM 的唯一出入口)
|
|
2
|
-
// [约束] 内核以 DOM 为文档模型,因此「装载时规范化、序列化时剥运行期装饰」是产物干净的唯一保障:
|
|
3
|
-
// · 规范化把外来 / 历史形态收敛到内核口径(li 与单元格内不再套 p、待办条目补齐复选框壳、
|
|
4
|
-
// 游离文本包成段落、空块补 br)。
|
|
5
|
-
// · 序列化只剥运行期(选中类名、框选底色、高亮壳),正文结构原样落盘。
|
|
6
|
-
// [设计] 空段落序列化成 `<p><br></p>` 而不是 `<p></p>`:零宽的空 p 在只读态没有行高,
|
|
7
|
-
// 正文里的空行会整体消失;br 让只读态与编辑态行位一致(存量 `<p></p>` 装载时补 br)。
|
|
8
|
-
|
|
9
|
-
import { SAFE_IMG, isSafeHref, sanitizeFontName } from '../../util/safeValue.js';
|
|
10
|
-
import { RUNTIME_CLASS_SELECTOR, hasBlockBody, isCell, isColumnBlock, isContainerBlock, isTaskItem, moveChildren, moveChildrenBefore, renameBlock, restoreRuntimeStructures, stripRuntimeClasses, unwrapElement } from './dom.js';
|
|
11
|
-
|
|
12
|
-
/** 结构性垃圾标签:装载时整段丢弃(含可执行/可嵌入内容;正文不支持 SVG/公式渲染) */
|
|
13
|
-
const DROP_TAGS = new Set(['script', 'style', 'link', 'meta', 'title', 'head', 'iframe', 'object', 'embed', 'form', 'button', 'select', 'textarea', 'svg', 'math']);
|
|
14
|
-
|
|
15
|
-
/** docx 方言的标记类名(与 .docx 编解码的 HTML 侧同一份拼写,内核与编解码共用此清单,禁各自写字面量):
|
|
16
|
-
* INS/DEL=修订标记、COMMENT=批注锚点、PAGEBREAK=分页符块、TITLE=标题样式段落、FOOTNOTE/ENDNOTE=脚注尾注锚点、
|
|
17
|
-
* SPAN_ALL=多栏文档里的跨栏块、SECTIONBREAK=分节边界块、TOC=目录块(TOC 域缓存行)、
|
|
18
|
-
* KEEP_NEXT/KEEP_LINES/WIDOW_OFF/PBB=段落分页控制(与下段同页/整段不拆/关闭孤行控制/段前分页)、
|
|
19
|
-
* CH_SCALE/CH_SCALE_BOX=字符缩放(段内压缩壳与其外层)、HLINE=线条图形的解析载体(渲染期被折成段落边框,不入 DOM)
|
|
20
|
-
* ATTACH/REF=笔记行内锚(附件引用/块引用)、EMBED=笔记嵌入块(引用内容的只读渲染;笔记域专用,.gbRtx 侧不产生) */
|
|
21
|
-
export const DOC_CLASS = {
|
|
22
|
-
INS: 'doc-ins',
|
|
23
|
-
DEL: 'doc-del',
|
|
24
|
-
COMMENT: 'doc-comment',
|
|
25
|
-
PAGEBREAK: 'doc-pagebreak',
|
|
26
|
-
TITLE: 'doc-title',
|
|
27
|
-
FOOTNOTE: 'doc-footnote',
|
|
28
|
-
ENDNOTE: 'doc-endnote',
|
|
29
|
-
SPAN_ALL: 'col-span-all',
|
|
30
|
-
SECTIONBREAK: 'doc-sectionbreak',
|
|
31
|
-
TOC: 'doc-toc',
|
|
32
|
-
KEEP_NEXT: 'doc-keep-next',
|
|
33
|
-
KEEP_LINES: 'doc-keep-lines',
|
|
34
|
-
WIDOW_OFF: 'doc-widow-off',
|
|
35
|
-
PBB: 'doc-pbb',
|
|
36
|
-
CH_SCALE: 'doc-ch-scale',
|
|
37
|
-
CH_SCALE_BOX: 'doc-ch-scale-box',
|
|
38
|
-
HLINE: 'doc-hline',
|
|
39
|
-
PUNCT: 'doc-punct',
|
|
40
|
-
PUNCT_OPEN: 'doc-punct-open',
|
|
41
|
-
EA: 'doc-ea',
|
|
42
|
-
ATTACH: 'doc-attach',
|
|
43
|
-
REF: 'doc-ref',
|
|
44
|
-
EMBED: 'doc-embed'
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
/** 歧义标点的字体提示(w:rFonts/@w:hint="eastAsia" 的 HTML 侧载体;只影响引号/省略号/破折号这类中西文歧义字) */
|
|
48
|
-
export const HINT_ATTR = 'data-docx-hint';
|
|
49
|
-
|
|
50
|
-
/** 批注锚点的键属性(span.doc-comment[data-cmt=键]) */
|
|
51
|
-
export const COMMENT_ATTR = 'data-cmt';
|
|
52
|
-
|
|
53
|
-
/** 脚注/尾注锚点的编号属性(sup.doc-footnote[data-fn=id];编号文字只是显示近似,写出端按引用关系重编) */
|
|
54
|
-
export const NOTE_ATTR = 'data-fn';
|
|
55
|
-
|
|
56
|
-
/** 笔记行内锚属性(全小写;属性名在 HTML 文档里会被压平,识别一律大小写无关)。
|
|
57
|
-
* [约束] 值与 web core/utils/block.js 的协议正则、后端 note_content_scan.go 同值,禁只改一处;
|
|
58
|
-
* 内核最底层的 dom.js 因不能 import 本文件,另持一份同名常量(同值注释见那边)。 */
|
|
59
|
-
export const ATTACH_ATTR = 'data-attach';
|
|
60
|
-
export const REF_DOC_ATTR = 'data-ref-doc';
|
|
61
|
-
export const REF_BLOCK_ATTR = 'data-ref-block';
|
|
62
|
-
export const REF_SUB_ATTR = 'data-ref-sub';
|
|
63
|
-
|
|
64
|
-
/** 页眉页脚里的域占位属性(PAGE/NUMPAGES/SECTIONPAGES:读入落成占位 span,显示按页求值,写出还原为域) */
|
|
65
|
-
export const FIELD_ATTR = 'data-fld';
|
|
66
|
-
|
|
67
|
-
/** 页眉页脚里按页求值的域码(读端/写端/就地编辑清洗三方共用这一份词表) */
|
|
68
|
-
export const HF_FIELD_KINDS = new Set(['PAGE', 'NUMPAGES', 'SECTIONPAGES']);
|
|
69
|
-
|
|
70
|
-
/** 主题色引用属性(显式色 + 主题引用二选一:引用来自文件时保留原样,禁摊平成 hex 丢失联动) */
|
|
71
|
-
export const THEME_COLOR_ATTR = 'data-color-theme';
|
|
72
|
-
export const THEME_TINT_ATTR = 'data-color-theme-tint';
|
|
73
|
-
export const THEME_SHADE_ATTR = 'data-color-theme-shade';
|
|
74
|
-
|
|
75
|
-
/** 运行期属性:绝不落盘 */
|
|
76
|
-
export const DROP_ATTRS = ['data-re-runtime', 'spellcheck', 'autocorrect'];
|
|
77
|
-
|
|
78
|
-
/** `<font size=1..7>` 的浏览器关键字口径(3 号 = 正文字号):换算成像素会改掉存量文档的字号 */
|
|
79
|
-
const FONT_SIZE_KEYWORDS = ['x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'xxx-large'];
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* 规范化整篇正文
|
|
83
|
-
* @param {Element|DocumentFragment} container 编辑面根或待插入片段
|
|
84
|
-
* @param {{editable?:boolean}} options editable=编辑面(空文档补一个空段落)
|
|
85
|
-
*/
|
|
86
|
-
export function normalizeBody(container, { editable = false } = {}) {
|
|
87
|
-
// 运行期布局结构(分页产物)不许随装载混进文档:防御性还原一次
|
|
88
|
-
restoreRuntimeStructures(container);
|
|
89
|
-
stripJunk(container);
|
|
90
|
-
unwrapPlainSpans(container);
|
|
91
|
-
renameStrayDivs(container);
|
|
92
|
-
mergeStrayText(container);
|
|
93
|
-
flattenBlocksInLiAndCells(container);
|
|
94
|
-
ensureTaskItems(container);
|
|
95
|
-
ensureCodeBlocks(container);
|
|
96
|
-
ensurePageBreaks(container);
|
|
97
|
-
ensureSectionBreaks(container);
|
|
98
|
-
ensureTocs(container);
|
|
99
|
-
ensureAnchors(container);
|
|
100
|
-
ensureNoteAnchors(container);
|
|
101
|
-
ensureEmbeds(container);
|
|
102
|
-
fillEmptyBlocks(container);
|
|
103
|
-
if (editable && !container.querySelector('p,h1,h2,h3,h4,h5,h6,ul,ol,blockquote,pre,table,hr,img')) {
|
|
104
|
-
const p = document.createElement('p');
|
|
105
|
-
p.appendChild(document.createElement('br'));
|
|
106
|
-
container.appendChild(p);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* 剥掉无任何属性的 span 空壳(存量正文里常见十几层连套:早期编辑器与网页粘贴的产物)
|
|
112
|
-
* [坑] 它们不影响渲染,却把正文体积与行内容器层级成倍撑大——行内偏移换算、选区重建都要逐层走,
|
|
113
|
-
* 层级越深越容易出错;只认「没有属性」的壳,带 style/class 的是有语义的标记壳,一律保留
|
|
114
|
-
*/
|
|
115
|
-
function unwrapPlainSpans(container) {
|
|
116
|
-
for (let round = 0; round < 12; round += 1) {
|
|
117
|
-
const plain = Array.from(container.querySelectorAll('span')).filter((el) => !el.hasAttributes());
|
|
118
|
-
if (!plain.length) return;
|
|
119
|
-
for (const el of plain) {
|
|
120
|
-
unwrapElement(el);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/** 丢弃垃圾标签与注释,剥掉运行期类名与属性 */
|
|
126
|
-
function stripJunk(container) {
|
|
127
|
-
const walker = document.createTreeWalker(container, NodeFilter.SHOW_COMMENT | NodeFilter.SHOW_ELEMENT);
|
|
128
|
-
const doomed = [];
|
|
129
|
-
let node = walker.currentNode;
|
|
130
|
-
if (node.nodeType === Node.COMMENT_NODE) doomed.push(node);
|
|
131
|
-
while (walker.nextNode()) {
|
|
132
|
-
const el = walker.currentNode;
|
|
133
|
-
if (el.nodeType === Node.COMMENT_NODE) doomed.push(el);
|
|
134
|
-
else if (DROP_TAGS.has(el.tagName.toLowerCase())) doomed.push(el);
|
|
135
|
-
}
|
|
136
|
-
doomed.forEach((el) => el.remove());
|
|
137
|
-
|
|
138
|
-
for (const el of Array.from(container.querySelectorAll('*'))) {
|
|
139
|
-
if (el.nodeType !== Node.ELEMENT_NODE) continue;
|
|
140
|
-
for (const attr of DROP_ATTRS) el.removeAttribute(attr);
|
|
141
|
-
// 事件属性是脚本入口:粘贴/装载的内容一律不允许携带
|
|
142
|
-
for (const attr of Array.from(el.attributes)) {
|
|
143
|
-
if (attr.name.toLowerCase().startsWith('on')) el.removeAttribute(attr.name);
|
|
144
|
-
}
|
|
145
|
-
stripRuntimeClasses(el);
|
|
146
|
-
// 只有复选框(待办条目)保留 contenteditable=false,其余一律不带着走
|
|
147
|
-
if (el.getAttribute('contenteditable') === 'false' && el.tagName !== 'INPUT') el.removeAttribute('contenteditable');
|
|
148
|
-
sanitizeUrls(el);
|
|
149
|
-
convertFontTag(el);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/** a / img 的协议白名单过滤(白名单唯一源见 core/utils/safeValue.js) */
|
|
154
|
-
function sanitizeUrls(el) {
|
|
155
|
-
if (el.tagName === 'A') {
|
|
156
|
-
const href = el.getAttribute('href') || '';
|
|
157
|
-
if (!isSafeHref(href)) el.removeAttribute('href');
|
|
158
|
-
else {
|
|
159
|
-
el.setAttribute('target', '_blank');
|
|
160
|
-
el.setAttribute('rel', 'noopener noreferrer nofollow');
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
if (el.tagName === 'IMG') {
|
|
164
|
-
const src = (el.getAttribute('src') || '').trim();
|
|
165
|
-
// [坑] 没有 src 的图片不是协议违规,而是宿主的资产占位形态(.gbRtx 正文里的 <img data-gbRtx-asset>):
|
|
166
|
-
// 以前一律按坏图删掉,等于每次打开带容器的正文都把图片抹光
|
|
167
|
-
if (!src) return;
|
|
168
|
-
if (!SAFE_IMG.test(src)) el.remove();
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/** 历史 `<font color size face>` → span 内联样式(只认这三条,其余属性丢弃) */
|
|
173
|
-
function convertFontTag(el) {
|
|
174
|
-
if (el.tagName !== 'FONT') return;
|
|
175
|
-
const style = [];
|
|
176
|
-
if (el.getAttribute('color')) style.push(`color: ${el.getAttribute('color')}`);
|
|
177
|
-
const face = sanitizeFontName(el.getAttribute('face'));
|
|
178
|
-
if (face) style.push(`font-family: ${face}`);
|
|
179
|
-
const size = Number(el.getAttribute('size'));
|
|
180
|
-
// [口径] 按浏览器对 <font size=1..7> 的关键字映射换算(3 号即默认字号):
|
|
181
|
-
// 写死像素会把存量文档的字号改掉,关键字才跟着正文字号走
|
|
182
|
-
if (size) style.push(`font-size: ${FONT_SIZE_KEYWORDS[Math.min(Math.max(size, 1), 7) - 1]}`);
|
|
183
|
-
const span = document.createElement('span');
|
|
184
|
-
if (style.length) span.setAttribute('style', style.join('; '));
|
|
185
|
-
moveChildren(el, span);
|
|
186
|
-
el.replaceWith(span);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* 容器(编辑面根 / 引用)里直接挂着的行内内容包成段落
|
|
191
|
-
* [坑] 只对这两类容器动手:li 与单元格本来就是文本块(正文直接挂),表格结构(tbody/tr)更不是「游离文本」——
|
|
192
|
-
* 早前一版把它们一起包了 p,存量表格被装成 <table><p><tbody>… 整表正文再也枚举不到
|
|
193
|
-
*/
|
|
194
|
-
function mergeStrayText(container) {
|
|
195
|
-
const hosts = [container, ...Array.from(container.querySelectorAll('blockquote, [data-callout], [data-super], [data-col]'))];
|
|
196
|
-
for (const host of hosts) {
|
|
197
|
-
let run = null;
|
|
198
|
-
for (const child of Array.from(host.childNodes)) {
|
|
199
|
-
if (child.nodeType === Node.TEXT_NODE && !child.data.trim()) {
|
|
200
|
-
child.remove();
|
|
201
|
-
continue;
|
|
202
|
-
}
|
|
203
|
-
const needBlock = child.nodeType === Node.TEXT_NODE || (child.nodeType === Node.ELEMENT_NODE && !isBlockish(child));
|
|
204
|
-
if (!needBlock) {
|
|
205
|
-
run = null;
|
|
206
|
-
continue;
|
|
207
|
-
}
|
|
208
|
-
if (!run) run = host.insertBefore(document.createElement('p'), child);
|
|
209
|
-
run.appendChild(child);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/** 块级形态:可以独立挂在容器下,不需要再包一层 p */
|
|
215
|
-
function isBlockish(el) {
|
|
216
|
-
return /^(P|H[1-6]|UL|OL|BLOCKQUOTE|PRE|TABLE|HR|DIV|LI|TBODY|THEAD|TFOOT|TR|TD|TH|COLGROUP|CAPTION|IMG)$/.test(el.tagName);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/** 浏览器遗留的 div 段落 → p(待办条目的正文 div、分页符/节边界块除外) */
|
|
220
|
-
function renameStrayDivs(container) {
|
|
221
|
-
for (const el of Array.from(container.querySelectorAll('div'))) {
|
|
222
|
-
if (isTaskItem(el.parentElement)) continue;
|
|
223
|
-
if (isCell(el.parentElement)) continue;
|
|
224
|
-
if (el.classList.contains(DOC_CLASS.PAGEBREAK)) continue;
|
|
225
|
-
if (el.classList.contains(DOC_CLASS.SECTIONBREAK)) continue;
|
|
226
|
-
if (el.classList.contains(DOC_CLASS.TOC)) continue;
|
|
227
|
-
if (el.hasAttribute(REF_DOC_ATTR)) continue; // 笔记嵌入块(ensureEmbeds 在其后,属性判断不依赖 class)
|
|
228
|
-
if (isContainerBlock(el) || isColumnBlock(el)) continue; // 高亮块/超级块/列外壳:容器不转段落
|
|
229
|
-
el.replaceWith(renameBlock(el, 'p'));
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/** li 与单元格内的段落压平(多段之间补 br,保住视觉换行) */
|
|
234
|
-
function flattenBlocksInLiAndCells(container) {
|
|
235
|
-
const hosts = Array.from(container.querySelectorAll('li,td,th'));
|
|
236
|
-
for (const host of hosts) {
|
|
237
|
-
const blocks = Array.from(host.children).filter((el) => /^(P|H[1-6])$/.test(el.tagName));
|
|
238
|
-
if (!blocks.length) continue;
|
|
239
|
-
const frag = document.createDocumentFragment();
|
|
240
|
-
blocks.forEach((block, index) => {
|
|
241
|
-
if (index) frag.appendChild(document.createElement('br'));
|
|
242
|
-
moveChildren(block, frag);
|
|
243
|
-
});
|
|
244
|
-
blocks[0].before(frag);
|
|
245
|
-
blocks.forEach((block) => block.remove());
|
|
246
|
-
if (isTaskItem(host.parentElement)) host.parentElement.replaceWith(host);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/** 待办条目收敛到唯一形态:li[data-checked] > label[contenteditable=false] > input + div 正文(存量缺壳、粘贴来的裸 li 都归到这里) */
|
|
251
|
-
function ensureTaskItems(container) {
|
|
252
|
-
for (const list of Array.from(container.querySelectorAll('ul[data-type="taskList"], ol[data-type="taskList"]'))) {
|
|
253
|
-
for (const child of Array.from(list.children)) {
|
|
254
|
-
const li = child.tagName === 'LI' ? child : null;
|
|
255
|
-
if (!li) {
|
|
256
|
-
const wrapped = document.createElement('li');
|
|
257
|
-
moveChildren(child, wrapped);
|
|
258
|
-
child.replaceWith(wrapped);
|
|
259
|
-
wrapped.setAttribute('data-checked', 'false');
|
|
260
|
-
continue;
|
|
261
|
-
}
|
|
262
|
-
if (!li.hasAttribute('data-checked')) li.setAttribute('data-checked', 'false');
|
|
263
|
-
let label = li.querySelector(':scope > label');
|
|
264
|
-
if (!label) {
|
|
265
|
-
label = document.createElement('label');
|
|
266
|
-
const input = document.createElement('input');
|
|
267
|
-
input.type = 'checkbox';
|
|
268
|
-
label.appendChild(input);
|
|
269
|
-
li.prepend(label);
|
|
270
|
-
}
|
|
271
|
-
label.setAttribute('contenteditable', 'false');
|
|
272
|
-
const input = label.querySelector('input');
|
|
273
|
-
if (input) {
|
|
274
|
-
input.type = 'checkbox';
|
|
275
|
-
input.checked = li.getAttribute('data-checked') === 'true';
|
|
276
|
-
}
|
|
277
|
-
let div = Array.from(li.children).find((el) => el.tagName === 'DIV');
|
|
278
|
-
if (!div) {
|
|
279
|
-
div = document.createElement('div');
|
|
280
|
-
for (const node of Array.from(li.childNodes)) {
|
|
281
|
-
if (node === label) continue;
|
|
282
|
-
div.appendChild(node);
|
|
283
|
-
}
|
|
284
|
-
li.appendChild(div);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
// 非待办列表里混进的待办形态:剥掉复选框壳,正文回到 li
|
|
289
|
-
for (const li of Array.from(container.querySelectorAll('li[data-checked]'))) {
|
|
290
|
-
if (li.parentElement?.getAttribute('data-type') === 'taskList') continue;
|
|
291
|
-
li.removeAttribute('data-checked');
|
|
292
|
-
const label = li.querySelector(':scope > label');
|
|
293
|
-
const div = li.querySelector(':scope > div');
|
|
294
|
-
label?.remove();
|
|
295
|
-
if (div) moveChildrenBefore(div);
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
/** 代码块必须有 code 子元素,且内部只有纯文本 */
|
|
300
|
-
function ensureCodeBlocks(container) {
|
|
301
|
-
for (const pre of Array.from(container.querySelectorAll('pre'))) {
|
|
302
|
-
const code = pre.querySelector('code');
|
|
303
|
-
const text = (code || pre).textContent || '';
|
|
304
|
-
pre.replaceChildren();
|
|
305
|
-
const next = document.createElement('code');
|
|
306
|
-
next.textContent = text;
|
|
307
|
-
pre.appendChild(next);
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/** 空文本块补 br:光标有处可落,只读态也保有行高 */
|
|
312
|
-
function fillEmptyBlocks(container) {
|
|
313
|
-
for (const block of Array.from(container.querySelectorAll('p,h1,h2,h3,h4,h5,h6,li,td,th,div'))) {
|
|
314
|
-
if (isTaskItem(block)) continue;
|
|
315
|
-
if (block.classList.contains(DOC_CLASS.PAGEBREAK)) continue;
|
|
316
|
-
if (block.classList.contains(DOC_CLASS.SECTIONBREAK)) continue;
|
|
317
|
-
if (block.classList.contains(DOC_CLASS.TOC)) continue;
|
|
318
|
-
if (block.hasAttribute(REF_DOC_ATTR)) continue; // 笔记嵌入块(空体是常态,渲染产物由宿主 hydrate 补)
|
|
319
|
-
if (isContainerBlock(block) || isColumnBlock(block)) continue; // 容器空否看子块,自身不补占位 br
|
|
320
|
-
if (hasBlockBody(block)) continue;
|
|
321
|
-
block.replaceChildren(document.createElement('br'));
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
/** 分页符块收敛到唯一形态:div.doc-pagebreak〔空体 + contenteditable=false〕
|
|
326
|
-
* [坑] 不能让它停在 p/其它标签上:那些是文本块,光标会落进空盒、退格还能把标记当段落删一半;
|
|
327
|
-
* 落盘一律不带 contenteditable(serializeBody 会剥),靠这里每次装载补回 */
|
|
328
|
-
function ensurePageBreaks(container) {
|
|
329
|
-
for (const el of Array.from(container.querySelectorAll(`.${DOC_CLASS.PAGEBREAK}`))) {
|
|
330
|
-
const target = el.tagName === 'DIV' ? el : renameBlock(el, 'div');
|
|
331
|
-
target.replaceChildren();
|
|
332
|
-
target.setAttribute('contenteditable', 'false');
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
/** 节边界块收敛到唯一形态(与分页符同款治理):div.doc-sectionbreak〔空体 + contenteditable=false〕;
|
|
337
|
-
* 它是正文流的编解码标记,不是文本块——光标不落、退格不删,落盘靠序列化剥 contenteditable 后装载补回 */
|
|
338
|
-
function ensureSectionBreaks(container) {
|
|
339
|
-
for (const el of Array.from(container.querySelectorAll(`.${DOC_CLASS.SECTIONBREAK}`))) {
|
|
340
|
-
const target = el.tagName === 'DIV' ? el : renameBlock(el, 'div');
|
|
341
|
-
target.replaceChildren();
|
|
342
|
-
target.setAttribute('contenteditable', 'false');
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
/** 目录块(TOC 域缓存行)收敛:div.doc-toc〔只读区块,内部行内容原样保留〕 */
|
|
347
|
-
function ensureTocs(container) {
|
|
348
|
-
for (const el of Array.from(container.querySelectorAll(`.${DOC_CLASS.TOC}`))) {
|
|
349
|
-
const target = el.tagName === 'DIV' ? el : renameBlock(el, 'div');
|
|
350
|
-
target.setAttribute('contenteditable', 'false');
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
/** 脚注/尾注锚点收敛:整枚原子(contenteditable=false),序号文字不可被光标进入改写。
|
|
355
|
-
* 缺 class 的 sup[data-fn](粘贴/存量)按脚注补齐。 */
|
|
356
|
-
function ensureNoteAnchors(container) {
|
|
357
|
-
for (const el of Array.from(container.querySelectorAll('sup'))) {
|
|
358
|
-
if (!el.hasAttribute(NOTE_ATTR)) continue;
|
|
359
|
-
if (!el.classList.contains(DOC_CLASS.FOOTNOTE) && !el.classList.contains(DOC_CLASS.ENDNOTE)) {
|
|
360
|
-
el.classList.add(DOC_CLASS.FOOTNOTE);
|
|
361
|
-
}
|
|
362
|
-
el.setAttribute('contenteditable', 'false');
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
/** 笔记行内锚收敛:补 class(粘贴链会剥)与 contenteditable=false(落盘一律剥、装载补回,照分页符口径) */
|
|
367
|
-
function ensureAnchors(container) {
|
|
368
|
-
for (const el of Array.from(container.querySelectorAll('span'))) {
|
|
369
|
-
if (el.hasAttribute(ATTACH_ATTR)) {
|
|
370
|
-
el.classList.add(DOC_CLASS.ATTACH);
|
|
371
|
-
el.setAttribute('contenteditable', 'false');
|
|
372
|
-
} else if (el.hasAttribute(REF_DOC_ATTR)) {
|
|
373
|
-
el.classList.add(DOC_CLASS.REF);
|
|
374
|
-
el.setAttribute('contenteditable', 'false');
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
/** 笔记嵌入块收敛:div.doc-embed〔只读区块,渲染产物原样保留;落盘剥、装载补,照 TOC 口径〕 */
|
|
380
|
-
function ensureEmbeds(container) {
|
|
381
|
-
for (const el of Array.from(container.querySelectorAll('[data-ref-doc]'))) {
|
|
382
|
-
if (el.tagName !== 'DIV') continue;
|
|
383
|
-
el.classList.add(DOC_CLASS.EMBED);
|
|
384
|
-
el.setAttribute('contenteditable', 'false');
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
/**
|
|
389
|
-
* 序列化编辑面为落盘 HTML:只剥运行期装饰,正文结构原样保留
|
|
390
|
-
* [坑] 复选框的勾选状态是 property,不进 innerHTML,必须同步成属性;
|
|
391
|
-
* contenteditable 是编辑面运行期属性,落盘一律剥掉(装载时 ensureTaskItems 会补回来)
|
|
392
|
-
* @param {Element} root 编辑面根元素
|
|
393
|
-
*/
|
|
394
|
-
export function serializeBody(root) {
|
|
395
|
-
const clone = root.cloneNode(true);
|
|
396
|
-
// 运行期布局结构(Word 分页占位/栏容器/拆分块):整块还原成单流正文,再剥装饰类
|
|
397
|
-
restoreRuntimeStructures(clone);
|
|
398
|
-
clone.querySelectorAll(RUNTIME_CLASS_SELECTOR).forEach((el) => stripRuntimeClasses(el));
|
|
399
|
-
clone.querySelectorAll('input[type="checkbox"]').forEach((input) => {
|
|
400
|
-
if (input.checked) input.setAttribute('checked', '');
|
|
401
|
-
else input.removeAttribute('checked');
|
|
402
|
-
});
|
|
403
|
-
clone.querySelectorAll('[contenteditable]').forEach((el) => el.removeAttribute('contenteditable'));
|
|
404
|
-
return clone.innerHTML;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
/**
|
|
408
|
-
* 解析外部 HTML 为可插入片段(粘贴用):清洗已由 pasteClean 做完,这里只做形态规范化
|
|
409
|
-
* @param {string} html
|
|
410
|
-
* @returns {DocumentFragment}
|
|
411
|
-
*/
|
|
412
|
-
export function parseFragment(html) {
|
|
413
|
-
const tpl = document.createElement('template');
|
|
414
|
-
tpl.innerHTML = String(html || '');
|
|
415
|
-
normalizeBody(tpl.content);
|
|
416
|
-
return tpl.content;
|
|
417
|
-
}
|
|
1
|
+
// [文件] web·editor-kernel·engine·schema —— 正文规范化与序列化(HTML ↔ DOM 的唯一出入口)
|
|
2
|
+
// [约束] 内核以 DOM 为文档模型,因此「装载时规范化、序列化时剥运行期装饰」是产物干净的唯一保障:
|
|
3
|
+
// · 规范化把外来 / 历史形态收敛到内核口径(li 与单元格内不再套 p、待办条目补齐复选框壳、
|
|
4
|
+
// 游离文本包成段落、空块补 br)。
|
|
5
|
+
// · 序列化只剥运行期(选中类名、框选底色、高亮壳),正文结构原样落盘。
|
|
6
|
+
// [设计] 空段落序列化成 `<p><br></p>` 而不是 `<p></p>`:零宽的空 p 在只读态没有行高,
|
|
7
|
+
// 正文里的空行会整体消失;br 让只读态与编辑态行位一致(存量 `<p></p>` 装载时补 br)。
|
|
8
|
+
|
|
9
|
+
import { SAFE_IMG, isSafeHref, sanitizeFontName } from '../../util/safeValue.js';
|
|
10
|
+
import { RUNTIME_CLASS_SELECTOR, hasBlockBody, isCell, isColumnBlock, isContainerBlock, isTaskItem, moveChildren, moveChildrenBefore, renameBlock, restoreRuntimeStructures, stripRuntimeClasses, unwrapElement } from './dom.js';
|
|
11
|
+
|
|
12
|
+
/** 结构性垃圾标签:装载时整段丢弃(含可执行/可嵌入内容;正文不支持 SVG/公式渲染) */
|
|
13
|
+
const DROP_TAGS = new Set(['script', 'style', 'link', 'meta', 'title', 'head', 'iframe', 'object', 'embed', 'form', 'button', 'select', 'textarea', 'svg', 'math']);
|
|
14
|
+
|
|
15
|
+
/** docx 方言的标记类名(与 .docx 编解码的 HTML 侧同一份拼写,内核与编解码共用此清单,禁各自写字面量):
|
|
16
|
+
* INS/DEL=修订标记、COMMENT=批注锚点、PAGEBREAK=分页符块、TITLE=标题样式段落、FOOTNOTE/ENDNOTE=脚注尾注锚点、
|
|
17
|
+
* SPAN_ALL=多栏文档里的跨栏块、SECTIONBREAK=分节边界块、TOC=目录块(TOC 域缓存行)、
|
|
18
|
+
* KEEP_NEXT/KEEP_LINES/WIDOW_OFF/PBB=段落分页控制(与下段同页/整段不拆/关闭孤行控制/段前分页)、
|
|
19
|
+
* CH_SCALE/CH_SCALE_BOX=字符缩放(段内压缩壳与其外层)、HLINE=线条图形的解析载体(渲染期被折成段落边框,不入 DOM)
|
|
20
|
+
* ATTACH/REF=笔记行内锚(附件引用/块引用)、EMBED=笔记嵌入块(引用内容的只读渲染;笔记域专用,.gbRtx 侧不产生) */
|
|
21
|
+
export const DOC_CLASS = {
|
|
22
|
+
INS: 'doc-ins',
|
|
23
|
+
DEL: 'doc-del',
|
|
24
|
+
COMMENT: 'doc-comment',
|
|
25
|
+
PAGEBREAK: 'doc-pagebreak',
|
|
26
|
+
TITLE: 'doc-title',
|
|
27
|
+
FOOTNOTE: 'doc-footnote',
|
|
28
|
+
ENDNOTE: 'doc-endnote',
|
|
29
|
+
SPAN_ALL: 'col-span-all',
|
|
30
|
+
SECTIONBREAK: 'doc-sectionbreak',
|
|
31
|
+
TOC: 'doc-toc',
|
|
32
|
+
KEEP_NEXT: 'doc-keep-next',
|
|
33
|
+
KEEP_LINES: 'doc-keep-lines',
|
|
34
|
+
WIDOW_OFF: 'doc-widow-off',
|
|
35
|
+
PBB: 'doc-pbb',
|
|
36
|
+
CH_SCALE: 'doc-ch-scale',
|
|
37
|
+
CH_SCALE_BOX: 'doc-ch-scale-box',
|
|
38
|
+
HLINE: 'doc-hline',
|
|
39
|
+
PUNCT: 'doc-punct',
|
|
40
|
+
PUNCT_OPEN: 'doc-punct-open',
|
|
41
|
+
EA: 'doc-ea',
|
|
42
|
+
ATTACH: 'doc-attach',
|
|
43
|
+
REF: 'doc-ref',
|
|
44
|
+
EMBED: 'doc-embed'
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** 歧义标点的字体提示(w:rFonts/@w:hint="eastAsia" 的 HTML 侧载体;只影响引号/省略号/破折号这类中西文歧义字) */
|
|
48
|
+
export const HINT_ATTR = 'data-docx-hint';
|
|
49
|
+
|
|
50
|
+
/** 批注锚点的键属性(span.doc-comment[data-cmt=键]) */
|
|
51
|
+
export const COMMENT_ATTR = 'data-cmt';
|
|
52
|
+
|
|
53
|
+
/** 脚注/尾注锚点的编号属性(sup.doc-footnote[data-fn=id];编号文字只是显示近似,写出端按引用关系重编) */
|
|
54
|
+
export const NOTE_ATTR = 'data-fn';
|
|
55
|
+
|
|
56
|
+
/** 笔记行内锚属性(全小写;属性名在 HTML 文档里会被压平,识别一律大小写无关)。
|
|
57
|
+
* [约束] 值与 web core/utils/block.js 的协议正则、后端 note_content_scan.go 同值,禁只改一处;
|
|
58
|
+
* 内核最底层的 dom.js 因不能 import 本文件,另持一份同名常量(同值注释见那边)。 */
|
|
59
|
+
export const ATTACH_ATTR = 'data-attach';
|
|
60
|
+
export const REF_DOC_ATTR = 'data-ref-doc';
|
|
61
|
+
export const REF_BLOCK_ATTR = 'data-ref-block';
|
|
62
|
+
export const REF_SUB_ATTR = 'data-ref-sub';
|
|
63
|
+
|
|
64
|
+
/** 页眉页脚里的域占位属性(PAGE/NUMPAGES/SECTIONPAGES:读入落成占位 span,显示按页求值,写出还原为域) */
|
|
65
|
+
export const FIELD_ATTR = 'data-fld';
|
|
66
|
+
|
|
67
|
+
/** 页眉页脚里按页求值的域码(读端/写端/就地编辑清洗三方共用这一份词表) */
|
|
68
|
+
export const HF_FIELD_KINDS = new Set(['PAGE', 'NUMPAGES', 'SECTIONPAGES']);
|
|
69
|
+
|
|
70
|
+
/** 主题色引用属性(显式色 + 主题引用二选一:引用来自文件时保留原样,禁摊平成 hex 丢失联动) */
|
|
71
|
+
export const THEME_COLOR_ATTR = 'data-color-theme';
|
|
72
|
+
export const THEME_TINT_ATTR = 'data-color-theme-tint';
|
|
73
|
+
export const THEME_SHADE_ATTR = 'data-color-theme-shade';
|
|
74
|
+
|
|
75
|
+
/** 运行期属性:绝不落盘 */
|
|
76
|
+
export const DROP_ATTRS = ['data-re-runtime', 'spellcheck', 'autocorrect'];
|
|
77
|
+
|
|
78
|
+
/** `<font size=1..7>` 的浏览器关键字口径(3 号 = 正文字号):换算成像素会改掉存量文档的字号 */
|
|
79
|
+
const FONT_SIZE_KEYWORDS = ['x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'xxx-large'];
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 规范化整篇正文
|
|
83
|
+
* @param {Element|DocumentFragment} container 编辑面根或待插入片段
|
|
84
|
+
* @param {{editable?:boolean}} options editable=编辑面(空文档补一个空段落)
|
|
85
|
+
*/
|
|
86
|
+
export function normalizeBody(container, { editable = false } = {}) {
|
|
87
|
+
// 运行期布局结构(分页产物)不许随装载混进文档:防御性还原一次
|
|
88
|
+
restoreRuntimeStructures(container);
|
|
89
|
+
stripJunk(container);
|
|
90
|
+
unwrapPlainSpans(container);
|
|
91
|
+
renameStrayDivs(container);
|
|
92
|
+
mergeStrayText(container);
|
|
93
|
+
flattenBlocksInLiAndCells(container);
|
|
94
|
+
ensureTaskItems(container);
|
|
95
|
+
ensureCodeBlocks(container);
|
|
96
|
+
ensurePageBreaks(container);
|
|
97
|
+
ensureSectionBreaks(container);
|
|
98
|
+
ensureTocs(container);
|
|
99
|
+
ensureAnchors(container);
|
|
100
|
+
ensureNoteAnchors(container);
|
|
101
|
+
ensureEmbeds(container);
|
|
102
|
+
fillEmptyBlocks(container);
|
|
103
|
+
if (editable && !container.querySelector('p,h1,h2,h3,h4,h5,h6,ul,ol,blockquote,pre,table,hr,img')) {
|
|
104
|
+
const p = document.createElement('p');
|
|
105
|
+
p.appendChild(document.createElement('br'));
|
|
106
|
+
container.appendChild(p);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* 剥掉无任何属性的 span 空壳(存量正文里常见十几层连套:早期编辑器与网页粘贴的产物)
|
|
112
|
+
* [坑] 它们不影响渲染,却把正文体积与行内容器层级成倍撑大——行内偏移换算、选区重建都要逐层走,
|
|
113
|
+
* 层级越深越容易出错;只认「没有属性」的壳,带 style/class 的是有语义的标记壳,一律保留
|
|
114
|
+
*/
|
|
115
|
+
function unwrapPlainSpans(container) {
|
|
116
|
+
for (let round = 0; round < 12; round += 1) {
|
|
117
|
+
const plain = Array.from(container.querySelectorAll('span')).filter((el) => !el.hasAttributes());
|
|
118
|
+
if (!plain.length) return;
|
|
119
|
+
for (const el of plain) {
|
|
120
|
+
unwrapElement(el);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** 丢弃垃圾标签与注释,剥掉运行期类名与属性 */
|
|
126
|
+
function stripJunk(container) {
|
|
127
|
+
const walker = document.createTreeWalker(container, NodeFilter.SHOW_COMMENT | NodeFilter.SHOW_ELEMENT);
|
|
128
|
+
const doomed = [];
|
|
129
|
+
let node = walker.currentNode;
|
|
130
|
+
if (node.nodeType === Node.COMMENT_NODE) doomed.push(node);
|
|
131
|
+
while (walker.nextNode()) {
|
|
132
|
+
const el = walker.currentNode;
|
|
133
|
+
if (el.nodeType === Node.COMMENT_NODE) doomed.push(el);
|
|
134
|
+
else if (DROP_TAGS.has(el.tagName.toLowerCase())) doomed.push(el);
|
|
135
|
+
}
|
|
136
|
+
doomed.forEach((el) => el.remove());
|
|
137
|
+
|
|
138
|
+
for (const el of Array.from(container.querySelectorAll('*'))) {
|
|
139
|
+
if (el.nodeType !== Node.ELEMENT_NODE) continue;
|
|
140
|
+
for (const attr of DROP_ATTRS) el.removeAttribute(attr);
|
|
141
|
+
// 事件属性是脚本入口:粘贴/装载的内容一律不允许携带
|
|
142
|
+
for (const attr of Array.from(el.attributes)) {
|
|
143
|
+
if (attr.name.toLowerCase().startsWith('on')) el.removeAttribute(attr.name);
|
|
144
|
+
}
|
|
145
|
+
stripRuntimeClasses(el);
|
|
146
|
+
// 只有复选框(待办条目)保留 contenteditable=false,其余一律不带着走
|
|
147
|
+
if (el.getAttribute('contenteditable') === 'false' && el.tagName !== 'INPUT') el.removeAttribute('contenteditable');
|
|
148
|
+
sanitizeUrls(el);
|
|
149
|
+
convertFontTag(el);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** a / img 的协议白名单过滤(白名单唯一源见 core/utils/safeValue.js) */
|
|
154
|
+
function sanitizeUrls(el) {
|
|
155
|
+
if (el.tagName === 'A') {
|
|
156
|
+
const href = el.getAttribute('href') || '';
|
|
157
|
+
if (!isSafeHref(href)) el.removeAttribute('href');
|
|
158
|
+
else {
|
|
159
|
+
el.setAttribute('target', '_blank');
|
|
160
|
+
el.setAttribute('rel', 'noopener noreferrer nofollow');
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (el.tagName === 'IMG') {
|
|
164
|
+
const src = (el.getAttribute('src') || '').trim();
|
|
165
|
+
// [坑] 没有 src 的图片不是协议违规,而是宿主的资产占位形态(.gbRtx 正文里的 <img data-gbRtx-asset>):
|
|
166
|
+
// 以前一律按坏图删掉,等于每次打开带容器的正文都把图片抹光
|
|
167
|
+
if (!src) return;
|
|
168
|
+
if (!SAFE_IMG.test(src)) el.remove();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** 历史 `<font color size face>` → span 内联样式(只认这三条,其余属性丢弃) */
|
|
173
|
+
function convertFontTag(el) {
|
|
174
|
+
if (el.tagName !== 'FONT') return;
|
|
175
|
+
const style = [];
|
|
176
|
+
if (el.getAttribute('color')) style.push(`color: ${el.getAttribute('color')}`);
|
|
177
|
+
const face = sanitizeFontName(el.getAttribute('face'));
|
|
178
|
+
if (face) style.push(`font-family: ${face}`);
|
|
179
|
+
const size = Number(el.getAttribute('size'));
|
|
180
|
+
// [口径] 按浏览器对 <font size=1..7> 的关键字映射换算(3 号即默认字号):
|
|
181
|
+
// 写死像素会把存量文档的字号改掉,关键字才跟着正文字号走
|
|
182
|
+
if (size) style.push(`font-size: ${FONT_SIZE_KEYWORDS[Math.min(Math.max(size, 1), 7) - 1]}`);
|
|
183
|
+
const span = document.createElement('span');
|
|
184
|
+
if (style.length) span.setAttribute('style', style.join('; '));
|
|
185
|
+
moveChildren(el, span);
|
|
186
|
+
el.replaceWith(span);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* 容器(编辑面根 / 引用)里直接挂着的行内内容包成段落
|
|
191
|
+
* [坑] 只对这两类容器动手:li 与单元格本来就是文本块(正文直接挂),表格结构(tbody/tr)更不是「游离文本」——
|
|
192
|
+
* 早前一版把它们一起包了 p,存量表格被装成 <table><p><tbody>… 整表正文再也枚举不到
|
|
193
|
+
*/
|
|
194
|
+
function mergeStrayText(container) {
|
|
195
|
+
const hosts = [container, ...Array.from(container.querySelectorAll('blockquote, [data-callout], [data-super], [data-col]'))];
|
|
196
|
+
for (const host of hosts) {
|
|
197
|
+
let run = null;
|
|
198
|
+
for (const child of Array.from(host.childNodes)) {
|
|
199
|
+
if (child.nodeType === Node.TEXT_NODE && !child.data.trim()) {
|
|
200
|
+
child.remove();
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
const needBlock = child.nodeType === Node.TEXT_NODE || (child.nodeType === Node.ELEMENT_NODE && !isBlockish(child));
|
|
204
|
+
if (!needBlock) {
|
|
205
|
+
run = null;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (!run) run = host.insertBefore(document.createElement('p'), child);
|
|
209
|
+
run.appendChild(child);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** 块级形态:可以独立挂在容器下,不需要再包一层 p */
|
|
215
|
+
function isBlockish(el) {
|
|
216
|
+
return /^(P|H[1-6]|UL|OL|BLOCKQUOTE|PRE|TABLE|HR|DIV|LI|TBODY|THEAD|TFOOT|TR|TD|TH|COLGROUP|CAPTION|IMG)$/.test(el.tagName);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** 浏览器遗留的 div 段落 → p(待办条目的正文 div、分页符/节边界块除外) */
|
|
220
|
+
function renameStrayDivs(container) {
|
|
221
|
+
for (const el of Array.from(container.querySelectorAll('div'))) {
|
|
222
|
+
if (isTaskItem(el.parentElement)) continue;
|
|
223
|
+
if (isCell(el.parentElement)) continue;
|
|
224
|
+
if (el.classList.contains(DOC_CLASS.PAGEBREAK)) continue;
|
|
225
|
+
if (el.classList.contains(DOC_CLASS.SECTIONBREAK)) continue;
|
|
226
|
+
if (el.classList.contains(DOC_CLASS.TOC)) continue;
|
|
227
|
+
if (el.hasAttribute(REF_DOC_ATTR)) continue; // 笔记嵌入块(ensureEmbeds 在其后,属性判断不依赖 class)
|
|
228
|
+
if (isContainerBlock(el) || isColumnBlock(el)) continue; // 高亮块/超级块/列外壳:容器不转段落
|
|
229
|
+
el.replaceWith(renameBlock(el, 'p'));
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** li 与单元格内的段落压平(多段之间补 br,保住视觉换行) */
|
|
234
|
+
function flattenBlocksInLiAndCells(container) {
|
|
235
|
+
const hosts = Array.from(container.querySelectorAll('li,td,th'));
|
|
236
|
+
for (const host of hosts) {
|
|
237
|
+
const blocks = Array.from(host.children).filter((el) => /^(P|H[1-6])$/.test(el.tagName));
|
|
238
|
+
if (!blocks.length) continue;
|
|
239
|
+
const frag = document.createDocumentFragment();
|
|
240
|
+
blocks.forEach((block, index) => {
|
|
241
|
+
if (index) frag.appendChild(document.createElement('br'));
|
|
242
|
+
moveChildren(block, frag);
|
|
243
|
+
});
|
|
244
|
+
blocks[0].before(frag);
|
|
245
|
+
blocks.forEach((block) => block.remove());
|
|
246
|
+
if (isTaskItem(host.parentElement)) host.parentElement.replaceWith(host);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** 待办条目收敛到唯一形态:li[data-checked] > label[contenteditable=false] > input + div 正文(存量缺壳、粘贴来的裸 li 都归到这里) */
|
|
251
|
+
function ensureTaskItems(container) {
|
|
252
|
+
for (const list of Array.from(container.querySelectorAll('ul[data-type="taskList"], ol[data-type="taskList"]'))) {
|
|
253
|
+
for (const child of Array.from(list.children)) {
|
|
254
|
+
const li = child.tagName === 'LI' ? child : null;
|
|
255
|
+
if (!li) {
|
|
256
|
+
const wrapped = document.createElement('li');
|
|
257
|
+
moveChildren(child, wrapped);
|
|
258
|
+
child.replaceWith(wrapped);
|
|
259
|
+
wrapped.setAttribute('data-checked', 'false');
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
if (!li.hasAttribute('data-checked')) li.setAttribute('data-checked', 'false');
|
|
263
|
+
let label = li.querySelector(':scope > label');
|
|
264
|
+
if (!label) {
|
|
265
|
+
label = document.createElement('label');
|
|
266
|
+
const input = document.createElement('input');
|
|
267
|
+
input.type = 'checkbox';
|
|
268
|
+
label.appendChild(input);
|
|
269
|
+
li.prepend(label);
|
|
270
|
+
}
|
|
271
|
+
label.setAttribute('contenteditable', 'false');
|
|
272
|
+
const input = label.querySelector('input');
|
|
273
|
+
if (input) {
|
|
274
|
+
input.type = 'checkbox';
|
|
275
|
+
input.checked = li.getAttribute('data-checked') === 'true';
|
|
276
|
+
}
|
|
277
|
+
let div = Array.from(li.children).find((el) => el.tagName === 'DIV');
|
|
278
|
+
if (!div) {
|
|
279
|
+
div = document.createElement('div');
|
|
280
|
+
for (const node of Array.from(li.childNodes)) {
|
|
281
|
+
if (node === label) continue;
|
|
282
|
+
div.appendChild(node);
|
|
283
|
+
}
|
|
284
|
+
li.appendChild(div);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
// 非待办列表里混进的待办形态:剥掉复选框壳,正文回到 li
|
|
289
|
+
for (const li of Array.from(container.querySelectorAll('li[data-checked]'))) {
|
|
290
|
+
if (li.parentElement?.getAttribute('data-type') === 'taskList') continue;
|
|
291
|
+
li.removeAttribute('data-checked');
|
|
292
|
+
const label = li.querySelector(':scope > label');
|
|
293
|
+
const div = li.querySelector(':scope > div');
|
|
294
|
+
label?.remove();
|
|
295
|
+
if (div) moveChildrenBefore(div);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** 代码块必须有 code 子元素,且内部只有纯文本 */
|
|
300
|
+
function ensureCodeBlocks(container) {
|
|
301
|
+
for (const pre of Array.from(container.querySelectorAll('pre'))) {
|
|
302
|
+
const code = pre.querySelector('code');
|
|
303
|
+
const text = (code || pre).textContent || '';
|
|
304
|
+
pre.replaceChildren();
|
|
305
|
+
const next = document.createElement('code');
|
|
306
|
+
next.textContent = text;
|
|
307
|
+
pre.appendChild(next);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** 空文本块补 br:光标有处可落,只读态也保有行高 */
|
|
312
|
+
function fillEmptyBlocks(container) {
|
|
313
|
+
for (const block of Array.from(container.querySelectorAll('p,h1,h2,h3,h4,h5,h6,li,td,th,div'))) {
|
|
314
|
+
if (isTaskItem(block)) continue;
|
|
315
|
+
if (block.classList.contains(DOC_CLASS.PAGEBREAK)) continue;
|
|
316
|
+
if (block.classList.contains(DOC_CLASS.SECTIONBREAK)) continue;
|
|
317
|
+
if (block.classList.contains(DOC_CLASS.TOC)) continue;
|
|
318
|
+
if (block.hasAttribute(REF_DOC_ATTR)) continue; // 笔记嵌入块(空体是常态,渲染产物由宿主 hydrate 补)
|
|
319
|
+
if (isContainerBlock(block) || isColumnBlock(block)) continue; // 容器空否看子块,自身不补占位 br
|
|
320
|
+
if (hasBlockBody(block)) continue;
|
|
321
|
+
block.replaceChildren(document.createElement('br'));
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** 分页符块收敛到唯一形态:div.doc-pagebreak〔空体 + contenteditable=false〕
|
|
326
|
+
* [坑] 不能让它停在 p/其它标签上:那些是文本块,光标会落进空盒、退格还能把标记当段落删一半;
|
|
327
|
+
* 落盘一律不带 contenteditable(serializeBody 会剥),靠这里每次装载补回 */
|
|
328
|
+
function ensurePageBreaks(container) {
|
|
329
|
+
for (const el of Array.from(container.querySelectorAll(`.${DOC_CLASS.PAGEBREAK}`))) {
|
|
330
|
+
const target = el.tagName === 'DIV' ? el : renameBlock(el, 'div');
|
|
331
|
+
target.replaceChildren();
|
|
332
|
+
target.setAttribute('contenteditable', 'false');
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** 节边界块收敛到唯一形态(与分页符同款治理):div.doc-sectionbreak〔空体 + contenteditable=false〕;
|
|
337
|
+
* 它是正文流的编解码标记,不是文本块——光标不落、退格不删,落盘靠序列化剥 contenteditable 后装载补回 */
|
|
338
|
+
function ensureSectionBreaks(container) {
|
|
339
|
+
for (const el of Array.from(container.querySelectorAll(`.${DOC_CLASS.SECTIONBREAK}`))) {
|
|
340
|
+
const target = el.tagName === 'DIV' ? el : renameBlock(el, 'div');
|
|
341
|
+
target.replaceChildren();
|
|
342
|
+
target.setAttribute('contenteditable', 'false');
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** 目录块(TOC 域缓存行)收敛:div.doc-toc〔只读区块,内部行内容原样保留〕 */
|
|
347
|
+
function ensureTocs(container) {
|
|
348
|
+
for (const el of Array.from(container.querySelectorAll(`.${DOC_CLASS.TOC}`))) {
|
|
349
|
+
const target = el.tagName === 'DIV' ? el : renameBlock(el, 'div');
|
|
350
|
+
target.setAttribute('contenteditable', 'false');
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** 脚注/尾注锚点收敛:整枚原子(contenteditable=false),序号文字不可被光标进入改写。
|
|
355
|
+
* 缺 class 的 sup[data-fn](粘贴/存量)按脚注补齐。 */
|
|
356
|
+
function ensureNoteAnchors(container) {
|
|
357
|
+
for (const el of Array.from(container.querySelectorAll('sup'))) {
|
|
358
|
+
if (!el.hasAttribute(NOTE_ATTR)) continue;
|
|
359
|
+
if (!el.classList.contains(DOC_CLASS.FOOTNOTE) && !el.classList.contains(DOC_CLASS.ENDNOTE)) {
|
|
360
|
+
el.classList.add(DOC_CLASS.FOOTNOTE);
|
|
361
|
+
}
|
|
362
|
+
el.setAttribute('contenteditable', 'false');
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** 笔记行内锚收敛:补 class(粘贴链会剥)与 contenteditable=false(落盘一律剥、装载补回,照分页符口径) */
|
|
367
|
+
function ensureAnchors(container) {
|
|
368
|
+
for (const el of Array.from(container.querySelectorAll('span'))) {
|
|
369
|
+
if (el.hasAttribute(ATTACH_ATTR)) {
|
|
370
|
+
el.classList.add(DOC_CLASS.ATTACH);
|
|
371
|
+
el.setAttribute('contenteditable', 'false');
|
|
372
|
+
} else if (el.hasAttribute(REF_DOC_ATTR)) {
|
|
373
|
+
el.classList.add(DOC_CLASS.REF);
|
|
374
|
+
el.setAttribute('contenteditable', 'false');
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** 笔记嵌入块收敛:div.doc-embed〔只读区块,渲染产物原样保留;落盘剥、装载补,照 TOC 口径〕 */
|
|
380
|
+
function ensureEmbeds(container) {
|
|
381
|
+
for (const el of Array.from(container.querySelectorAll('[data-ref-doc]'))) {
|
|
382
|
+
if (el.tagName !== 'DIV') continue;
|
|
383
|
+
el.classList.add(DOC_CLASS.EMBED);
|
|
384
|
+
el.setAttribute('contenteditable', 'false');
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* 序列化编辑面为落盘 HTML:只剥运行期装饰,正文结构原样保留
|
|
390
|
+
* [坑] 复选框的勾选状态是 property,不进 innerHTML,必须同步成属性;
|
|
391
|
+
* contenteditable 是编辑面运行期属性,落盘一律剥掉(装载时 ensureTaskItems 会补回来)
|
|
392
|
+
* @param {Element} root 编辑面根元素
|
|
393
|
+
*/
|
|
394
|
+
export function serializeBody(root) {
|
|
395
|
+
const clone = root.cloneNode(true);
|
|
396
|
+
// 运行期布局结构(Word 分页占位/栏容器/拆分块):整块还原成单流正文,再剥装饰类
|
|
397
|
+
restoreRuntimeStructures(clone);
|
|
398
|
+
clone.querySelectorAll(RUNTIME_CLASS_SELECTOR).forEach((el) => stripRuntimeClasses(el));
|
|
399
|
+
clone.querySelectorAll('input[type="checkbox"]').forEach((input) => {
|
|
400
|
+
if (input.checked) input.setAttribute('checked', '');
|
|
401
|
+
else input.removeAttribute('checked');
|
|
402
|
+
});
|
|
403
|
+
clone.querySelectorAll('[contenteditable]').forEach((el) => el.removeAttribute('contenteditable'));
|
|
404
|
+
return clone.innerHTML;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* 解析外部 HTML 为可插入片段(粘贴用):清洗已由 pasteClean 做完,这里只做形态规范化
|
|
409
|
+
* @param {string} html
|
|
410
|
+
* @returns {DocumentFragment}
|
|
411
|
+
*/
|
|
412
|
+
export function parseFragment(html) {
|
|
413
|
+
const tpl = document.createElement('template');
|
|
414
|
+
tpl.innerHTML = String(html || '');
|
|
415
|
+
normalizeBody(tpl.content);
|
|
416
|
+
return tpl.content;
|
|
417
|
+
}
|