8000 Add `getInnerHTML()` that optionally serializes declarative Shadow DOM · Issue #8867 · whatwg/html · GitHub
[go: up one dir, main page]

Skip to content

Add getInnerHTML() that optionally serializes declarative Shadow DOM #8867

@mfreed7

Description

@mfreed7

This issue is split out from the discussion that starts roughly here #5465 (comment). The original Declarative Shadow DOM spec PR included this functionality, which allowed a DOM sub-tree to be serialized in a way that doesn't throw away all of the contained shadow roots. To facilitate landing that PR, this functionality was removed. I'd like to use this issue to resume that discussion.

The problem statement is that DOM content containing developer shadow roots (i.e. not UA shadow roots) cannot be serialized. Doing so today requires a manual tree walk in JS land, manually grabbing shadow roots and serializing them into <template shadowrootmode> elements.

The ideal solution would be for const html = element.innerHTML; to just start serializing shadow roots like it already serializes the rest of the DOM into HTML. However, this is almost surely not web compatible. One option is to provide an attribute on Document and/or ShadowRoot that opts-in to this serialization behavior. However, given the recent push to define new parser entrypoints that are functions instead of attributes, it seems to make more sense to also define a new functional getter that optionally (or always?) serializes shadow roots:

const html = element.getInnerHTML({includeShadowRoots: true});
// html contains `<template shadowrootmode=...>` for any contained "open" shadow roots.

To preserve the encapsulation of "closed" shadow roots, another option would be needed in the case that "closed" shadow roots want to be serialized:

const html = element.getInnerHTML({
  includeShadowRoots: true,
  closedRoots: [shadowRoot1, shadowRoot2, ...]
});

Thoughts? Concerns?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0