-
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: HTMLTemplateElement (fixes #18)
- Loading branch information
Showing
10 changed files
with
336 additions
and
77 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
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,86 @@ | ||
import { Node } from "../node.ts"; | ||
import { Element } from "../element.ts"; | ||
import { Document } from "../document.ts"; | ||
import { DocumentFragment } from "../document-fragment.ts"; | ||
import { getElementAttributesString, getInnerHtmlFromNodes } from "../utils.ts"; | ||
import { fragmentNodesFromString } from "../../deserialize.ts"; | ||
import { CTOR_KEY } from "../../constructor-lock.ts"; | ||
|
||
export class HTMLTemplateElement extends Element { | ||
/** | ||
* This blocks access to the .#contents property when the | ||
* super() constructor is running which invokes (our | ||
* overridden) _setParent() method. Without it, we get | ||
* the following error thrown: | ||
* | ||
* TypeError: Cannot read private member #content from | ||
* an object whose class did not declare it | ||
* | ||
* FIXME: Maybe find a cleaner way to do this | ||
*/ | ||
private __contentIsSet = false; | ||
#content: DocumentFragment | null = null; | ||
|
||
constructor( | ||
parentNode: Node | null, | ||
attributes: [string, string][], | ||
key: typeof CTOR_KEY, | ||
content: DocumentFragment, | ||
) { | ||
super( | ||
"TEMPLATE", | ||
parentNode, | ||
attributes, | ||
key, | ||
); | ||
|
||
this.#content = content; | ||
this.__contentIsSet = true; | ||
} | ||
|
||
get content(): DocumentFragment { | ||
return this.#content!; | ||
} | ||
|
||
override _setOwnerDocument(document: Document | null) { | ||
super._setOwnerDocument(document); | ||
|
||
if (this.__contentIsSet) { | ||
this.content._setOwnerDocument(document); | ||
} | ||
} | ||
|
||
get innerHTML(): string { | ||
return getInnerHtmlFromNodes(this.content.childNodes, "template"); | ||
} | ||
|
||
// Replace children in the `.content` | ||
set innerHTML(html: string) { | ||
const content = this.content; | ||
|
||
// Remove all children | ||
for (const child of content.childNodes) { | ||
child._setParent(null); | ||
} | ||
|
||
const mutator = content._getChildNodesMutator(); | ||
mutator.splice(0, content.childNodes.length); | ||
|
||
// Parse HTML into new children | ||
if (html.length) { | ||
const parsed = fragmentNodesFromString(html); | ||
mutator.push(...parsed.childNodes[0].childNodes); | ||
|
||
for (const child of content.childNodes) { | ||
child._setParent(content); | ||
child._setOwnerDocument(content.ownerDocument); | ||
} | ||
} | ||
} | ||
|
||
get outerHTML(): string { | ||
return `<template${ | ||
getElementAttributesString(this.attributes) | ||
}>${this.innerHTML}</template>`; | ||
} | ||
} |
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,19 @@ | ||
import { DOMParser } from "../../deno-dom-wasm.ts"; | ||
import { assertStrictEquals as assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts"; | ||
|
||
// TODO: More comprehensive tests | ||
|
||
Deno.test("Element.outerHTML", () => { | ||
const doc = new DOMParser().parseFromString( | ||
` | ||
<button onclick=false data-foo="bar baz">Hi, <strong qux>there!</strong></button> | ||
`, | ||
"text/html", | ||
)!; | ||
|
||
const button = doc.querySelector("button")!; | ||
assertEquals( | ||
button.outerHTML, | ||
`<button onclick="false" data-foo="bar baz">Hi, <strong qux="">there!</strong></button>`, | ||
); | ||
}); |
Oops, something went wrong.