Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Shadow DOM (ShadowRoot) #101

Merged
merged 13 commits into from
Jan 24, 2023

Conversation

zm-cttae
Copy link

Resolves #86.

@zm-cttae
Copy link
Author

zm-cttae commented Jan 10, 2023

Possible test case we can comandeer:

./test-lib/mdn-web-components-examples/slotchange.html (source)
<!DOCTYPE html>
<html>
  <!-- Sourced from https://github.com/mdn/web-components-examples/blob/a507099/slotchange/index.html -->
  <head>
    <meta charset="utf-8">
    <title>mdn/web-components-examples/slotchange</title>
    <script defer>
      customElements.define('summary-display',
        class extends HTMLElement {
          constructor() {
            super();
            const template = document.getElementById('summary-display-template');
            const templateContent = template.content;
            const shadowRoot = this.attachShadow({mode: 'open'});
            shadowRoot.appendChild(templateContent.cloneNode(true));
            const items = Array.from(this.querySelectorAll('li'));
            const descriptions = Array.from(this.querySelectorAll('p'));
            items.forEach(function(item) { item.addEventListener('click', clickHandler); })
            items[0].click();
            function clickHandler(event) {
              items.forEach(item => {
                item.style.backgroundColor = 'white';
              });
              descriptions.forEach(description => {
                updateDisplay(description, event.target);
              });
            }
            function updateDisplay(description, item) {
              description.removeAttribute('slot');
              if(description.getAttribute('data-name') === item.textContent) {
                description.setAttribute('slot', 'choice');
                item.style.backgroundColor = '#bad0e4';
              }
            }
          }
        }
      );
    </script>
    <style>
      li {
        cursor: pointer;
      }
    </style>
  </head>
  <body>
    <h1>slotchange event example</h1>

    <summary-display>
      <ul slot="master-list">
        <li>Apples</li>
        <li>Pears</li>
        <li>Bananas</li>
        <li>Oranges</li>
        <li>Peaches</li>
        <li>Strawberries</li>
        <li>Blueberries</li>
      </ul>

      <p data-name="Apples">A common, sweet, crunchy fruit, usually green or yellow in color.</p>
      <p data-name="Pears">A fairly common, sweet, usually green fruit, usually softer than Apples.</p>
      <p data-name="Bananas">A long, curved, yellow fruit, with a fairly gentle flavor.</p>
      <p data-name="Oranges">Orange in color, usually sweet but can be sharp, often contains pips.</p>
      <p data-name="Peaches">An orange fruit with big stone in the middle, and sweet, juicy flesh.</p>
      <p data-name="Strawberries">A red fruit with yellow seeds on the outside; has a sweet flavor and a pretty shape.</p>
      <p data-name="Blueberries">They are berries and they are blue; sweet in flavor, small in size, round.</p>
    </summary-display>

    <template id="summary-display-template">
      <article>
        <div>
          <slot name="master-list"></slot>
        </div>
        <div>
          <slot name="choice"></slot>
        </div>
      </article>
    </template>
  </body>
</html>

@zm-cttae zm-cttae force-pushed the feat-shadow-dom-support branch from addf823 to bb82420 Compare January 11, 2023 09:53
src/dom-to-image-more.js Outdated Show resolved Hide resolved
src/dom-to-image-more.js Outdated Show resolved Hide resolved
@zm-cttae zm-cttae requested a review from IDisposable January 18, 2023 16:13
@zm-cttae zm-cttae marked this pull request as ready for review January 18, 2023 16:19
@IDisposable
Copy link
Member

Hate to do this to you, but probably will land this after the #106... do you have a test we can put in the suite?

@zm-cttae
Copy link
Author

Added tests, greatly simplified that snippet first too :)

src/dom-to-image-more.js Outdated Show resolved Hide resolved
src/dom-to-image-more.js Outdated Show resolved Hide resolved
src/dom-to-image-more.js Outdated Show resolved Hide resolved
src/dom-to-image-more.js Outdated Show resolved Hide resolved
src/dom-to-image-more.js Outdated Show resolved Hide resolved
src/dom-to-image-more.js Outdated Show resolved Hide resolved
src/dom-to-image-more.js Show resolved Hide resolved
- Remove extraneous logic to handle ShadowRoot
- Abstract `cloneChildren` ternaries awayy
These tags are (mis?)used without scoping in shadow DOM
Remove them to prevent the cloned shadow DOM breaking
Also STYLE tags can contribute media queries that damage output integrity
@IDisposable
Copy link
Member

This is looking REALLY good, thanks for all your hard work :)
The questions I have outstanding are trivial and you can just say :shipit: and I'm happy to merge as we are now.

@zm-cttae
Copy link
Author

:shipit:

@IDisposable IDisposable merged commit 437d360 into 1904labs:master Jan 24, 2023
@zm-cttae zm-cttae deleted the feat-shadow-dom-support branch January 25, 2023 07:34
@IDisposable
Copy link
Member

This seems to have failed unit testing
image

@zm-cttae
Copy link
Author

Yup must be that margin on <p>

@IDisposable
Copy link
Member

The whole cloned node section is blank.... creating a fiddle
https://jsfiddle.net/w0efg2y7/6/

@IDisposable
Copy link
Member

This is the emitted IFRAME from the fiddle
shadow-fiddle.html.txt
(rename back to HTML)

@zm-cttae
Copy link
Author

I think there must have been a race condition where the <script> kicked in after the test bench ran. It's a custom element that won't render until then

Maybe it works if the <script> tag is inside the element

@IDisposable
Copy link
Member

I moved the script up to above the content and now we get a warning when running it... but the cloning works. SO WRONG
https://jsfiddle.net/c349vyLs/4/

@zm-cttae
Copy link
Author

zm-cttae commented Feb 2, 2023

Got it ... the element in the testcase is technically dependent on template also so it needs to go after it.

@IDisposable
Copy link
Member

... going to have to think through how to delay long enough to make the tests pass... maybe a simple await somewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Does not work with ShadowDom/ShadowRoot.
2 participants