perf(table-info): mark DDL search hits on the rendered DOM
This commit is contained in:
6 files changed
+172
-15
No files matched your search
@@ -0,0 +1,72 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
import { applyDdlSearchMarks, clearDdlSearchMarks, DDL_SEARCH_MARK_CLASS } from "@/lib/sql/ddlSearchMarks";
|
||||
|
||||
let pre: HTMLElement;
|
||||
|
||||
function marks(): HTMLElement[] {
|
||||
return Array.from(pre.querySelectorAll<HTMLElement>(`mark.${DDL_SEARCH_MARK_CLASS}`));
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
pre = document.createElement("pre");
|
||||
document.body.replaceChildren(pre);
|
||||
});
|
||||
|
||||
describe("applyDdlSearchMarks", () => {
|
||||
it("marks every occurrence, ignoring case", () => {
|
||||
pre.innerHTML = "<span>child wo</span><span>CHILD</span>";
|
||||
applyDdlSearchMarks(pre, "chiLD");
|
||||
expect(marks().map((mark) => mark.textContent)).toEqual(["child", "CHILD"]);
|
||||
});
|
||||
|
||||
it("keeps the underlying DDL text unchanged", () => {
|
||||
pre.innerHTML = "<span>CREATE TABLE [dbo].[Orders] (</span>";
|
||||
const before = pre.textContent;
|
||||
applyDdlSearchMarks(pre, "table");
|
||||
expect(pre.textContent).toBe(before);
|
||||
expect(marks()).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("treats the query literally instead of as a regex", () => {
|
||||
pre.innerHTML = "<span>SUM(a+b) AND (b+c)</span>";
|
||||
applyDdlSearchMarks(pre, "(b+c)");
|
||||
expect(marks().map((mark) => mark.textContent)).toEqual(["(b+c)"]);
|
||||
|
||||
applyDdlSearchMarks(pre, "a+b");
|
||||
expect(marks().map((mark) => mark.textContent)).toEqual(["a+b"]);
|
||||
});
|
||||
|
||||
it("marks matches inside nested elements and next to each other", () => {
|
||||
pre.innerHTML = '<span class="hl"><span>name_name</span></span>';
|
||||
applyDdlSearchMarks(pre, "name");
|
||||
expect(marks().map((mark) => mark.textContent)).toEqual(["name", "name"]);
|
||||
expect(pre.textContent).toBe("name_name");
|
||||
});
|
||||
|
||||
it("clears the previous marks when the query changes or empties", () => {
|
||||
pre.innerHTML = "<span>alpha beta</span>";
|
||||
applyDdlSearchMarks(pre, "alpha");
|
||||
applyDdlSearchMarks(pre, "beta");
|
||||
expect(marks().map((mark) => mark.textContent)).toEqual(["beta"]);
|
||||
|
||||
applyDdlSearchMarks(pre, "");
|
||||
expect(marks()).toHaveLength(0);
|
||||
expect(pre.textContent).toBe("alpha beta");
|
||||
});
|
||||
|
||||
it("does not match across element boundaries", () => {
|
||||
pre.innerHTML = "<span>child</span><span>worker</span>";
|
||||
applyDdlSearchMarks(pre, "childworker");
|
||||
expect(marks()).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("ignores missing elements and treats the query literally", () => {
|
||||
expect(() => applyDdlSearchMarks(null, "x")).not.toThrow();
|
||||
expect(() => clearDdlSearchMarks(undefined)).not.toThrow();
|
||||
|
||||
pre.innerHTML = "<span>[Id] int NOT NULL,</span>";
|
||||
applyDdlSearchMarks(pre, "[Id] ");
|
||||
expect(marks().map((mark) => mark.textContent)).toEqual(["[Id] "]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* Search highlighting for the table-info DDL panel (#9212).
|
||||
*
|
||||
* The DDL is syntax-highlighted and rendered once; the search marks are then
|
||||
* applied straight to that rendered DOM. Rebuilding the highlighted HTML for
|
||||
* every keystroke used to re-parse the whole document (these DDLs are often
|
||||
* tens to hundreds of KB) and re-run the SQL highlighter, which made typing in
|
||||
* the DDL search box look like a freeze.
|
||||
*/
|
||||
export const DDL_SEARCH_MARK_CLASS = "ddl-search-match";
|
||||
|
||||
/** Removes every search mark, keeping the underlying DDL text untouched. */
|
||||
export function clearDdlSearchMarks(pre: HTMLElement | null | undefined): void {
|
||||
if (!pre) return;
|
||||
const marks = Array.from(pre.querySelectorAll(`mark.${DDL_SEARCH_MARK_CLASS}`));
|
||||
for (const mark of marks) {
|
||||
const parent = mark.parentNode;
|
||||
if (!parent) continue;
|
||||
parent.replaceChild(pre.ownerDocument.createTextNode(mark.textContent ?? ""), mark);
|
||||
parent.normalize();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rebuilds the search marks for `query` inside the rendered DDL.
|
||||
*
|
||||
* Only text nodes are searched, so a match never spans element boundaries —
|
||||
* the same limit the previous HTML-rewriting implementation had. Matching is
|
||||
* case-insensitive and literal (no regex), which keeps `(`, `%` and friends
|
||||
* from being interpreted as patterns.
|
||||
*/
|
||||
export function applyDdlSearchMarks(pre: HTMLElement | null | undefined, query: string): void {
|
||||
if (!pre) return;
|
||||
clearDdlSearchMarks(pre);
|
||||
const needle = query.toLowerCase();
|
||||
if (!needle) return;
|
||||
|
||||
const textNodes: Text[] = [];
|
||||
const walker = pre.ownerDocument.createTreeWalker(pre, NodeFilter.SHOW_TEXT);
|
||||
while (walker.nextNode()) {
|
||||
if (walker.currentNode.nodeType === Node.TEXT_NODE) textNodes.push(walker.currentNode as Text);
|
||||
}
|
||||
|
||||
for (const node of textNodes) {
|
||||
const text = node.nodeValue ?? "";
|
||||
if (!text) continue;
|
||||
const lowered = text.toLowerCase();
|
||||
// Some characters change length when lower-cased; fall back to a
|
||||
// case-sensitive scan so the match offsets stay aligned with `text`.
|
||||
const haystack = lowered.length === text.length ? lowered : text;
|
||||
const search = lowered.length === text.length ? needle : query;
|
||||
let index = haystack.indexOf(search);
|
||||
if (index === -1) continue;
|
||||
|
||||
const fragment = pre.ownerDocument.createDocumentFragment();
|
||||
let cursor = 0;
|
||||
while (index !== -1) {
|
||||
if (index > cursor) fragment.appendChild(pre.ownerDocument.createTextNode(text.slice(cursor, index)));
|
||||
const mark = pre.ownerDocument.createElement("mark");
|
||||
mark.className = DDL_SEARCH_MARK_CLASS;
|
||||
mark.textContent = text.slice(index, index + search.length);
|
||||
fragment.appendChild(mark);
|
||||
cursor = index + search.length;
|
||||
index = haystack.indexOf(search, cursor);
|
||||
}
|
||||
if (cursor < text.length) fragment.appendChild(pre.ownerDocument.createTextNode(text.slice(cursor)));
|
||||
node.parentNode?.replaceChild(fragment, node);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user