|
@@ -0,0 +1,198 @@
|
|
|
|
|
+/**
|
|
|
|
|
+ * 富文本默认字号处理
|
|
|
|
|
+ *
|
|
|
|
|
+ * unified_attrib.font_size 的语义是「没有单独设置字号的文本的默认字号」。
|
|
|
|
|
+ * 如果只把字号挂在富文本容器上,块级元素会继承这个字号,块自身的行盒(strut)
|
|
|
|
|
+ * 就会按 默认字号 × 行高 撑开:例如默认 18pt 的组件里,整段都设置为 9pt 的
|
|
|
|
|
+ * 文字每行仍占 27pt(36px),组件高度于是由默认字号决定,而不是由富文本内容决定。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 处理方式:把默认字号下沉到决定行盒高度的块级元素上
|
|
|
|
|
+ * - 块内存在未设置字号的文本:保持继承默认字号(容器上仍然设置着默认字号)
|
|
|
|
|
+ * - 块内文本都设置了字号、且都小于默认字号:把块字号收紧为其中最大的字号
|
|
|
|
|
+ * 这样既保留「默认字号作用于未设置字号的文本」,又让行高跟随文本自己的字号。
|
|
|
|
|
+ */
|
|
|
|
|
+
|
|
|
|
|
+// 决定行盒高度的块级元素
|
|
|
|
|
+const BLOCK_TAGS = new Set([
|
|
|
|
|
+ 'ADDRESS',
|
|
|
|
|
+ 'ARTICLE',
|
|
|
|
|
+ 'ASIDE',
|
|
|
|
|
+ 'BLOCKQUOTE',
|
|
|
|
|
+ 'DD',
|
|
|
|
|
+ 'DIV',
|
|
|
|
|
+ 'DL',
|
|
|
|
|
+ 'DT',
|
|
|
|
|
+ 'FIELDSET',
|
|
|
|
|
+ 'FIGCAPTION',
|
|
|
|
|
+ 'FIGURE',
|
|
|
|
|
+ 'FOOTER',
|
|
|
|
|
+ 'FORM',
|
|
|
|
|
+ 'H1',
|
|
|
|
|
+ 'H2',
|
|
|
|
|
+ 'H3',
|
|
|
|
|
+ 'H4',
|
|
|
|
|
+ 'H5',
|
|
|
|
|
+ 'H6',
|
|
|
|
|
+ 'HEADER',
|
|
|
|
|
+ 'LI',
|
|
|
|
|
+ 'MAIN',
|
|
|
|
|
+ 'NAV',
|
|
|
|
|
+ 'OL',
|
|
|
|
|
+ 'P',
|
|
|
|
|
+ 'PRE',
|
|
|
|
|
+ 'SECTION',
|
|
|
|
|
+ 'TD',
|
|
|
|
|
+ 'TH',
|
|
|
|
|
+ 'UL',
|
|
|
|
|
+]);
|
|
|
|
|
+
|
|
|
|
|
+const PT_TO_PX = 4 / 3;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 解析绝对单位字号为 px,相对单位(em/%/rem 等)依赖父级字号,无法单独换算
|
|
|
|
|
+ * @param {string} value 字号值,如 '9pt'、'12px'
|
|
|
|
|
+ * @returns {number|null} px 值;无法换算时返回 null
|
|
|
|
|
+ */
|
|
|
|
|
+function parseFontSizePx(value) {
|
|
|
|
|
+ const match = String(value || '')
|
|
|
|
|
+ .trim()
|
|
|
|
|
+ .match(/^(\d+(?:\.\d+)?)(pt|px)$/i);
|
|
|
|
|
+ if (!match) return null;
|
|
|
|
|
+
|
|
|
|
|
+ const amount = parseFloat(match[1]);
|
|
|
|
|
+ if (!Number.isFinite(amount)) return null;
|
|
|
|
|
+
|
|
|
|
|
+ return match[2].toLowerCase() === 'pt' ? amount * PT_TO_PX : amount;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 取元素自身内联样式的字号
|
|
|
|
|
+ * @param {HTMLElement} el 元素
|
|
|
|
|
+ * @returns {string} 内联字号,未设置时返回空字符串
|
|
|
|
|
+ */
|
|
|
|
|
+function getInlineFontSize(el) {
|
|
|
|
|
+ if (!el || !el.style) return '';
|
|
|
|
|
+ return String(el.style.fontSize || '').trim();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 向上查找文本的块级祖先(块级祖先决定该行文本所在的行盒)
|
|
|
|
|
+ * @param {Node} node 文本节点
|
|
|
|
|
+ * @param {HTMLElement} root 内容根节点
|
|
|
|
|
+ * @returns {HTMLElement|null} 块级祖先
|
|
|
|
|
+ */
|
|
|
|
|
+function findBlockAncestor(node, root) {
|
|
|
|
|
+ let el = node.parentElement;
|
|
|
|
|
+ while (el && el !== root) {
|
|
|
|
|
+ if (BLOCK_TAGS.has(el.nodeName)) return el;
|
|
|
|
|
+ el = el.parentElement;
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 取文本实际生效的内联字号(自身或到块级祖先之间的行内祖先)
|
|
|
|
|
+ * @param {Node} node 文本节点
|
|
|
|
|
+ * @param {HTMLElement} block 文本所属的块级元素
|
|
|
|
|
+ * @returns {string} 字号,未设置时返回空字符串
|
|
|
|
|
+ */
|
|
|
|
|
+function findInlineFontSize(node, block) {
|
|
|
|
|
+ let el = node.parentElement;
|
|
|
|
|
+ while (el) {
|
|
|
|
|
+ const fontSize = getInlineFontSize(el);
|
|
|
|
|
+ if (fontSize) return fontSize;
|
|
|
|
|
+ if (el === block) break;
|
|
|
|
|
+ el = el.parentElement;
|
|
|
|
|
+ }
|
|
|
|
|
+ return '';
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 按执行顺序收集块级元素自身的文本字号信息
|
|
|
|
|
+ * @param {HTMLElement} root 内容根节点
|
|
|
|
|
+ * @returns {Map<HTMLElement, {hasUnstyled: boolean, hasUnstyledDescendant: boolean,
|
|
|
|
|
+ * pxList: number[], sizeList: string[]}>}
|
|
|
|
|
+ */
|
|
|
|
|
+function collectBlockFontSizes(root) {
|
|
|
|
|
+ const infoByBlock = new Map();
|
|
|
|
|
+ const walker = root.ownerDocument.createTreeWalker(root, NodeFilter.SHOW_TEXT);
|
|
|
|
|
+
|
|
|
|
|
+ while (walker.nextNode()) {
|
|
|
|
|
+ const textNode = walker.currentNode;
|
|
|
|
|
+ if (!textNode.nodeValue || !textNode.nodeValue.trim()) continue;
|
|
|
|
|
+
|
|
|
|
|
+ const block = findBlockAncestor(textNode, root);
|
|
|
|
|
+ // 根级文本(没有块级祖先)由容器上的默认字号兜底
|
|
|
|
|
+ if (!block) continue;
|
|
|
|
|
+
|
|
|
|
|
+ const info = infoByBlock.get(block) || {
|
|
|
|
|
+ hasUnstyled: false,
|
|
|
|
|
+ hasUnstyledDescendant: false,
|
|
|
|
|
+ pxList: [],
|
|
|
|
|
+ sizeList: [],
|
|
|
|
|
+ };
|
|
|
|
|
+ const size = findInlineFontSize(textNode, block);
|
|
|
|
|
+ const px = parseFontSizePx(size);
|
|
|
|
|
+
|
|
|
|
|
+ if (px === null) {
|
|
|
|
|
+ info.hasUnstyled = true;
|
|
|
|
|
+ let ancestor = block.parentElement;
|
|
|
|
|
+ while (ancestor && ancestor !== root) {
|
|
|
|
|
+ if (BLOCK_TAGS.has(ancestor.nodeName)) {
|
|
|
|
|
+ const ancestorInfo = infoByBlock.get(ancestor) || {
|
|
|
|
|
+ hasUnstyled: false,
|
|
|
|
|
+ hasUnstyledDescendant: false,
|
|
|
|
|
+ pxList: [],
|
|
|
|
|
+ sizeList: [],
|
|
|
|
|
+ };
|
|
|
|
|
+ ancestorInfo.hasUnstyledDescendant = true;
|
|
|
|
|
+ infoByBlock.set(ancestor, ancestorInfo);
|
|
|
|
|
+ }
|
|
|
|
|
+ ancestor = ancestor.parentElement;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ info.pxList.push(px);
|
|
|
|
|
+ info.sizeList.push(size);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ infoByBlock.set(block, info);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return infoByBlock;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 依据默认字号处理富文本 HTML,使其高度跟随文本自身的字号
|
|
|
|
|
+ * @param {string} html 富文本 HTML
|
|
|
|
|
+ * @param {string|number} defaultFontSize 统一属性中的默认字号,如 '12pt'
|
|
|
|
|
+ * @returns {string} 处理后的 HTML;无需处理时原样返回
|
|
|
|
|
+ */
|
|
|
|
|
+export function applyRichTextDefaultFontSize(html, defaultFontSize) {
|
|
|
|
|
+ if (typeof html !== 'string' || !html) return html;
|
|
|
|
|
+
|
|
|
|
|
+ const defaultPx = parseFontSizePx(defaultFontSize);
|
|
|
|
|
+ // 默认字号不是绝对单位时不处理,避免误改相对单位的继承链
|
|
|
|
|
+ if (defaultPx === null) return html;
|
|
|
|
|
+ if (!/<[a-z][\s\S]*>/i.test(html)) return html;
|
|
|
|
|
+ // 没有任何内联字号时无需解析,历史内容原样返回
|
|
|
|
|
+ if (!/font-size\s*:/i.test(html)) return html;
|
|
|
|
|
+
|
|
|
|
|
+ const doc = new DOMParser().parseFromString(`<body>${html}</body>`, 'text/html');
|
|
|
|
|
+ const root = doc.body;
|
|
|
|
|
+ let changed = false;
|
|
|
|
|
+
|
|
|
|
|
+ collectBlockFontSizes(root).forEach((info, block) => {
|
|
|
|
|
+ // 未设置字号的后代文本会继承块字号,因此不能收缩该块
|
|
|
|
|
+ if (info.hasUnstyled || info.hasUnstyledDescendant || !info.pxList.length) return;
|
|
|
|
|
+
|
|
|
|
|
+ const maxPx = Math.max(...info.pxList);
|
|
|
|
|
+ // 文本字号不小于默认字号时,行盒不会被默认字号撑大,保持原样
|
|
|
|
|
+ if (maxPx >= defaultPx) return;
|
|
|
|
|
+
|
|
|
|
|
+ block.style.fontSize = info.sizeList[info.pxList.indexOf(maxPx)];
|
|
|
|
|
+ changed = true;
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 未改动时返回原始 HTML,避免序列化差异影响历史内容
|
|
|
|
|
+ return changed ? root.innerHTML : html;
|
|
|
|
|
+}
|