Closed Bug 1288080 Opened 8 years ago Closed 8 years ago

Scripts imported from templates in other documents that got there via an innerHTML setter do not run

Categories

(Core :: DOM: Core & HTML, defect)

47 Branch
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: matthew, Unassigned, NeedInfo)

Details

Attachments

(3 files)

629 bytes, application/zip
Details
22 bytes, application/x-javascript
Details
605 bytes, text/html
Details
Attached file example.zip
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36

Steps to reproduce:

1. Created a new document using document.implementation.createHTMLDocument
2. Created a template inside that document with a script.
3. Imported that template into the main document using document.importNode
4. Gave the script a src and inserted it into the main document.


Actual results:

Nothing.


Expected results:

The script should have executed and the script's onload event should have fired.
Attached file foo.js
Attached file example.html
Component: Untriaged → DOM: Core & HTML
Product: Firefox → Core
Boris, is this a bug in Gecko?
Flags: needinfo?(bzbarsky)
Hmm.  So the relevant spec here is starting at https://w3c.github.io/DOM-Parsing/#dom-element-innerhtml which calls into https://w3c.github.io/DOM-Parsing/#dfn-fragment-parsing-algorithm which calls into https://html.spec.whatwg.org/multipage/syntax.html#html-fragment-parsing-algorithm which starts running the parser.  We see the <template> tag, enter https://html.spec.whatwg.org/multipage/syntax.html#parsing-main-intemplate then see the <script> tag and process at as if in the "in head" insertion mode, per spec.  That lands us in https://html.spec.whatwg.org/multipage/syntax.html#parsing-main-inhead where we see the <script> start tag token and run the relevant steps.  Step 4 is:

  If the parser was originally created for the HTML fragment parsing algorithm,
  then mark the script element as "already started". (fragment case)

So after this point this script will never execute.  So as far as I can tell, we're doing exactly what the spec says here.  Anne, am I missing something?
Flags: needinfo?(bzbarsky) → needinfo?(annevk)
Summary: Scripts imported from templates in other documents do not load → Scripts imported from templates in other documents that got there via an innerHTML setter do not run
Yeah, this looks like a bug in Chrome. No other browser does this.
Flags: needinfo?(annevk)
Marking invalid.  I went to file a Chromium issue on this, then discovered one has been filed since late 2015 and there is a patch under review as of a week or two ago.  See https://bugs.chromium.org/p/chromium/issues/detail?id=572693
Status: UNCONFIRMED → RESOLVED
Closed: 8 years ago
Resolution: --- → INVALID
Thanks for the explanation. This doesn't seem intuitive though, this means you can't have scripts within templates? I thought the point of templates was to template things.
You can have <script> within templates.  What you can't do is use the innerHTML setter to get runnable scripts.

Just like you can have <div><script></script></div> and the script will run, but if you start with an empty <div> and set its innerHTML to "<script></script>" the script will NOT run.  That is, the weird thing here is innerHTML, not templates.
Ah, so the problem is where I set the new document's body. I did it this way because it emulates what the HTML Imports polyfill does, so there's a bug in that polyfill.

Thanks!
Hmm.  Which polyfill is this?  Sounds like we should work with the authors to get it fixed...
Flags: needinfo?(matthew)
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: