fix: 行內排版透明性——pill 前標點誤隙+行內元素整體掉行+審計(Bug1/Bug2/T4-T6)

統一「行內元素對排版透明」原則在各遍歷原語之口徑,修復兩個下游 Bug 及三項同根隱患。

Bug1(pill 前標點誤加間隙):
  isTransparentInline 補 JZ-CHAR/JZ-INNER。`漢字。<code>` 經禁則綁定後,
  adjacentLogicalChar 之 descend 遇不透明 jz-char 既不進入亦不終止、而跳過它越過
  標點取到「字」誤補 jz-hws;透明後回傳標點「。」(非 pill 鄰字)→ 不補隙,與
  collectRuns 既有透明遞迴對齊。

Bug2(行內樣式元素整體掉行):
  新增 core/split.ts 之 isolateBoundaryToken/splitElementAt——沿祖先鏈逐層把邊界
  詞元剝入淺克隆(保留標籤/class/style/lang、去 id;標 data-jz-kind="splitoff"),
  jinze/orphan 改最小綁定:`<strong>長串</strong>,` 只綁末字+逗號(非整串入 nowrap
  inline-block 致行末整體掉行);段末長 strong 不再整綁溢出容器。revertPass 增延後
  階段把 splitoff 併回原元素(先解包 wrap、後併 splitoff),render→revert 還原一致。
  純樣式標籤白名單可切,a/abbr/ruby/帶 id 退化整綁。

審計(同根隱患):
  T4 lineedge edgeRect 攀爬集改由 options.finder.styleInlines 派生(棄硬編碼子集,
     免遺漏 ins/del/sub 等致行端誤判)。
  T5 jiyaAdjacency 遇 pill(pillMatches)斷相鄰、不穿越(內容對排版不可見)。
  T6 spacing processPills 補 per-node 功能閘(data-juzhen-off=spacing 區域內不補隙)。

文檔:README 切片表(spacing/jinze/orphan 三行)+ARCHITECTURE §4.1 透明性原則、
  §4.4 splitoff 還原、§6.4 行內元素最小綁定、§5 jz-orphan 詞彙。

