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

Select <style> more strictly #4976

Closed
wants to merge 1 commit into from

Conversation

Westbrook
Copy link
Contributor

@Westbrook Westbrook commented Dec 6, 2017

Select only direct children because you need to be able to user "insertBefore" later

Fixes #4975

ToDo:

  • confirm x-browser (:scope might not be the answer because of that)
  • add tests

@Westbrook
Copy link
Contributor Author

Reading up on the very clever and interesting :scope > style selector, it looks like there may be some issue with x-browser support, I need to confirm on a system with IE, but in that case would something like the following be overkill:

function processElementStyles(klass, template, is, baseURI) {
      const styles = Polymer.StyleGather.stylesFromModuleImports(is).concat(
        Polymer.StyleGather.stylesFromTemplate(template));
      let templateStyles = template.content.querySelectorAll('style');
      let lastDirectChildStyleIndex = templateStyles.length-1
      let lastDirectChildStyle = templateStyles[lastDirectChildStyleIndex];
      // a direct child of the tempalte is needed for application of
      // "included" styles below via `insertBefore`
      while (!lastDirectChildStyle.parentNode.isSameNode(template.content)) {
        lastDirectChildStyleIndex--;
        if (lastDirectChildStyleIndex < 0) {
          // if no direct child style elements, choose the first element child
          lastDirectChildStyle = template.content.firstElementChild;
        } else {
          lastDirectChildStyle = templateStyles[lastDirectChildStyleIndex];
        }
      }
      // ensure all gathered styles are actually in this template.
      for (let i=0; i < styles.length; i++) {
        let s = styles[i];
        // if the style is not in this template, it's been "included" and
        // we put a clone of it in the template.
        if (s.getRootNode() != template.content) {
          s = s.cloneNode(true);
          template.content.insertBefore(s, lastDirectChildStyle);
        }
        s.textContent = klass._processStyleText(s.textContent, baseURI);
      }
      if (window.ShadyCSS) {
        window.ShadyCSS.prepareTemplate(template, is);
      }
    }

Reading it back here, should it always be template.content.firstElementChild, which allows to a removal of all the back checking?

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

Successfully merging this pull request may close these issues.

2 participants