shadydom icon indicating copy to clipboard operation
shadydom copied to clipboard

Moving elements into iFrame with shady-dom

Open TimvdLippe opened this issue 8 years ago • 1 comments

From @HendrikThePendric on July 24, 2017 12:23

My app consists of a container with an iframe inside it. The container does not use Polymer and doesn't include webcomponents-loader.js, but the iframe does. When I try to move an element from the container into the iframe I get an error in browsers that rely on the shady DOM:

TypeError: a.__shady is undefined (FireFox - perhaps related to #811) Object doesn't support property or method 'getRootNode' (IE11)

I realize this might be quite an exotic scenario, and could imagine that this is not supported. However, I have made a minimal replication of my problem and have found that a lot of similar scenarios are working fine, so it might possibly be a bug after all. A summary:

  • Adding an from the iframe context is working fine, as expected.
  • Adding an item from the document context is working as well. I hadn't expected this to work.
  • Getting an an existing element from the document context, and then moving it into the iframe is failing.

Please see a reproduction path below:

The container page

<!DOCTYPE html>
<html lang="en">
  <head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body>
    <div id="move-me" style="border: 1px solid green; margin: 20px; width: 400px;">I will be moved after 3 seconds</div>
    <iframe id="iframe" src="iframe.html" style="width: 800px; height: 800px; border: 1px solid red;"></iframe>
    <script>
      window.onload = function() {
        moveEl     = document.querySelector('#move-me');
        iframe     = document.querySelector('#iframe');
        iframeDoc  = iframe.contentDocument || iframe.contentWindow.document;
        iframeBody = iframeDoc.querySelector('body');

        setTimeout(function () {
          // Will work
          createInIframe();
        }, 1000);
        setTimeout(function () {
          // Will work
          createOutsideAndAddInside();
        }, 2000);
        setTimeout(function() {
          // Will fail
          moveToIframe();
        }, 3000);
      }

      
      function createInIframe () {
        var div          = iframeDoc.createElement('div'); // THIS IS ONLY DIFFERENCE
        div.innerText    = 'I was created INSIDE the iFrame';
        div.style.border = '1px solid magenta';
        iframeBody.appendChild(div);
      }

      function createOutsideAndAddInside() {
        var div          = document.createElement('div'); // THIS IS ONLY DIFFERENCE
        div.innerText    = 'I was created OUTSIDE the iframe';
        div.style.border = '1px solid green';
        iframeBody.appendChild(div);
      }

      function moveToIframe () {
        iframeBody.appendChild(moveEl);
      }
    </script>
  </body>
</html>

The iframe

<!DOCTYPE html>
<html lang="en">
  <head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="bower_components/webcomponentsjs/custom-elements-es5-adapter.js"></script>
    <script src="bower_components/webcomponentsjs/webcomponents-loader.js"></script>
  </head>
  <body>
  </body>
</html>

Copied from original issue: webcomponents/webcomponentsjs#814

TimvdLippe avatar Jul 03 '18 10:07 TimvdLippe

This would only work if the polyfills are loaded in both frames. Any other configuration is not supported.

dfreedm avatar Jun 07 '19 22:06 dfreedm