測試 90→102(Bug1×4、Bug2×6、T5/T6×2)。headless Chrome 實證 Bug2:段末 strong
最小切分、orphan 寬 60px 不溢出版心。
This commit is contained in:
2026-06-11 16:06:17 +08:00
parent f3fe2399a7
commit 2890549f44
14 changed files with 985 additions and 89 deletions
+28 -10
View File
@@ -62,9 +62,20 @@ function uninstallLayout(root: Element, key: string): void
if (ro) { ro.disconnect(); m.delete(key); }
}
// 我們的行內包裹:跨越它們找相鄰流內容(jz-char 可能被 jz-jinze 包住,其流
// 鄰居在群組之外)。遇 block 邊界即止(回 null,視為行端)。
const INLINE_WRAP = new Set([ "JZ-JINZE", "JZ-CHAR", "JZ-INNER", "JZ-HWS", "SPAN", "STRONG", "EM", "A", "B", "I", "U", "MARK" ]);
// 行內包裹集:edgeRect 攀爬時須跨越「不中斷行內流」之元素找相鄰內容(jz-char 可能被
// jz-jinze 包住,其流鄰居在群組外)。遇 block 邊界即止(回 null,視為行端)。
// **由 options.finder.styleInlines 派生**+聚珍自身之 jz 包裹標籤——舊版硬編碼僅含
// SPAN/STRONG/EM/A/B/I/U/MARK 子集,遇未列之樣式行內(S/INS/DEL/SUB/SUP/SMALL/
// ABBR/CITE/DFN/Q/VAR/SAMP/TIME/BDO/BDI/RUBY 等)會提前止步、誤判行端而錯加行端
// 半形(透明性不一致之同根隱患 T4)。
const JZ_WRAP_TAGS = [ "JZ-JINZE", "JZ-CHAR", "JZ-INNER", "JZ-HWS" ];
function buildWrapSet(options: ResolvedOptions): Set<string>
{
const s = new Set<string>(JZ_WRAP_TAGS);
for (const name of options.finder.styleInlines) { s.add(name); }
return s;
}
function rectOf(n: Node, side: "prev" | "next"): DOMRect | null
{
@@ -90,7 +101,12 @@ function rectOf(n: Node, side: "prev" | "next"): DOMRect | null
/** 取某節點側邊界字元之矩形(橫排:判定相鄰內容是否同行);跨我們的行內包裹
* 向上攀爬,遇 block 邊界回 null(視為行端)。 */
function edgeRect(start: Node | null, side: "prev" | "next", from: Node): DOMRect | null
function edgeRect(
start: Node | null,
side: "prev" | "next",
from: Node,
wrapSet: Set<string>,
): DOMRect | null
{
let n = start;
let parentRef: Node | null = from.parentNode;
@@ -104,7 +120,7 @@ function edgeRect(start: Node | null, side: "prev" | "next", from: Node): DOMRec
}
// 本層無內容:若父為行內包裹,攀出續找其鄰居。
if (!parentRef || parentRef.nodeType !== 1) { return null; }
if (!INLINE_WRAP.has((parentRef as Element).nodeName)) { return null; }
if (!wrapSet.has((parentRef as Element).nodeName)) { return null; }
n = side === "prev" ? parentRef.previousSibling : parentRef.nextSibling;
parentRef = parentRef.parentNode;
}
@@ -189,6 +205,7 @@ interface EdgeDecision { el: Element; edge: "head" | "tail"; mode: LineEndMode;
function relayout(root: Element, finder: Finder, options: ResolvedOptions): void
{
clearEdge(root);
const wrapSet = buildWrapSet(options);
const chars = Array.from(root.querySelectorAll("jz-char")).filter(
(c) => finder.inScope(c) && !finder.isAvoided(c),
@@ -219,12 +236,12 @@ function relayout(root: Element, finder: Finder, options: ResolvedOptions): void
if (isHead)
{
const prev = edgeRect(el.previousSibling, "prev", el);
const prev = edgeRect(el.previousSibling, "prev", el, wrapSet);
if (!prev || higherLine(r, prev)) { decisions.push({ el, edge: "head", mode }); }
}
else
{
const next = edgeRect(el.nextSibling, "next", el);
const next = edgeRect(el.nextSibling, "next", el, wrapSet);
if (!next || lowerLine(r, next)) { decisions.push({ el, edge: "tail", mode }); }
}
}
@@ -259,7 +276,7 @@ export const lineEdgePass: StandalonePass = {
const TRIM = "jz-hws-trim";
function trimGaps(root: Element, finder: Finder): void
function trimGaps(root: Element, finder: Finder, wrapSet: Set<string>): void
{
const gaps = Array.from(root.querySelectorAll("jz-hws")).filter(
(g) => finder.inScope(g) && !finder.isAvoided(g) && finder.levelAllows(g, "paragraph"),
@@ -285,7 +302,7 @@ function trimGaps(root: Element, finder: Finder): void
for (const g of gaps)
{
const r = lastRect(g);
const next = r ? edgeRect(g.nextSibling, "next", g) : null;
const next = r ? edgeRect(g.nextSibling, "next", g, wrapSet) : null;
const atLineEnd = !r || !next || next.top > r.top + EPS;
if (!atLineEnd) { g.classList.remove(TRIM); }
}
@@ -300,7 +317,8 @@ export const gapTrimPass: StandalonePass = {
render(ctx: RenderContext): void
{
if (!hasLayout()) { return; }
installLayout(ctx.root, "gapTrim", () => trimGaps(ctx.root, ctx.finder));
const wrapSet = buildWrapSet(ctx.options);
installLayout(ctx.root, "gapTrim", () => trimGaps(ctx.root, ctx.finder, wrapSet));
},
revert(ctx: RenderContext): void
{