Files
cjk-autospace/test/spacing-detail.test.mjs
T

121 lines
4.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 針對性驗證:v1 之「行內塊(pill)邊界空格」與「原檔多餘空格收斂」是否
// 在新引擎落地(回應使用者提問)。
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { test } from "node:test";
import { setupDom, countTag } from "./helpers.mjs";
import { createJuzhen } from "../dist/juzhen.mjs";
const SPACING_ONLY = { jiya: false, longWord: false, jinze: false };
// ----- 原檔多餘空格收斂(STRIP_CJK_SPACE_ANS / STRIP_ANS_SPACE_CJK)-----
test("strip:CJK↔Latin 間之作者空格被移除(間隙改由 margin,複製緊湊)", () =>
{
const { document } = setupDom("<p>用 Neovim 寫</p>");
createJuzhen(SPACING_ONLY).render(document.body);
const p = document.querySelector("p");
assert.equal(p.textContent, "用Neovim寫", "作者空格移除,間隙改由 jz-hws margin(copy 緊湊)");
assert.equal(countTag(p, "jz-hws"), 2);
});
test("strip:間隙 copy-clean(無注入空白,textContent 不含中西間空白)", () =>
{
const { document } = setupDom("<p>裝 fzf 工具</p>");
createJuzhen(SPACING_ONLY).render(document.body);
const p = document.querySelector("p");
assert.equal(p.textContent, "裝fzf工具", "中西間隙不入 textContent(純 copy-clean)");
assert.equal(countTag(p, "jz-hws"), 2);
});
test("strip:全流程(含 charify)下作者空格仍收斂、不雙空格", () =>
{
const { document } = setupDom('<p lang="zh-Hans">用 TypeScript 寫,很好</p>');
createJuzhen({}).render(document.body);
const p = document.querySelector("p");
assert.ok(!/ {2,}/.test(p.textContent), "無雙空格:" + JSON.stringify(p.textContent));
});
// ----- 行內塊(pill)邊界空格(Pass B)-----
test("pill:code 與 CJK 緊鄰兩側各插一 jz-hws", () =>
{
const { document } = setupDom("<p>用<code>vim</code>編輯</p>");
createJuzhen(SPACING_ONLY).render(document.body);
assert.equal(countTag(document.querySelector("p"), "jz-hws"), 2);
});
test("pill:code 與 Latin 緊鄰亦插間隙(PILL_NEIGHBOR 含 A-Za-z0-9)", () =>
{
const { document } = setupDom("<p>看<code>x</code>嗎</p>");
createJuzhen(SPACING_ONLY).render(document.body);
assert.equal(countTag(document.querySelector("p"), "jz-hws"), 2);
});
test("pill:已有作者空格時收斂為單一 jz-hws(不雙空格)", () =>
{
const { document } = setupDom("<p>用 <code>vim</code> 編輯</p>");
createJuzhen(SPACING_ONLY).render(document.body);
const p = document.querySelector("p");
assert.ok(!/ {2,}/.test(p.textContent), "無雙空格:" + JSON.stringify(p.textContent));
assert.equal(countTag(p, "jz-hws"), 2);
});
test("pill:標點與 code 之間不強插間隙(鄰字非 PILL_NEIGHBOR)", () =>
{
const { document } = setupDom("<p>他說:<code>vim</code>很好</p>");
createJuzhen(SPACING_ONLY).render(document.body);
// : 非 PILL_NEIGHBOR → 左側不插;code|很 右側插 1。
assert.equal(countTag(document.querySelector("p"), "jz-hws"), 1);
});
// ----- 間隔號 ·(U+00B7)不觸發中西間隙 -----
test("間隔號:姓名 · 兩側不插 jz-hws(· 屬 CJK 標點,非西文)", () =>
{
const { document } = setupDom("<p>卡尔·马克思</p>");
createJuzhen(SPACING_ONLY).render(document.body);
assert.equal(countTag(document.querySelector("p"), "jz-hws"), 0, "· 兩側皆不應有間隙");
});
// ----- ASCII 左括號(Safari 16 無 lookbehind 路徑)-----
test("左括號:普通英數字後補間隙,連續樣式不漏匹配", () =>
{
const { document } = setupDom("<p>A(b[c{中</p>");
createJuzhen(SPACING_ONLY).render(document.body);
assert.equal(countTag(document.querySelector("p"), "jz-hws"), 3);
});
test("左括號:dot-prefixed 英數字串段不補間隙", () =>
{
const { document } = setupDom("<p>.foo(中</p><p>x.bar[文</p><p>.baz{字</p>");
createJuzhen(SPACING_ONLY).render(document.body);
for (const p of document.querySelectorAll("p"))
{
assert.equal(countTag(p, "jz-hws"), 0);
}
});
test("左括號:點號不在當前英數字 suffix 前時仍補間隙", () =>
{
const { document } = setupDom("<p>.foo-bar(中</p>");
createJuzhen(SPACING_ONLY).render(document.body);
assert.equal(countTag(document.querySelector("p"), "jz-hws"), 1);
});
test("Safari 16 產物:spacing 源碼與發佈 JS 不得含 lookbehind 語法", () =>
{
const lookbehindSyntax = /\(\?<([=!])/;
for (const path of [
new URL("../src/typeset/spacing.ts", import.meta.url),
new URL("../dist/juzhen.mjs", import.meta.url),
new URL("../dist/juzhen.iife.js", import.meta.url),
])
{
assert.doesNotMatch(readFileSync(path, "utf8"), lookbehindSyntax, path.pathname);
}
});