Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(search): scope the match highlight rules to their roots (regression of #12021)
Elian Doran5aba71e37b51
diff --git a/apps/client/src/services/highlight_matches.ts b/apps/client/src/services/highlight_matches.tsindex 1d6d756a4e..94cf2d5007 100644--- a/apps/client/src/services/highlight_matches.ts+++ b/apps/client/src/services/highlight_matches.ts@@ -13,6 +13,13 @@ const PRIORITIES = {export type HighlightName = keyof typeof PRIORITIES;+/**+ * Marks the elements that ranges are found in. The `::highlight()` rules apply only under it,+ * because a document-wide `::highlight()` rule crashes Chromium's renderer at some page zoom+ * levels (#12021).+ */+export const HIGHLIGHT_ROOT_ATTRIBUTE = "data-highlight-root";+export interface MatchOptions {/** Selectors of the elements whose own text is not searched. */exclude?: string[];@@ -116,6 +123,7 @@ function findRanges(root: HTMLElement, options: MatchOptions, getOffsets: (text:const excluded = [ ...ALWAYS_EXCLUDED, ...options.exclude ?? [] ].join(",");const limit = options.limit ?? Infinity;const ranges: Range[] = [];+ root.setAttribute(HIGHLIGHT_ROOT_ATTRIBUTE, "");for (const node of getTextNodes(root, excluded)) {for (const [ start, end ] of getOffsets(node.data).slice(0, limit - ranges.length)) {diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.cssindex db4d4327f1..da8e7857b1 100644--- a/apps/client/src/stylesheets/style.css+++ b/apps/client/src/stylesheets/style.css@@ -525,7 +525,8 @@ div.ui-tooltip {}/* The search terms `useImperativeSearchHighlighlighting()` finds in titles, cards and previews. */-::highlight(search-match) {+[data-highlight-root]::highlight(search-match),+[data-highlight-root] *::highlight(search-match) {color: var(--note-list-view-search-result-highlight-color, var(--admonition-warning-accent-color));text-decoration: underline;}diff --git a/apps/client/src/test/highlights.ts b/apps/client/src/test/highlights.tsindex 447b86dfce..3f4229611a 100644--- a/apps/client/src/test/highlights.ts+++ b/apps/client/src/test/highlights.ts@@ -17,10 +17,17 @@ export function stubHighlightApi() {Object.defineProperty(Object.getPrototypeOf(globalThis.CSS), "highlights", { value: new Map() });}-/** The ranges of the highlight `name` that start under `root`, in the order they were added. */+/**+ * The ranges of the highlight `name` that start under `root`, in the order they were added. Only+ * ranges inside an element carrying `HIGHLIGHT_ROOT_ATTRIBUTE` count, since the `::highlight()`+ * rules draw no others. The attribute name is repeated because importing `highlight_matches.ts`+ * here loads `utils.ts` ahead of the specs' mocks.+ */export function highlightedRanges(name: string, root: Node = document.body) {return [ ...CSS.highlights.get(name) ?? [] ]- .filter((range): range is Range => range instanceof Range && root.contains(range.startContainer));+ .filter((range): range is Range => range instanceof Range+ && root.contains(range.startContainer)+ && !!range.startContainer.parentElement?.closest("[data-highlight-root]"));}/** The text of {@link highlightedRanges}. */diff --git a/apps/client/src/widgets/find.ts b/apps/client/src/widgets/find.tsindex cf79643bb7..74e258e942 100644--- a/apps/client/src/widgets/find.ts+++ b/apps/client/src/widgets/find.ts@@ -59,16 +59,19 @@ const TPL = /*html*/`/* The highlights of FindInHtml, in the colors CKEditor gives .ck-find-result and.ck-find-result_selected in an editable text note. */- ::highlight(find-result) {+ [data-highlight-root]::highlight(find-result),+ [data-highlight-root] *::highlight(find-result) {background-color: var(--ck-color-highlight-background);color: var(--ck-color-text);}- ::highlight(find-result-selected) {+ [data-highlight-root]::highlight(find-result-selected),+ [data-highlight-root] *::highlight(find-result-selected) {background-color: var(--ck-color-find-result-selected);}- ::highlight(find-result-secondary) {+ [data-highlight-root]::highlight(find-result-secondary),+ [data-highlight-root] *::highlight(find-result-secondary) {background-color: color-mix(in srgb, var(--ck-color-find-result-background, #66ff99) 40%, transparent);}</style>