-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add queryAll function * allow for either a function or patch object to be passed to both query and queryAll * cleanup
- Loading branch information
Showing
5 changed files
with
284 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { expect } from "chai"; | ||
|
||
import { element } from "./element.js"; | ||
import { queryAll } from "./query-all.js"; | ||
import { html } from "./tags.js"; | ||
|
||
it("should work", () => { | ||
@element({ | ||
tagName: "query-test-1", | ||
shadowDom: [ | ||
html` | ||
<form> | ||
<input id="fname" name="fname" /> | ||
<input id="lname" name="lname" /> | ||
</form> | ||
`, | ||
], | ||
}) | ||
class MyElement extends HTMLElement { | ||
inputs = queryAll("input"); | ||
} | ||
|
||
const el = new MyElement(); | ||
|
||
expect(el.inputs()[0]).to.equal(el.shadowRoot?.querySelector("#fname")); | ||
expect(el.inputs()[1]).to.equal(el.shadowRoot?.querySelector("#lname")); | ||
}); | ||
|
||
it("should patch items when patch is returned", () => { | ||
@element({ | ||
tagName: "query-test-2", | ||
shadowDom: [ | ||
html` | ||
<form> | ||
<input id="fname" name="fname" value="Danny" /> | ||
<input id="lname" name="lname" value="Blue" /> | ||
</form> | ||
`, | ||
], | ||
}) | ||
class MyElement extends HTMLElement { | ||
inputs = queryAll("input"); | ||
} | ||
|
||
const el = new MyElement(); | ||
|
||
el.inputs((node) => { | ||
if (node.id === "fname") { | ||
return { | ||
value: "Foo", | ||
}; | ||
} | ||
|
||
return null; | ||
}); | ||
|
||
expect( | ||
el.shadowRoot?.querySelector<HTMLInputElement>("#fname")?.value, | ||
).to.equal("Foo"); | ||
|
||
expect( | ||
el.shadowRoot?.querySelector<HTMLInputElement>("#lname")?.value, | ||
).to.equal("Blue"); | ||
}); | ||
|
||
it("should patch the selected item when cached", () => { | ||
@element({ | ||
tagName: "query-test-3", | ||
shadowDom: [ | ||
html` | ||
<form> | ||
<input id="fname" name="fname" /> | ||
<input id="lname" name="lname" /> | ||
</form> | ||
`, | ||
], | ||
}) | ||
class MyElement extends HTMLElement { | ||
inputs = queryAll("input"); | ||
} | ||
|
||
const el = new MyElement(); | ||
el.inputs(); | ||
|
||
el.inputs((node) => { | ||
if (node.id === "fname") { | ||
return { | ||
value: "Foo", | ||
}; | ||
} | ||
|
||
return { | ||
value: "Bar", | ||
}; | ||
}); | ||
|
||
expect( | ||
el.shadowRoot?.querySelector<HTMLInputElement>("#fname")?.value, | ||
).to.equal("Foo"); | ||
|
||
expect( | ||
el.shadowRoot?.querySelector<HTMLInputElement>("#lname")?.value, | ||
).to.equal("Bar"); | ||
}); | ||
|
||
it("should apply the same patch to all elements", () => { | ||
@element({ | ||
tagName: "query-test-4", | ||
shadowDom: [ | ||
html` | ||
<form> | ||
<input id="fname" name="fname" /> | ||
<input id="lname" name="lname" /> | ||
</form> | ||
`, | ||
], | ||
}) | ||
class MyElement extends HTMLElement { | ||
inputs = queryAll("input"); | ||
} | ||
|
||
const el = new MyElement(); | ||
el.inputs({ value: "TEST" }); | ||
|
||
expect( | ||
el.shadowRoot?.querySelector<HTMLInputElement>("#fname")?.value, | ||
).to.equal("TEST"); | ||
|
||
expect( | ||
el.shadowRoot?.querySelector<HTMLInputElement>("#lname")?.value, | ||
).to.equal("TEST"); | ||
}); |
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,74 @@ | ||
type Tags = keyof HTMLElementTagNameMap; | ||
type SVGTags = keyof SVGElementTagNameMap; | ||
type MathTags = keyof MathMLElementTagNameMap; | ||
|
||
type NodeUpdate<T extends Node> = Partial<T> | ((node: T) => Partial<T> | null); | ||
|
||
type QueryAllResult<T extends Node> = ( | ||
updates?: NodeUpdate<T>, | ||
) => NodeListOf<T>; | ||
|
||
export function queryAll<K extends Tags>( | ||
selectors: K, | ||
): QueryAllResult<HTMLElementTagNameMap[K]>; | ||
export function queryAll<K extends SVGTags>( | ||
selectors: K, | ||
): QueryAllResult<SVGElementTagNameMap[K]>; | ||
export function queryAll<K extends MathTags>( | ||
selectors: K, | ||
): QueryAllResult<MathMLElementTagNameMap[K]>; | ||
export function queryAll<E extends HTMLElement = HTMLElement>( | ||
selectors: string, | ||
): QueryAllResult<E>; | ||
export function queryAll<K extends Tags>( | ||
query: K, | ||
): QueryAllResult<HTMLElementTagNameMap[K]> { | ||
let res: NodeListOf<HTMLElementTagNameMap[K]> | null = null; | ||
|
||
return function ( | ||
this: HTMLElementTagNameMap[K], | ||
update?: NodeUpdate<HTMLElementTagNameMap[K]>, | ||
) { | ||
if (res) { | ||
return patchNodes(res, update); | ||
} | ||
|
||
if (this.shadowRoot) { | ||
res = this.shadowRoot.querySelectorAll<K>(query); | ||
} else { | ||
res = this.querySelectorAll<K>(query); | ||
} | ||
|
||
if (!res) { | ||
throw new Error(`could not find ${query}`); | ||
} | ||
|
||
return patchNodes(res, update); | ||
}; | ||
} | ||
|
||
function patchNodes<T extends HTMLElement>( | ||
target: NodeListOf<T>, | ||
update?: NodeUpdate<T>, | ||
): NodeListOf<T> { | ||
if (!update) { | ||
return target; | ||
} | ||
|
||
for (const node of target) { | ||
const patch = typeof update === "function" ? update(node) : update; | ||
|
||
if (patch) { | ||
for (const update in patch) { | ||
const newValue = patch[update]; | ||
const oldValue = node[update]; | ||
|
||
if (newValue && newValue !== oldValue) { | ||
node[update] = newValue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return target; | ||
} |
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