Skip to content

Commit

Permalink
Merge pull request #4891 from Polymer/1.x-shady-unscoped-style
Browse files Browse the repository at this point in the history
[1.x] Adds support for `shady-unscoped` attribute to style elements.
  • Loading branch information
dfreedm authored Oct 20, 2017
2 parents 0e15b17 + 471dc9a commit f530289
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 3 deletions.
28 changes: 26 additions & 2 deletions src/lib/style-util.html
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion test/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
6 changes: 6 additions & 0 deletions test/unit/custom-style.html
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down
6 changes: 6 additions & 0 deletions test/unit/preserve-style-include/styling-scoped.html
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

Expand Down
12 changes: 12 additions & 0 deletions test/unit/shady-unscoped-style-import.css
Original file line number Diff line number Diff line change
@@ -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;
}
35 changes: 35 additions & 0 deletions test/unit/shady-unscoped-style-import.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!--
@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
-->
<dom-module id="global-shared1">
<link rel="import" type="css" shady-unscoped href="shady-unscoped-style-import.css">
<template>
<style shady-unscoped>
:root {
--zug: margin: 10px;
}

.happy {
@apply --zug;
border: 1px solid green;
}
</style>

<style>
.normal {
border: 3px solid orange;
}
</style>
</template>
</dom-module>

<dom-module id="global-shared2">
<link rel="import" type="css" shady-unscoped href="shady-unscoped-style-import.css">
</dom-module>

130 changes: 130 additions & 0 deletions test/unit/shady-unscoped-style.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<!doctype html>
<!--
@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
-->
<html>
<head>
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../../web-component-tester/browser.js"></script>
<link rel="import" href="../../polymer.html">
<link rel="import" href="shady-unscoped-style-import.html">
</head>
<body>

<custom-style>
<style is="custom-style">
html {
--foo: {
padding: 10px;
}
}
</style>
</custom-style>

<dom-module id="my-element">

<template>
<style include="global-shared1 global-shared2">
:host {
display: block;
}

.happy {
@apply --foo;
}
</style>
<div id="fromStyle" class="happy">Happy: green</div>
<div id="fromImport" class="import">Happy: yellow</div>
<div id="normal" class="normal">Happy: orange</div>
</template>

<script>
HTMLImports.whenReady(function() {
Polymer({
is: 'my-element'
});
});
</script>

</dom-module>

<dom-module id="my-element2">

<template>
<style include="global-shared1">
:host {
display: block;
}

</style>
<div id="fromStyle" class="happy">Happy: green</div>
<div id="fromImport" class="import">Happy: yellow</div>
<div id="normal" class="normal">Happy: orange</div>
</template>

<script>
HTMLImports.whenReady(function() {
Polymer({ is: 'my-element2'});
});
</script>

</dom-module>

<my-element></my-element>
<my-element2></my-element2>

<script>
suite('shady-unscoped styles', function() {

function assertComputed(element, value, property, pseudo) {
var computed = getComputedStyle(element, pseudo);
property = property || 'border-top-width';
if (Array.isArray(value)) {
assert.oneOf(computed[property], value, 'computed style incorrect for ' + property);
} else {
assert.equal(computed[property], value, 'computed style incorrect for ' + property);
}
}

var el1 = document.querySelector('my-element');
var el2 = document.querySelector('my-element2');

test('unscoped styles apply', function() {
assertComputed(el1.$.fromStyle, '1px');
assertComputed(el1.$.fromImport, '2px');
assertComputed(el2.$.fromStyle, '1px');
assertComputed(el2.$.fromImport, '2px');
});

test('shared and @apply apply when used with unscoped styles', function() {
assertComputed(el1.$.fromStyle, '10px', 'padding-top');
assertComputed(el1.$.normal, '3px');
assertComputed(el2.$.normal, '3px');
})

test('unscoped styling de-duped in ShadyDOM', function() {
if (Polymer.Settings.useNativeShadow) {
this.skip();
}
assert.equal(document.head.querySelectorAll('style[shady-unscoped]').length, 2);
});

test('@apply does not apply under ShadyDOM for shady-unscoped styles', function() {
if (Polymer.Settings.useNativeShadow) {
this.skip();
}
assertComputed(el1.$.fromStyle, '0px', 'margin-top');
assertComputed(el2.$.fromStyle, '0px', 'margin-top');
})


});
</script>
</body>
</html>

0 comments on commit f530289

Please sign in to comment.