Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

shadowrootserializable — serializing a declarative shadow root

The boolean shadowrootserializable attribute marks a declarative shadow root for serialization through getHTML({ serializableShadowRoots: true }). It does not automatically include that root in ordinary getHTML() output or the host's innerHTML.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<template shadowrootmode="open" shadowrootserializable>…</template>

shadowrootserializable is a boolean attribute of the template element. When the parser creates a declarative shadow root from shadowrootmode="open" or shadowrootmode="closed", this attribute's presence sets its serializable flag. shadowrootserializable alone, without a valid shadowrootmode, does not create a shadow root. To disable the option, omit the attribute; shadowrootserializable="false" still counts as present.

Calling host.getHTML({ serializableShadowRoots: true }) includes the marked shadow root as declarative template markup containing its contents. Ordinary host.getHTML() and reading host.innerHTML omit the host's shadow root. The shadowRoots option of getHTML() lets you explicitly select roots for serialization regardless of their serializable flag.

Serialization is not cloning: shadowrootserializable does not replace shadowrootclonable. It is not a data-protection mechanism either. A marked root in closed mode can also appear in serialization of its host, so do not store secrets there assuming that mode prevents their export.

Load the example as an HTML document so that the parser creates the declarative shadow root. Assigning the same markup to innerHTML will not create it. The three checks demonstrate the root's default omission and the presence of its attribute and contents when serialization is enabled.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>shadowrootserializable</title>
<div id="host"><template shadowrootmode="open" shadowrootserializable><span>Hello</span></template></div>
<pre id="result"></pre>
<script>
  const host = document.getElementById('host');
  const html = host.getHTML({ serializableShadowRoots: true });
  document.getElementById('result').textContent = [
    host.getHTML() === '',
    html.includes('shadowrootserializable=""'),
    html.includes('<span>Hello</span>')
  ].join('\n');
</script>
</html>

Result

true
true
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources