Repository navigation
Add innerHTML and outerHTML getters for focus navigation fixtures #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cowboyd
wants to merge
1
commit into
main
Choose a base branch
from
dom-html-serialization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+126
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,103 @@ | ||
| /* oxlint-disable bombshell-dev/exported-function-async -- DOM serialization is synchronous. */ | ||
|
|
||
| import { html, serialize as serializeHTML, serializeOuter as serializeOuterHTML } from 'parse5'; | ||
| import type { TreeAdapter, TreeAdapterTypeMap } from 'parse5'; | ||
| import { Comment, type Document, DocumentType, Element, Node, Text, XMLDocument } from './dom.ts'; | ||
|
|
||
| /** Serialize an element's children as HTML. */ | ||
| export function serialize(element: Element): string { | ||
| assertHTMLDocument(element); | ||
| return serializeHTML(element, { treeAdapter }); | ||
| } | ||
|
|
||
| /** Serialize an element and its children as HTML. */ | ||
| export function serializeOuter(element: Element): string { | ||
| assertHTMLDocument(element); | ||
| return serializeOuterHTML(element, { treeAdapter }); | ||
| } | ||
|
|
||
| function assertHTMLDocument(element: Element): void { | ||
| if (element.ownerDocument instanceof XMLDocument) { | ||
| throw new DOMException('XML serialization is not supported', 'NotSupportedError'); | ||
| } | ||
| } | ||
|
|
||
| function unsupported(): never { | ||
| throw new DOMException('This tree adapter only supports serialization', 'NotSupportedError'); | ||
| } | ||
|
|
||
| type DOMTree = TreeAdapterTypeMap< | ||
| Node, | ||
| Node, | ||
| Node, | ||
| Document, | ||
| Node, | ||
| Element, | ||
| Comment, | ||
| Text, | ||
| Element, | ||
| DocumentType | ||
| >; | ||
|
|
||
| const treeAdapter: TreeAdapter<DOMTree> = { | ||
| getAttrList(element) { | ||
| return Array.from(element.attributes, (attribute) => ({ | ||
| name: attribute.localName, | ||
| value: attribute.value, | ||
| namespace: attribute.namespaceURI ?? undefined, | ||
| prefix: attribute.prefix ?? undefined, | ||
| })); | ||
| }, | ||
| getChildNodes(node) { | ||
| if (node.childNodes.some((child) => child.nodeType === Node.PROCESSING_INSTRUCTION_NODE)) { | ||
| throw new DOMException( | ||
| 'Processing instruction serialization is not supported', | ||
| 'NotSupportedError', | ||
| ); | ||
| } | ||
| return Array.from(node.childNodes); | ||
| }, | ||
| getCommentNodeContent: (node) => node.data, | ||
| getDocumentMode: unsupported, | ||
| getDocumentTypeNodeName: (node) => node.name, | ||
| getDocumentTypeNodePublicId: (node) => node.publicId, | ||
| getDocumentTypeNodeSystemId: (node) => node.systemId, | ||
| getFirstChild: (node) => node.firstChild, | ||
| getNamespaceURI: (element) => element.namespaceURI as html.NS, | ||
| getNodeSourceCodeLocation: () => undefined, | ||
| getParentNode: (node) => node.parentNode, | ||
| getTagName(element) { | ||
| if ( | ||
| element.namespaceURI === html.NS.HTML || | ||
| element.namespaceURI === html.NS.SVG || | ||
| element.namespaceURI === html.NS.MATHML | ||
| ) { | ||
| return element.localName; | ||
| } | ||
| return element.prefix === null ? element.localName : `${element.prefix}:${element.localName}`; | ||
| }, | ||
| getTextNodeContent: (node) => node.data, | ||
| getTemplateContent() { | ||
| throw new DOMException('Template contents are not supported', 'NotSupportedError'); | ||
| }, | ||
| isCommentNode: (node): node is Comment => node instanceof Comment, | ||
| isDocumentTypeNode: (node): node is DocumentType => node instanceof DocumentType, | ||
| isElementNode: (node): node is Element => node instanceof Element, | ||
| isTextNode: (node): node is Text => node instanceof Text, | ||
| adoptAttributes: unsupported, | ||
| appendChild: unsupported, | ||
| createCommentNode: unsupported, | ||
| createTextNode: unsupported, | ||
| createDocument: unsupported, | ||
| createDocumentFragment: unsupported, | ||
| createElement: unsupported, | ||
| detachNode: unsupported, | ||
| insertBefore: unsupported, | ||
| insertText: unsupported, | ||
| insertTextBefore: unsupported, | ||
| setDocumentMode: unsupported, | ||
| setDocumentType: unsupported, | ||
| setNodeSourceCodeLocation: unsupported, | ||
| updateNodeSourceCodeLocation: unsupported, | ||
| setTemplateContent: unsupported, | ||
| }; |
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with only a getter,
el.innerHTML = ...from strict code now throws a genericTypeError, where before it silently set an own prop. vue, solid and preact'sdangerouslySetInnerHTMLall assign it. is the setter coming soon, or worth a setter that throws a clearerNotSupportedErroruntil then?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we will need to have them eventually. We are actually already using
parse5in the WPT to parse thehtmlfiles and build our DOM, so it isn't even that far off.However, I know that @natemoo-re is also cooking something with html parsing as well, but we could wire it in pretty easily.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference #137