HTML / Element / Initial coverage

<template>要素

状態: 初期Coverage 対象: HTML Living Standard 仕様確認日: 2026-09-19

Technical Summary

template要素は、scriptでcloneしてDocumentへ挿入できるHTML fragmentを宣言します。template要素自身はrendering上何も表現せず、内容は要素の通常のchildではなく、関連付けられたTemplate ContentsであるDocumentFragmentに保持されます。

template.contentからdeep cloneしたfragmentを挿入すると、その子孫が通常のDocument treeに入ります。shadowrootmodeを使う場合は、Declarative Shadow DOMのparser・shadow tree・slot・focus・Accessibilityを別の境界として確認します。

Definition / Categories

項目定義
意味scriptでcloneしてDocumentへ挿入するHTML fragmentの宣言
CategoriesMetadata content、Flow content、Phrasing content、Script-supporting element
ContextMetadata、Phrasing、Script-supporting contentが期待される場所。span属性のないcolgroupの子という特殊なcontextもある
Content modelNothing。markupとして書かれた内容はTemplate Contentsに入る
Tag omission開始タグ・終了タグとも省略不可
Content attributesグローバル属性、forshadowrootmodeshadowrootdelegatesfocusshadowrootserializableshadowrootslotassignmentshadowrootclonableshadowrootcustomelementregistry
DOM interfaceHTMLTemplateElementcontentはreadonlyのDocumentFragment

Template ContentsとDOM tree

HTML parserでtemplateの内容を読んだ場合、その内容はtemplate要素の通常のchildとしては扱われません。template.contentが返すTemplate Contentsへ入ります。

<template id="card-template">
  <article>
    <h2>Card</h2>
  </article>
</template>

const template = document.querySelector('#card-template');
template.childNodes.length;         // 0
template.content.childNodes.length; // 1
template.content.firstElementChild.nodeName; // "ARTICLE"

これは単なるCSSの非表示とは異なります。template contentsには、template要素の通常のdescendantとは異なるowner documentとinsertionの処理が関係します。parser、DOM manipulation、adopting、custom elementsを一緒に確認する必要があります。

Clone・Insert・IDの境界

contentはDocumentFragmentです。cloneNode(true)で複製し、複製側の参照を更新してからDocumentへ挿入します。fragmentを複数回使うときは、固定IDの重複、label・for、ARIA参照、form controlのnameと値を個別に設計します。

const template = document.querySelector('#card-template');
const fragment = template.content.cloneNode(true);
const heading = fragment.querySelector('h2');
heading.textContent = 'A cloned card';
document.querySelector('#cards').append(fragment);

同じnodeをそのまま別の場所へ何度も追加するのではなく、必要な回数だけcloneします。外部データをHTMLとして解釈する必要がない場合は、textContentなどで値を設定し、template自身をsanitizerとは扱いません。

表示・実体化・Accessibility

template要素はrendering上何も表現しません。template contentsも、通常のDocument treeへ挿入されるまでは、ページの利用者が操作するライブな内容として扱いません。実体化後は、挿入された見出し、label、button、form control、ARIA関係が通常のsemanticsとAccessibility mappingの評価対象になります。

そのため、templateの中に意味のある内容を書くだけでは、利用者へ届くことを保証できません。JavaScriptが失敗したときのfallback、動的挿入後のfocus、name・role・accessible name、status通知を別に確認します。

Declarative Shadow DOM

shadowrootmodeは、templateをDeclarative Shadow Rootとして扱うための属性です。openclosedでshadow rootの公開状態が変わり、shadowrootdelegatesfocus、slot assignment、serializable、clonableなどの追加条件が関係します。

<my-card>
  <template shadowrootmode="open">
    <style>:host { display: block; }</style>
    <slot></slot>
  </template>
  <span>Light DOM content</span>
</my-card>

Declarative Shadow DOMでは、parserがshadow treeを作る条件、template contents、slotのassigned nodes、hostとshadow treeのAccessibility境界を分けて扱います。通常のclone用途の結果を、Declarative Shadow DOMの実装結果へそのまま一般化しません。

forとstreaming用途

HTML Standardはtemplateのfor属性を、processing instructionで示した既存の位置へ内容を挿入・置換する仕組みとして定義しています。通常のクライアント側template利用とは別のprocessing modelであり、実装時期と対応範囲を確認してから扱います。

Accessibility

template自身がrendering上何も表現しないことと、template contentsが実体化後にどのようにAccessibility treeへ入るかを分けます。templateは、実体化された見出しやcontrolのaccessible nameを自動的に作る要素ではありません。

段階確認する対象注意点
実体化前template要素とtemplate contents表示されるライブコンテンツや操作対象として扱わない
通常の挿入後見出し、label、button、form control、live region挿入されたDOMのsemanticsとfocusを観測する
Shadow DOMhost、shadow tree、slot、delegates focusbrowser・platform・支援技術ごとのmappingを分ける

Fact / Evidence(主張 / 根拠)

template elementの定義、Template Contents、DOM interface、Declarative Shadow DOMを、仕様上の主張と実装観測に分離して記録します。

主要Factと根拠位置
種別Fact / 主張条件・範囲状態根拠
SPECtemplateはscriptでcloneしてDocumentへ挿入するHTML fragmentを宣言し、rendering上何も表現しない。element definition、rendering、script insertion確認済みHTML Standard: the template element
SPECTemplate Contentsはtemplate要素自身の通常のchildではなく、関連付けられたDocumentFragmentである。content、template contents owner document、parser確認済みHTML Standard: template contents
SPECHTMLTemplateElement.contentはtemplate contentsのDocumentFragmentを返す。readonly IDL attribute、clone、insert確認済みHTML Standard: HTMLTemplateElement
SPECshadowrootmodeはDeclarative Shadow Rootのopen / closed stateを指定する。shadow tree、slot assignment、focus delegation、serialization確認済みHTML Standard: shadowrootmode
SPECtemplateのcontent modelはNothingであり、tag omissionはない。authoring conformanceとparserによるTemplate Contentsの生成確認済みHTML Standard: content model

Evidence

  1. HTML Standard: the template element — definition、categories、content attributes、HTMLTemplateElement、rendering
  2. DOM Standard: DocumentFragment — template contentsを受け取るfragment interface
  3. HTML Accessibility API Mappings — template自身と実体化後の要素をmapping上分けて確認する入口
  4. Web Platform Tests: the-template-element — template parsing、content、DOM APIに関係するテスト群の入口
  5. Web Platform Tests: declarative Shadow DOMshadowrootmodeとshadow treeのテスト群の入口

Implementation Evidence

仕様上のprocessing modelと、ブラウザーで観測するTemplate Contents、clone、parser、Declarative Shadow DOM、Accessibility Treeを分けて登録します。専用fixtureをまだ実行していないため、未実施の項目を確認済みとは扱いません。

専用fixture候補: template-v1で、childNodesとcontentの差、deep clone、fragment insertion、重複ID、shadowrootmode、実体化前後のAccessibility Treeを再現する。

template要素のImplementation Evidence登録表
種別再現確認の範囲記録する条件状態
IMPLTemplate Contents、content、deep clone、fragment insertion、adopting、parser、ID参照、Declarative Shadow DOMfixture ID template-v1を作成し、ブラウザー名・バージョン、OS、確認日、各結果を記録する未実施
WPTtemplate parsing、content、DOM API、Declarative Shadow DOMに対応するテスト選定ファイルを実行し、browser・実行日・pass/fail・未実行理由を登録する未実施
AAM実体化前のtemplate、通常の挿入後、host・shadow tree・slot・focusのAccessibility mappingbrowser・OS・AX tree・支援技術・実体化状態を分けて記録する未実施

Coverage / Open Issues

  • 確認済み意味、categories、context、content model、tag omission、content attributes
  • 確認済みTemplate Contents、DocumentFragmentcontent、clone・insertの仕様上の入口
  • 確認済みshadowrootmode、slot assignment、focus delegation、serializationに関する仕様上の入口
  • 未完了Chrome・Firefox・Safariのparser、custom elements、adopting、動的変更、重複IDとform・ARIA参照の比較
  • 未完了Declarative Shadow DOM、slot、closed root、focus、serialization、custom element registryの実装差
  • 未完了WPTの個別実行結果、HTML-AAMのplatform mapping、支援技術、互換性、Expert-gap Review

このページは初期Coverageです。仕様上の入口を整理したものであり、すべてのブラウザー、DOM操作、custom element、Shadow DOM構成、支援技術で同じ結果になることを主張しません。

Related surface

初心者向けのひな形、content、cloneの使い方はYugienのtemplate要素ページを参照してください。scriptによるDOM操作はscript要素、slotとshadow treeはDeclarative Shadow DOMの実装確認で併せて扱います。