<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://www.w3.org/Bugs/Public/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4"
          urlbase="https://www.w3.org/Bugs/Public/"
          
          maintainer="sysbot+bugzilla@w3.org"
>

    <bug>
          <bug_id>23990</bug_id>
          
          <creation_ts>2013-12-03 20:52:28 +0000</creation_ts>
          <short_desc>HTMLTemplateElement.content.ownerDocument should be exclusive to templates</short_desc>
          <delta_ts>2014-02-11 23:56:17 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WHATWG</product>
          <component>HTML</component>
          <version>unspecified</version>
          <rep_platform>PC</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>Unsorted</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Adam Klein">adamk</reporter>
          <assigned_to name="Ian &apos;Hixie&apos; Hickson">ian</assigned_to>
          <cc>dglazkov</cc>
    
    <cc>dominicc</cc>
    
    <cc>ian</cc>
    
    <cc>mike</cc>
    
    <cc>rafaelw</cc>
    
    <cc>rniwa</cc>
    
    <cc>wchen</cc>
          
          <qa_contact>contributor</qa_contact>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>97073</commentid>
    <comment_count>0</comment_count>
    <who name="Adam Klein">adamk</who>
    <bug_when>2013-12-03 20:52:28 +0000</bug_when>
    <thetext>This bug refers to the following passage: http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#appropriate-template-contents-owner-document

Per the spec, and as implemented in WebKit, Blink, and Gecko, the &quot;appropriate template contents owner document&quot; is the template element&apos;s own ownerDocument in any case where that document does not have a browsing context. This allows sub-templates to share an ownerDocument with their enclosing template, but happens to apply in other cases, such as when a template is created from a Document created via document.implementation.createHTMLDocument(). Given that the reason for using a separate document was mostly to avoid loading resources in templates, that was fine.

But in the Custom Elements spec, there is now a difference between Documents created via createHTMLDocument and those in &lt;templates&gt;: the former share the custom element registry with the Document they&apos;re created from, while &lt;template&gt; documents do not share the registry, thus making custom elements inside templates &quot;inert&quot;. The relevant part of Custom Elements is http://w3c.github.io/webcomponents/spec/custom/#creating-and-passing-registries. See https://www.w3.org/Bugs/Public/show_bug.cgi?id=23839#c5 for more details on the interaction between that part of the spec and &lt;template&gt;.

All of the above means that a custom element in a template could have different behavior depending on whether that template&apos;s ownerDocument was created via createHTMLDocument (in which case custom elements inside will, against the wishes of the Custom Elements spec, &quot;come to life&quot;).

The easiest fix to the HTML spec would be to change step 1 of the linked algorithm above to branch on whether |doc| is itself a template contents owner document (which would be some new property of the document).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>97076</commentid>
    <comment_count>1</comment_count>
    <who name="Adam Klein">adamk</who>
    <bug_when>2013-12-03 21:05:42 +0000</bug_when>
    <thetext>Some code:

document.register(&apos;my-element&apos;, {prototype: {__proto__: HTMLElement.prototype, createdCallback: function() { console.log(&apos;my-element created&apos;); } } });

// logs, as it should
document.createElement(&apos;my-element&apos;);

 // does not log, as it shouldn&apos;t
document.createElement(&apos;template&apos;).content.ownerDocument.createElement(&apos;my-element&apos;);

// logs, as it should
document.implementation.createHTMLDocument(&apos;&apos;).createElement(&apos;my-element&apos;);

// ERROR! logs, but shouldn&apos;t
document.implementation.createHTMLDocument(&apos;&apos;).createElement(&apos;template&apos;).content.ownerDocument.createElement(&apos;my-element&apos;);</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>97193</commentid>
    <comment_count>2</comment_count>
    <who name="Ian &apos;Hixie&apos; Hickson">ian</who>
    <bug_when>2013-12-05 18:23:01 +0000</bug_when>
    <thetext>Why do we want elements in otherwise-inert createDocument-style documents to have bindings applied, but not have elements in template contents have bindings applied?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>97199</commentid>
    <comment_count>3</comment_count>
    <who name="Rafael Weinstein">rafaelw</who>
    <bug_when>2013-12-05 18:41:48 +0000</bug_when>
    <thetext>I pushed pretty hard on the same issue. My understanding is two-fold.

The practical answer is very tactical:

A worry that if custom elements boot-strap inside of template, then the naive implementation of custom elements may be extremely wasteful. The guard against this is to check whether the element has a defaultView, but it&apos;s a fairly esoteric concept and even invoking the callbacks has cost.

The theoretical answer which ultimately made me comfortable is:

We *will* (at some point) allow custom elements to boot up inside of templates (converging on equivalent expressiveness for built-in vs custom elements) However, we can wait to do that until we have clear &amp; compelling use cases. (e.g. add an optional argument to document.register which requests being booted up inside of templates).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>97548</commentid>
    <comment_count>4</comment_count>
    <who name="Ian &apos;Hixie&apos; Hickson">ian</who>
    <bug_when>2013-12-12 23:18:25 +0000</bug_when>
    <thetext>So isn&apos;t the fix for this as simple as changing the &quot;appropriate template contents owner document&quot; algorithm to just not check if there&apos;s a browsing context and simply always use that code path?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>97550</commentid>
    <comment_count>5</comment_count>
    <who name="Adam Klein">adamk</who>
    <bug_when>2013-12-12 23:23:30 +0000</bug_when>
    <thetext>(In reply to Ian &apos;Hixie&apos; Hickson from comment #4)
&gt; So isn&apos;t the fix for this as simple as changing the &quot;appropriate template
&gt; contents owner document&quot; algorithm to just not check if there&apos;s a browsing
&gt; context and simply always use that code path?

The trouble with that approach is that:

&lt;template&gt;
  &lt;template&gt;
    &lt;template&gt;

results in 3 documents. Given that one of the wins of &lt;template&gt; over &lt;script type=&quot;text/html&quot;&gt; is nestability, that seems unfortunate. But maybe this is acceptable? I haven&apos;t thought hard about the consequences of having lots of documents in cases like this in a long time.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>97554</commentid>
    <comment_count>6</comment_count>
    <who name="">contributor</who>
    <bug_when>2013-12-12 23:47:42 +0000</bug_when>
    <thetext>Checked in as WHATWG revision r8351.
Check-in comment: Make the template contents of &lt;template&gt; elements in docs created by createDocument() not have live bindings.
http://html5.org/tools/web-apps-tracker?from=8350&amp;to=8351</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>100341</commentid>
    <comment_count>7</comment_count>
    <who name="Adam Klein">adamk</who>
    <bug_when>2014-02-11 23:56:17 +0000</bug_when>
    <thetext>For the record, I&apos;ve uploaded a pull request that updates the html-templates tests to match the now-specced behavior:

https://github.com/w3c/web-platform-tests/pull/624</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>