The template element is used to declare fragments of HTML that can be cloned and inserted in the document by script. The "contents of the template" (as accessed via HTMLTemplateElement.prototype.content) are stored in a DocumentFragment associated with a different Document other than the main document.
So, because templ.content.ownerDocument != document, this is OK:
<template id="templ">
<script>console.error('This does not execute!');</script>
</template>
templ.content is an inert DocumentFragment, that's fine.
Edit: The rest of the question, below, has an incorrect premise -- I was testing this by creating a template in JavaScript with document.createElement instead of via HTML, which yields different results. More precisely, when creating a template and using document.body.appendChild, the template is adopted into document which specifically adopts template.content into document.
However, the
templateitself, and its descendants, are associated withdocument:templ.children[0].tagName == 'script' templ.children[0].ownerDocument == documentIn other words, the
scriptis still a part of the mainDocument, which would normally mean it executes.While the spec explains the
DocumentFragmentaccessed via.content, that doesn't account for this concrete, instantiated<script>instance having its associatedDocumentbe the maindocumentbut not executing.Does the spec explain somewhere exactly why this
<script>isn't executed? How is it excluded from execution if it does belong todocument?
(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.)