Skip to content
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

Proposal: getHTML() for Document interface #10280

Open
lukewarlow opened this issue Apr 17, 2024 · 6 comments
Open

Proposal: getHTML() for Document interface #10280

lukewarlow opened this issue Apr 17, 2024 · 6 comments
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest topic: shadow Relates to shadow trees (as defined in DOM)

Comments

@lukewarlow
Copy link
Member

What problem are you trying to solve?

There's currently no way to serialise a document object, that includes shadow roots.

What solutions exist today?

document.documentElement.outerHTML is the closest we have but it won't serialize shadow roots, and it wont serialize the DOCTYPE node either.

How would you solve it?

Define a new document.getHTML() function that would return a string such as <!DOCTYPE html><html><head><title>Title</title></head><body><p>Contents</p></body>

Anything else?

No response

@lukewarlow lukewarlow added addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest labels Apr 17, 2024
@keithamus keithamus added the topic: shadow Relates to shadow trees (as defined in DOM) label Apr 17, 2024
@keithamus
Copy link
Contributor

/cc @mfreed7

@lukewarlow lukewarlow changed the title Proposal: GetHTML() for Document interface Proposal: getHTML() for Document interface Apr 17, 2024
@annevk
Copy link
Member

annevk commented Apr 17, 2024

It seems reasonable, but do we have use cases for this? I think we mainly added Document.parseHTMLUnsafe() because there's a bunch of existing usage of DOMParser for custom sanitizer purposes, but it's not clear they also need to (or do) serialize at a document level.

@lukewarlow
Copy link
Member Author

One case that I can think of is a cobrowsing functionality where the current pages DOM is serialised sent across the wire and reconstructed on the other side. Though in practice those situations might actually use something like a tree walker to serialise to their own format.

@lukewarlow
Copy link
Member Author

It would be a nice HTML equivalent of new XMLSerializer().serializeToString(document) I guess too? Not sure how much that is used.

@mfreed7
Copy link
Collaborator

mfreed7 commented Apr 23, 2024

If there's a use case, this is relatively trivial to implement, so I'm supportive.

@lukewarlow
Copy link
Member Author

lukewarlow commented Apr 23, 2024

@domenic you mentioned you might have a use case for this in jsdom. I know that isn't actually using the platform it's recreating it but the use case might transfer?

Edit: Here's the method https://github.com/jsdom/jsdom?tab=readme-ov-file#serializing-the-document-with-serialize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest topic: shadow Relates to shadow trees (as defined in DOM)
Development

No branches or pull requests

4 participants