Skip to content

Build your personal knowledge base with Trilium Notes

chore(client): fail the build on a document-wide ::highlight() rule

Elian Dorane26f7486eb66
diff --git a/apps/client/src/stylesheets/css_invalidation.spec.ts b/apps/client/src/stylesheets/css_invalidation.spec.ts
index 7995a763a1..be84af86d0 100644
--- a/apps/client/src/stylesheets/css_invalidation.spec.ts
+++ b/apps/client/src/stylesheets/css_invalidation.spec.ts
@@ -2,7 +2,7 @@ import { readdirSync, readFileSync, statSync } from "fs";
import { join, relative } from "path";
import { describe, expect, it } from "vitest";
-import { findSlowSelectors } from "../test/css_invalidation";
+import { findDocumentWideHighlights, findSlowSelectors } from "../test/css_invalidation";
const ROOT = join(__dirname, "../../../..");
const SOURCES = [ "apps/client/src", "packages/ckeditor5/src" ];
@@ -70,28 +70,67 @@ describe("findSlowSelectors", () => {
});
});
+describe("findDocumentWideHighlights", () => {
+ it("finds a ::highlight() rule that names no class, id or attribute", () => {
+ const found = (css: string) => findDocumentWideHighlights(css, "test.css");
+
+ expect(found([
+ "::highlight(a)",
+ "*::highlight(b)",
+ ":root ::highlight(c)",
+ "body::highlight(d)"
+ ].map((selector) => `${selector} { color: red }`).join("\n"))).toEqual([
+ "::highlight(a)",
+ "::highlight(b)",
+ ":root ::highlight(c)",
+ "body::highlight(d)"
+ ]);
+ expect(found(".chat { &::highlight(e), & *::highlight(e) { color: red } }")).toEqual([]);
+ expect(found([
+ "[data-highlight-root] *::highlight(f)",
+ "#note::highlight(g)",
+ "::selection"
+ ].map((selector) => `${selector} { color: red }`).join("\n"))).toEqual([]);
+ });
+});
+
describe("Stylesheets", () => {
it("have no selector that restyles a whole container on an insert or a focus change", () => {
const found: string[] = [];
- for (const dir of SOURCES) {
- for (const file of cssFiles(join(ROOT, dir))) {
- const path = relative(ROOT, file).replace(/\\/g, "/");
- if (SEPARATE_PAGES.has(path)) {
- continue;
- }
- const code = readFileSync(file, "utf8");
- for (const { shape, selector } of findSlowSelectors(code, file)) {
- if (!ACCEPTED[path]?.includes(selector)) {
- found.push(`${path} :: ${selector} (${shape})`);
- }
+ for (const { path, code, file } of stylesheets()) {
+ for (const { shape, selector } of findSlowSelectors(code, file)) {
+ if (!ACCEPTED[path]?.includes(selector)) {
+ found.push(`${path} :: ${selector} (${shape})`);
}
}
}
expect(found).toEqual([]);
});
+
+ it("scope every ::highlight() rule to the elements that hold its ranges", () => {
+ const found: string[] = [];
+ for (const { path, code, file } of stylesheets()) {
+ for (const selector of findDocumentWideHighlights(code, file)) {
+ found.push(`${path} :: ${selector}`);
+ }
+ }
+
+ expect(found).toEqual([]);
+ });
});
+function* stylesheets() {
+ for (const dir of SOURCES) {
+ for (const file of cssFiles(join(ROOT, dir))) {
+ const path = relative(ROOT, file).replace(/\\/g, "/");
+ if (!SEPARATE_PAGES.has(path)) {
+ yield { path, file, code: readFileSync(file, "utf8") };
+ }
+ }
+ }
+}
+
function* cssFiles(dir: string): Generator<string> {
for (const name of readdirSync(dir)) {
const path = join(dir, name);
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.css
index da8e7857b1..e7d868983d 100644
--- a/apps/client/src/stylesheets/style.css
+++ b/apps/client/src/stylesheets/style.css
@@ -531,6 +531,24 @@ div.ui-tooltip {
text-decoration: underline;
}
+/* The highlights of `FindInHtml`, in the colors CKEditor gives .ck-find-result and
+ .ck-find-result_selected in an editable text note. */
+[data-highlight-root]::highlight(find-result),
+[data-highlight-root] *::highlight(find-result) {
+ background-color: var(--ck-color-highlight-background);
+ color: var(--ck-color-text);
+}
+
+[data-highlight-root]::highlight(find-result-selected),
+[data-highlight-root] *::highlight(find-result-selected) {
+ background-color: var(--ck-color-find-result-selected);
+}
+
+[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);
+}
+
/* Allow to use <kbd> elements inside the title to define shortcut hints. */
.ui-menu kbd,
button kbd {
diff --git a/apps/client/src/test/css_invalidation.ts b/apps/client/src/test/css_invalidation.ts
index 3f2d03c4cf..67486a7732 100644
--- a/apps/client/src/test/css_invalidation.ts
+++ b/apps/client/src/test/css_invalidation.ts
@@ -19,6 +19,31 @@ export interface SlowSelector {
/** Lists the selectors of the stylesheet `code` that have a {@link SlowShape}. */
export function findSlowSelectors(code: string, filename: string): SlowSelector[] {
+ const found: SlowSelector[] = [];
+ for (const selector of flatSelectors(code, filename)) {
+ const shape = classifySelector(selector);
+ if (shape) {
+ found.push({ shape, selector: serializeSelector(selector) });
+ }
+ }
+ return found;
+}
+
+/**
+ * Lists the `::highlight()` selectors of the stylesheet `code` that name no class, id or
+ * attribute, and so apply to every element of the page. Chromium's renderer crashes on such a
+ * rule at some page zoom levels (#12021).
+ */
+export function findDocumentWideHighlights(code: string, filename: string): string[] {
+ return flatSelectors(code, filename)
+ .filter((selector) => selector.some((part) =>
+ part.type === "pseudo-element" && part.kind === "highlight-function"))
+ .filter((selector) => !splitCompounds(selector).some((compound) => hasKey(compound.parts)))
+ .map(serializeSelector);
+}
+
+/** The selectors of the style rules in `code`, with nesting resolved. */
+function flatSelectors(code: string, filename: string): Selector[] {
// Lowering for an engine without nesting turns each `&` into the full selector.
const flat = transform({
filename,
@@ -26,7 +51,7 @@ export function findSlowSelectors(code: string, filename: string): SlowSelector[
targets: { chrome: 100 << 16 },
errorRecovery: true
}).code;
- const found: SlowSelector[] = [];
+ const selectors: Selector[] = [];
transform({
filename,
code: flat,
@@ -34,17 +59,12 @@ export function findSlowSelectors(code: string, filename: string): SlowSelector[
visitor: {
Rule: {
style(rule) {
- for (const selector of rule.value.selectors) {
- const shape = classifySelector(selector);
- if (shape) {
- found.push({ shape, selector: serializeSelector(selector) });
- }
- }
+ selectors.push(...rule.value.selectors);
}
}
}
});
- return found;
+ return selectors;
}
const NTH = new Set([
@@ -154,7 +174,10 @@ function serializeSelector(selector: Selector): string {
case "type": return part.name;
case "universal": return "*";
case "attribute": return `[${part.name}]`;
- case "pseudo-element": return `::${part.kind}`;
+ case "pseudo-element":
+ return part.kind === "highlight-function"
+ ? `::highlight(${part.name})`
+ : `::${part.kind}`;
case "pseudo-class": {
const selectors = argumentsOf(part);
if (selectors.length) {
diff --git a/apps/client/src/widgets/find.ts b/apps/client/src/widgets/find.ts
index 74e258e942..34ccd01d32 100644
--- a/apps/client/src/widgets/find.ts
+++ b/apps/client/src/widgets/find.ts
@@ -56,24 +56,6 @@ const TPL = /*html*/`
.find-widget-spacer {
flex-grow: 1;
}
-
- /* The highlights of FindInHtml, in the colors CKEditor gives .ck-find-result and
- .ck-find-result_selected in an editable text note. */
- [data-highlight-root]::highlight(find-result),
- [data-highlight-root] *::highlight(find-result) {
- background-color: var(--ck-color-highlight-background);
- color: var(--ck-color-text);
- }
-
- [data-highlight-root]::highlight(find-result-selected),
- [data-highlight-root] *::highlight(find-result-selected) {
- background-color: var(--ck-color-find-result-selected);
- }
-
- [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>
<div class="find-widget-box">