-
Notifications
You must be signed in to change notification settings - Fork 42
/
selectors.ts
51 lines (46 loc) · 1.28 KB
/
selectors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { DOM as NWAPI } from "./nwsapi-types.ts";
import { DOM as Sizzle } from "./sizzle-types.ts";
import type { Element } from "../element.ts";
import type { Document } from "../document.ts";
export type Selector = (doc: Document) => {
first(
selector: string,
context: Element | Document,
callback?: (element: Element) => void,
): Element | null;
match(
selector: string,
context: Element | Document,
callback?: (element: Element) => void,
): boolean;
select(
selector: string,
context: Element | Document,
callback?: (element: Element) => void,
): Element[];
};
export type SelectorApi = ReturnType<Selector>;
let codeGenerationAllowed: boolean | null = null;
export function getSelectorEngine(): Selector {
if (codeGenerationAllowed === null) {
try {
new Function("");
codeGenerationAllowed = true;
} catch (e) {
codeGenerationAllowed = false;
}
}
if (codeGenerationAllowed) {
return NWAPI;
} else {
return Sizzle;
}
}
/**
* Explicitly disable querySelector/All code generation with the `Function`
* constructor forcing the Sizzle engine. Enables those APIs on platforms
* like Deno Deploy that don't allow code generation.
*/
export function disableCodeGeneration() {
codeGenerationAllowed = false;
}