diff --git a/src/lib/style-util.html b/src/lib/style-util.html index 1fd3845f42..82c6eeec41 100644 --- a/src/lib/style-util.html +++ b/src/lib/style-util.html @@ -17,6 +17,8 @@ var settings = Polymer.Settings; return { + unscopedStyleImports: new WeakMap(), + SHADY_UNSCOPED_ATTR: 'shady-unscoped', // chrome 49 has semi-working css vars, check if box-shadow works // safari 9.1 has a recalc bug: https://bugs.webkit.org/show_bug.cgi?id=155782 NATIVE_VARIABLES: Polymer.Settings.useNativeCSSProperties, @@ -211,12 +213,34 @@ // get style element applied to main doc via HTMLImports polyfill e = e.__appliedElement || e; e.parentNode.removeChild(e); - cssText += this.resolveCss(e.textContent, element.ownerDocument); + var css = this.resolveCss(e.textContent, element.ownerDocument); + // support: unscoped styles + if (!settings.useNativeShadow && + e.hasAttribute(this.SHADY_UNSCOPED_ATTR)) { + e.textContent = css; + document.head.insertBefore(e, document.head.firstChild); + } else { + cssText += css; + } // it's an import, assume this is a text file of css content. // TODO(sorvell): plan is to deprecate this way to get styles; // remember to add deprecation warning when this is done. } else if (e.import && e.import.body) { - cssText += this.resolveCss(e.import.body.textContent, e.import); + var importCss = this.resolveCss(e.import.body.textContent, e.import); + // support: unscoped styles + // record imports in a WeakMap so they are de-duped if unscoped > 1x. + if (!settings.useNativeShadow && + e.hasAttribute(this.SHADY_UNSCOPED_ATTR)) { + if (!this.unscopedStyleImports.has(e.import)) { + this.unscopedStyleImports.set(e.import, true); + var importStyle = document.createElement('style'); + importStyle.setAttribute(this.SHADY_UNSCOPED_ATTR, ''); + importStyle.textContent = importCss; + document.head.insertBefore(importStyle, document.head.firstChild); + } + } else { + cssText += importCss; + } } } } diff --git a/test/runner.html b/test/runner.html index d18d6ba6ca..d72fbfcfc7 100644 --- a/test/runner.html +++ b/test/runner.html @@ -102,7 +102,11 @@ 'unit/custom-style-transformed.html', 'unit/custom-style-transformed.html?dom=shadow', 'unit/custom-style-transformed.html?lazyRegister=true&useNativeCSSProperties=true', - 'unit/custom-style-transformed.html?lazyRegister=true&useNativeCSSProperties=true&dom=shadow' + 'unit/custom-style-transformed.html?lazyRegister=true&useNativeCSSProperties=true&dom=shadow', + 'unit/shady-unscoped-style.html', + 'unit/shady-unscoped-style.html?dom=shadow', + 'unit/shady-unscoped-style.html?lazyRegister=true&useNativeCSSProperties=true', + 'unit/shady-unscoped-style.html?lazyRegister=true&useNativeCSSProperties=true&dom=shadow' ]; if ('import' in document.createElement('link') && (window.customElements || document.registerElement)) { diff --git a/test/unit/custom-style.html b/test/unit/custom-style.html index 3e926a51a7..25990dc06f 100644 --- a/test/unit/custom-style.html +++ b/test/unit/custom-style.html @@ -404,11 +404,17 @@ }); test('::shadow styles applied', function() { + if (Polymer.Settings.useNativeShadow) { + this.skip(); + } assertComputed(xFoo.$.bar2, '2px'); assertComputed(xFoo.$.bar2.$.baz, '3px'); }); test('/deep/ styles applied', function() { + if (Polymer.Settings.useNativeShadow) { + this.skip(); + } assertComputed(xFoo.$.bar3, '4px'); assertComputed(xFoo.$.bar3.$.baz, '5px'); }); diff --git a/test/unit/preserve-style-include/styling-scoped.html b/test/unit/preserve-style-include/styling-scoped.html index a6b5aa9e60..2a4e5fefc8 100644 --- a/test/unit/preserve-style-include/styling-scoped.html +++ b/test/unit/preserve-style-include/styling-scoped.html @@ -213,10 +213,16 @@ }); test('::shadow selectors', function() { + if (Polymer.Settings.useNativeShadow) { + this.skip(); + } assertComputed(styled.$.child.$.shadow, '7px'); }); test('/deep/ selectors', function() { + if (Polymer.Settings.useNativeShadow) { + this.skip(); + } assertComputed(styled.$.child.$.deep, '8px'); }); diff --git a/test/unit/shady-unscoped-style-import.css b/test/unit/shady-unscoped-style-import.css new file mode 100644 index 0000000000..ebc79b19e0 --- /dev/null +++ b/test/unit/shady-unscoped-style-import.css @@ -0,0 +1,12 @@ +/* +@license +Copyright (c) 2017 The Polymer Project Authors. All rights reserved. +This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt +The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt +The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt +Code distributed by Google as part of the polymer project is also +subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt +*/ +.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..808fd49490 --- /dev/null +++ b/test/unit/shady-unscoped-style-import.html @@ -0,0 +1,35 @@ + + + + + + + + + + diff --git a/test/unit/shady-unscoped-style.html b/test/unit/shady-unscoped-style.html new file mode 100644 index 0000000000..17bac40a17 --- /dev/null +++ b/test/unit/shady-unscoped-style.html @@ -0,0 +1,130 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file