Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Page-wide :has() check: catch root, html and body qualified by a class or an id

luishron1236dd2113a0434
diff --git a/src/lib/page-wide-has.test.ts b/src/lib/page-wide-has.test.ts
index 2d261165cf..bd45b384b8 100644
--- a/src/lib/page-wide-has.test.ts
+++ b/src/lib/page-wide-has.test.ts
@@ -1,13 +1,33 @@
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
-import { expect, it } from "vitest";
+import { describe, expect, it } from "vitest";
const css = readFileSync(join(dirname(fileURLToPath(import.meta.url)), "..", "styles.css"), "utf8");
+/** Each `:has(` hung on the root, html or body, whatever classes, ids, attributes or pseudo-classes qualify it. */
+const pageWideHas = (source: string) => source.replace(/\/\*[\s\S]*?\*\//g, "")
+ .match(/(?:^|[\s,{}>+~(])(?::root|html|body)(?:\.[\w-]+|#[\w-]+|\[[^\]]*\]|:(?!has\()[\w-]+(?:\([^)]*\))?)*:has\(/gm) ?? [];
+
// A `:has()` on the root, html or body is checked again on every node added anywhere in the page, and its match
// restyles the whole page: with several replies streaming that is a full restyle many times a second.
it("matches nothing from the root with :has()", () => {
- const rules = css.replace(/\/\*[\s\S]*?\*\//g, "").match(/(?:^|[\s,{}>+~(])(?::root|html|body)(?:\[[^\]]*\]|:[\w-]+(?:\([^)]*\))?)*:has\(/gm) ?? [];
- expect(rules).toEqual([]);
+ expect(pageWideHas(css)).toEqual([]);
+});
+
+describe("the page-wide :has() check", () => {
+ it.each([
+ ":root:has(.streaming) { color: red }",
+ ":root.compact:has([data-chathead-tabs]) { color: red }",
+ "body.compact:has(.streaming) { color: red }",
+ "html#app[data-theme]:not(.x):has(.y) { color: red }",
+ "a, body:has(.y) { color: red }",
+ ])("catches %s", (rule) => expect(pageWideHas(rule)).toHaveLength(1));
+
+ it.each([
+ ".chat:has(.streaming) { color: red }",
+ ":root .chat:has(.streaming) { color: red }",
+ "body .composer.compact:has(textarea) { color: red }",
+ ".tbody:has(.row) { color: red }",
+ ])("lets %s through", (rule) => expect(pageWideHas(rule)).toEqual([]));
});