Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PickTarget.test.ts

121 lines4.0 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { pickElementAtPoint } from "./PickTarget.ts";
4​
5// Models the DOM contract the picker relies on: hit-testing covers the whole page, and each
6// `elementsFromPoint` retargets the hits against the scope it is called on, like Blink does.
7class FakeElement {
8 shadowRoot: FakeScope | null = null;
9 readonly label: string;
10 readonly scope: FakeScope;
11​
12 constructor(label: string, scope: FakeScope) {
13 this.label = label;
14 this.scope = scope;
15 scope.members.add(this);
16 }
18​
19class FakeScope {
20 readonly members = new Set<FakeElement>();
21 readonly host: FakeElement | null;
22 private readonly page: FakePage;
23​
24 constructor(page: FakePage, host: FakeElement | null) {
25 this.page = page;
26 this.host = host;
27 }
28​
29 contains(element: FakeElement): boolean {
30 return this.members.has(element);
31 }
32​
33 elementsFromPoint(): FakeElement[] {
34 const elements: FakeElement[] = [];
35 for (const hit of this.page.hits) {
36 const element = this.retarget(hit);
37 if (elements.at(-1) !== element) elements.push(element);
38 }
39 if (elements.at(-1) !== this.page.html) elements.push(this.page.html);
40 return elements;
41 }
42​
43 private retarget(target: FakeElement): FakeElement {
44 let element = target;
45 while (element.scope.host && !element.scope.encloses(this)) element = element.scope.host;
46 return element;
47 }
48​
49 private encloses(scope: FakeScope): boolean {
50 for (let current: FakeScope | null = scope; current; current = current.host?.scope ?? null) {
51 if (current === this) return true;
52 }
53 return false;
54 }
56​
57class FakePage {
58 hits: FakeElement[] = [];
59 readonly overlays = new Set<FakeElement>();
60 readonly document = new FakeScope(this, null);
61 readonly html = new FakeElement("html", this.document);
62 readonly body = new FakeElement("body", this.document);
63​
64 attachShadow(host: FakeElement, mode: "open" | "closed"): FakeScope {
65 const root = new FakeScope(this, host);
66 if (mode === "open") host.shadowRoot = root;
67 return root;
68 }
69​
70 pick(): FakeElement | null {
71 return pickElementAtPoint(
72 this.document,
73 0,
74 0,
75 (element) => this.overlays.has(element) || element === this.html || element === this.body,
76 );
77 }
79​
80describe("pickElementAtPoint", () => {
81 it("picks the element inside an open shadow root instead of its host", () => {
82 const page = new FakePage();
83 const host = new FakeElement("shadow-demo", page.document);
84 const button = new FakeElement("button", page.attachShadow(host, "open"));
85 page.hits = [button, host, page.body, page.html];
86​
87 expect(page.pick()?.label).toBe("button");
88 });
89​
90 it("falls back to the host when the point misses its shadow content", () => {
91 const page = new FakePage();
92 const host = new FakeElement("shadow-demo", page.document);
93 page.attachShadow(host, "open");
94 page.hits = [host, page.body, page.html];
95​
96 expect(page.pick()?.label).toBe("shadow-demo");
97 });
98​
99 it("keeps a closed shadow root opaque", () => {
100 const page = new FakePage();
101 const outerHost = new FakeElement("outer-host", page.document);
102 const outerRoot = page.attachShadow(outerHost, "open");
103 const closedHost = new FakeElement("closed-host", outerRoot);
104 const secret = new FakeElement("secret", page.attachShadow(closedHost, "closed"));
105 page.hits = [secret, closedHost, outerHost, page.body, page.html];
106​
107 expect(page.pick()?.label).toBe("closed-host");
108 });
109​
110 it("picks the page element beneath the picker overlay, never the overlay", () => {
111 const page = new FakePage();
112 const host = new FakeElement("shadow-demo", page.document);
113 const button = new FakeElement("button", page.attachShadow(host, "open"));
114 const overlay = new FakeElement("overlay", page.document);
115 page.overlays.add(overlay);
116 const toolbar = new FakeElement("toolbar", page.attachShadow(overlay, "closed"));
117 page.hits = [toolbar, button, host, page.body, page.html];
118​
119 expect(page.pick()?.label).toBe("button");
120 });
121});