From d77e073eb9a8c4955d0221a01f4fb5445ab992ff Mon Sep 17 00:00:00 2001 From: Steven Orvell Date: Wed, 18 Oct 2017 16:49:49 -0700 Subject: [PATCH] Add support for styles with a `shady-unscoped` attribute This is being added to 1.x and it's ported forward here so it can be used in hybrid mode. --- lib/utils/style-gather.html | 27 +++- test/runner.html | 3 +- .../unit/shady-unscoped-style-import-css.html | 3 + test/unit/shady-unscoped-style-import.html | 26 ++++ test/unit/shady-unscoped-style.html | 121 ++++++++++++++++++ 5 files changed, 175 insertions(+), 5 deletions(-) create mode 100644 test/unit/shady-unscoped-style-import-css.html create mode 100644 test/unit/shady-unscoped-style-import.html create mode 100644 test/unit/shady-unscoped-style.html diff --git a/lib/utils/style-gather.html b/lib/utils/style-gather.html index a48ab2915f..8e9f61c65a 100644 --- a/lib/utils/style-gather.html +++ b/lib/utils/style-gather.html @@ -14,6 +14,8 @@ const MODULE_STYLE_LINK_SELECTOR = 'link[rel=import][type~=css]'; const INCLUDE_ATTR = 'include'; + const SHADY_UNSCOPED_ATTR = 'shady-unscoped'; + const unscopedStyleImportsMap = new WeakMap(); function importModule(moduleId) { const /** Polymer.DomModule */ PolymerDomModule = customElements.get('dom-module'); @@ -109,8 +111,14 @@ cssText += this.cssFromModules(include); } e.parentNode.removeChild(e); - cssText += baseURI ? + const styleCss = baseURI ? Polymer.ResolveUrl.resolveCss(e.textContent, baseURI) : e.textContent; + if (window.ShadyDOM && e.hasAttribute(SHADY_UNSCOPED_ATTR)) { + e.textContent = styleCss; + document.head.insertBefore(e, document.head.firstChild); + } else { + cssText += styleCss; + } } return cssText; }, @@ -145,9 +153,20 @@ // under the HTMLImports polyfill, there will be no 'body', // but the import pseudo-doc can be used directly. let container = importDoc.body ? importDoc.body : importDoc; - cssText += - Polymer.ResolveUrl.resolveCss(container.textContent, - importDoc.baseURI); + const importCss = Polymer.ResolveUrl.resolveCss(container.textContent, + importDoc.baseURI); + // support the shady-unscoped promoting styles to main document + if (window.ShadyDOM && p.hasAttribute(SHADY_UNSCOPED_ATTR)) { + if (!unscopedStyleImportsMap.has(importDoc)) { + unscopedStyleImportsMap.set(importDoc); + const style = document.createElement('style'); + style.setAttribute(SHADY_UNSCOPED_ATTR, ''); + style.textContent = importCss; + document.head.insertBefore(style, document.head.firstChild); + } + } else { + cssText += importCss; + } } } return cssText; diff --git a/test/runner.html b/test/runner.html index 8fe8b14f85..bc8e1f6217 100644 --- a/test/runner.html +++ b/test/runner.html @@ -73,7 +73,8 @@ 'unit/mixin-utils.html', 'unit/mixin-behaviors.html', 'unit/render-status.html', - 'unit/dir.html' + 'unit/dir.html', + 'unit/shady-unscoped-style.html' ]; // http://eddmann.com/posts/cartesian-product-in-javascript/ diff --git a/test/unit/shady-unscoped-style-import-css.html b/test/unit/shady-unscoped-style-import-css.html new file mode 100644 index 0000000000..3e99a3189c --- /dev/null +++ b/test/unit/shady-unscoped-style-import-css.html @@ -0,0 +1,3 @@ +.import { + border: 2px solid yellow; +} \ No newline at end of file diff --git a/test/unit/shady-unscoped-style-import.html b/test/unit/shady-unscoped-style-import.html new file mode 100644 index 0000000000..5fe5c6ae4f --- /dev/null +++ b/test/unit/shady-unscoped-style-import.html @@ -0,0 +1,26 @@ + + + + + + + + + diff --git a/test/unit/shady-unscoped-style.html b/test/unit/shady-unscoped-style.html new file mode 100644 index 0000000000..746c526d1c --- /dev/null +++ b/test/unit/shady-unscoped-style.html @@ -0,0 +1,121 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file