Skip to content
This repository has been archived by the owner on Sep 20, 2019. It is now read-only.

Scripts cloned from HTML Imported template does not execute on polyfilled browsers #872

Closed
3 of 5 tasks
tomalec opened this issue Dec 2, 2017 · 1 comment
Closed
3 of 5 tasks

Comments

@tomalec
Copy link
Contributor

tomalec commented Dec 2, 2017

I have an externalDoc.html:

<script>
  document.body.innerHTML += '<p>Script from HTML Import executed</p>';
</script>
<template>
  Template content from HTML Import stamped
  <script>
    document.body.innerHTML += '<p>Script from imported template executed</p>';
  </script>
</template>

Then, I:

  1. Import it to the main using <link rel="import" href="externalDoc.html">
  2. Outer script is executed - ✓
  3. Clone and stamp templates content:
       let template = linkEl.import.querySelector('template');
       document.body.appendChild(
         document.importNode(template.content, true)
       );
  4. Text content and script gets stamped/appended to the main document - ✓

jsbin: http://output.jsbin.com/yijokor/6/quiet

Expected behavior

Cloned script is executed

Actual behavior

It's not

Affected browsers

  • Chrome ✓,
  • Firefox,
  • Edge,
  • IE,
  • [?] Safari,
  • v0 Polyfill - ✓

Unfortunately, it's a blocking issue for us to migrate to V1 polyfill, as we are developing an application platform based on modular views made with HTML Imports based client-side includes. (https://starcounter.io/html-partialsincludes-webcomponents-way/)

So our customers have hundreds of nested imports with every kind of HTML markup in their templates.

//cc @valdrinkoshi

tomalec added a commit to Juicy/imported-template that referenced this issue Dec 2, 2017
WIP: support WebComponents.js polyfill >=1.x - per-template script execution is missing.
webcomponents/webcomponentsjs#872

Implements #29
@valdrinkoshi
Copy link
Contributor

valdrinkoshi commented Dec 4, 2017

Looks like we'll have to clone scripts inside templates in order to execute them :[

<pre id="out"></pre>
<script>
  const template = document.createElement('template');
  template.innerHTML = '<script>window.executedTemplateScript=true<\/script>';

  // we have to clone scripts inside templates :[
  // const script = template.content.querySelector('script');
  // const clone = document.createElement('script');
  // clone.textContent = script.textContent;
  // template.content.insertBefore(clone, script);
  // template.content.removeChild(script);

  document.body.appendChild(document.importNode(template.content, true));
  out.textContent = 'executed? ' + window.executedTemplateScript;
</script>

valdrinkoshi pushed a commit to webcomponents/html-imports that referenced this issue Dec 5, 2017
valdrinkoshi pushed a commit to webcomponents/html-imports that referenced this issue Dec 11, 2017
tomalec added a commit to Juicy/imported-template that referenced this issue Dec 12, 2017
tomalec added a commit to Juicy/imported-template that referenced this issue Dec 12, 2017
valdrinkoshi pushed a commit to webcomponents/html-imports that referenced this issue Dec 16, 2017
valdrinkoshi added a commit to webcomponents/html-imports that referenced this issue Dec 18, 2017
* clone scripts inside templates

* skip test if template is not supported

* Clone scripts also in nested templates

#77
webcomponents/webcomponentsjs#872

* test with template polyfill

* expand tests for cloned templates

* chai.assert -> assert

* test with fix-template-scripts branch

* cannot exec w/o cloning, remove wrong test

* use template v1.2.1
tomalec added a commit to Juicy/imported-template that referenced this issue Feb 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants