HTML 规范在哪里声明 <template> 的内容是惰性的?

Where does the HTML spec state that a <template>'s contents are inert?

The template element is used to declare fragments of HTML that can be cloned and inserted in the document by script. "contents of the template"(通过 HTMLTemplateElement.prototype.content 访问)存储在 DocumentFragment 中,与主 [= 以外的不同 Document 关联19=].

所以,因为 templ.content.ownerDocument != document,这是可以的:

<template id="templ">
  <script>console.error('This does not execute!');</script>
</template>

templ.content是一个惰性的DocumentFragment,没关系。


编辑:下面问题的其余部分有一个不正确的前提——我通过在 JavaScript 中使用 document.createElement 而不是通过 HTML,这会产生不同的结果。 更准确地说,当创建模板并使用 document.body.appendChild 时,模板被采用到 document 中,其中 specifically adopts template.content into document

However, the template itself, and its descendants, are associated with document:

templ.children[0].tagName == 'script'
templ.children[0].ownerDocument == document

In other words, the script is still a part of the main Document, which would normally mean it executes.

While the spec explains the DocumentFragment accessed via .content, that doesn't account for this concrete, instantiated <script> instance having its associated Document be the main document but not executing.

Does the spec explain somewhere exactly why this <script> isn't executed? How is it excluded from execution if it does belong to document?


(I realize it's useful for the script not to execute. This is a question about the spec, not about the usefulness of <template> or whether the in-browser behavior is appropriate.)

嗯,在 template 元素描述中有 this note

Note: Templates provide a method for declaring inert DOM subtrees and manipulating them to instantiate document fragments with identical contents.

(我的重点。) 然后在以下段落中详细介绍 (我的重点)

A Document doc's appropriate template contents owner document is the Document returned by the following algorithm:

  1. If doc is not a Document created by this algorithm, run these substeps:

    1. If doc does not yet have an associated inert template document then run these substeps:

      1. Let new doc be a new Document (that does not have a browsing context). This is "a Document created by this algorithm" for the purposes of the step above.

      2. If doc is an HTML document, mark new doc as an HTML document also.

      3. Let doc's associated inert template document be new doc.

    2. Set doc to doc's associated inert template document.

  2. Return doc