Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(extension): normalize nested code block descendants to pre (#703)

sivdeade90a6c0c821b
diff --git a/apps/extension/region-clip.test.mjs b/apps/extension/region-clip.test.mjs
index 17d7036e..be8f6375 100644
--- a/apps/extension/region-clip.test.mjs
+++ b/apps/extension/region-clip.test.mjs
@@ -13,9 +13,11 @@ test("region conversion preserves only chosen visible content and does not alter
test("shrinking a table or code block keeps indivisible Markdown containers intact", async () => {
const { RegionSelection, regionMarkdown, regionWrapper } = await import("./src/region-selection");
- const window = createWindow('<section id="chosen"><table id="tbl"><caption id="cap">Summary</caption><tbody id="body"><tr id="row"><th id="h1">H1</th><th id="h2">H2</th></tr><tr><td>A</td><td>B</td></tr></tbody></table><pre id="pre"><code id="code">line 1\n\nline 2</code></pre></section>', "https://example.test/story");
+ const window = createWindow('<section id="chosen"><table id="tbl"><caption id="cap">Summary</caption><tbody id="body"><tr id="row"><th id="h1">H1</th><th id="h2">H2</th></tr><tr><td>A</td><td>B</td></tr></tbody></table><pre id="pre"><code id="code"><span id="kw">const</span> <span id="expr"><span id="ident">line</span> = 1;</span>\n\n<span id="ret">line 2</span></code></pre></section>', "https://example.test/story");
for (const element of Array.from(window.document.querySelectorAll("*"))) {
- element.getBoundingClientRect = () => ({ width: 120, height: 40, left: 0, top: 0, right: 120, bottom: 40 });
+ const width = element.tagName === "SPAN" ? 36 : 120;
+ const height = element.tagName === "SPAN" ? 16 : 40;
+ element.getBoundingClientRect = () => ({ width, height, left: 0, top: 0, right: width, bottom: height });
}
const tableSelection = new RegionSelection(regionWrapper(window.document.getElementById("h1")));
expect(tableSelection.element.id).toBe("tbl");
@@ -36,10 +38,13 @@ test("shrinking a table or code block keeps indivisible Markdown containers inta
expect(wholeTable.element.id).toBe("tbl");
expect(regionMarkdown(wholeTable.element, "https://example.test/story")).toBe("| H1 | H2 |\n| --- | --- |\n| A | B |");
- const codeSelection = new RegionSelection(regionWrapper(window.document.getElementById("code")));
+ for (const targetId of ["code", "kw", "ident"]) {
+ expect(regionWrapper(window.document.getElementById(targetId)).id).toBe("pre");
+ }
+ const codeSelection = new RegionSelection(regionWrapper(window.document.getElementById("ident")));
expect(codeSelection.element.id).toBe("pre");
expect(codeSelection.canShrink).toBe(false);
codeSelection.child();
expect(codeSelection.element.id).toBe("pre");
- expect(regionMarkdown(codeSelection.element, "https://example.test/story")).toBe("```\nline 1\n\nline 2\n```");
+ expect(regionMarkdown(codeSelection.element, "https://example.test/story")).toBe("```\nconst line = 1;\n\nline 2\n```");
});
diff --git a/apps/extension/src/region-selection.ts b/apps/extension/src/region-selection.ts
index e75ea817..6d981621 100644
--- a/apps/extension/src/region-selection.ts
+++ b/apps/extension/src/region-selection.ts
@@ -18,7 +18,7 @@ const sameBox = (a: Element, b: Element) => {
};
const indivisibleChild = (element: Element, parent: Element | null) =>
- !!parent && ((element.matches("code") && parent.matches("pre"))
+ !!parent && (!!parent.closest("pre")
|| (element.matches("thead,tbody,tfoot,tr,td,th") && parent.matches("table,thead,tbody,tfoot,tr")));
const selectableDirectChild = (parent: Element): HTMLElement | undefined =>
@@ -26,7 +26,10 @@ const selectableDirectChild = (parent: Element): HTMLElement | undefined =>
export const regionWrapper = (element: HTMLElement): HTMLElement => {
const parent = element.parentElement;
- if (!parent || element.tagName === "BODY" || !selectableRegion(parent)) return element;
+ if (!parent || element.tagName === "BODY") return element;
+ const pre = parent.closest("pre");
+ if (selectableRegion(pre)) return regionWrapper(pre);
+ if (!selectableRegion(parent)) return element;
const hasSibling = Array.from(parent.children).some(child => child !== element && selectableRegion(child));
return indivisibleChild(element, parent) || (sameBox(element, parent) && !hasSibling) ? regionWrapper(parent) : element;
};