Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

shadowrootclonable — cloning a declarative shadow root

The boolean shadowrootclonable attribute on a template element marks a declarative shadow root as clonable. Copying the host element can then include its shadow tree as well.

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

Syntax

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

shadowrootclonable is a boolean attribute of template, used with shadowrootmode. When creating a declarative shadow root, the parser sets its clonable flag according to the attribute's presence. Without it, the flag is false; the attribute alone does not create a shadow root.

When the root is clonable, host.cloneNode(true) also copies its shadow tree. This is not ordinary template-content copying with template.content.cloneNode(true): it copies the host with its already-created shadow root.

Presence enables the option: shadowrootclonable="false" still enables it and is not a valid way to disable a boolean attribute. To disable this option in the declaration, omit the attribute. shadowrootclonable does not replace shadowrootserializable: node cloning and HTML serialization are separate mechanisms.

The example requires a browser supporting declarative shadow roots and this attribute. Open the complete HTML document rather than inserting the markup through innerHTML. The open mode allows inspection through copy.shadowRoot; the copied host does not need to be attached to the document.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Shadow root cloning</title>
<div id="host"><template shadowrootmode="open" shadowrootclonable><span>Hello</span></template></div>
<pre id="result"></pre>
<script>
  const host = document.getElementById('host');
  const copy = host.cloneNode(true);
  document.getElementById('result').textContent =
    copy.shadowRoot?.textContent ?? 'No shadow root';
</script>
</html>

Result

Hello

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