-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use alternative selector engine Sizzle.js to avoid code generat…
- Loading branch information
Showing
13 changed files
with
2,792 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* Symbols for using getElementsByTagName/ClassName on document-fragment | ||
*/ | ||
|
||
export const customByTagNameSym = Symbol(); | ||
export const customByClassNameSym = Symbol(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Sizzle from "./sizzle.js"; | ||
import type { Element } from "../element.ts"; | ||
import type { Document } from "../document.ts"; | ||
|
||
export const DOM: (doc: Document) => { | ||
first( | ||
selector: string, | ||
context: Element | Document, | ||
): Element | null; | ||
match( | ||
selector: string, | ||
context: Element | Document, | ||
): boolean; | ||
select( | ||
selector: string, | ||
context: Element | Document, | ||
): Element[]; | ||
} = Sizzle as any; |
Oops, something went wrong.