Skip to content

Commit 8f4c3f6

Browse files
committed
Merge pull request #3470 from Polymer/fix-3461
Fixes #3461: Only avoid creating a statically scoped stylesheet when …
2 parents 91873c1 + e2c5f9e commit 8f4c3f6

File tree

6 files changed

+96
-32
lines changed

6 files changed

+96
-32
lines changed

polymer.html

+1-3
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@
3232
this._prepConstructor();
3333
// template
3434
this._prepTemplate();
35-
// styles
35+
// styles and style properties
3636
this._prepStyles();
37-
// style properties
38-
this._prepStyleProperties();
3937
// template markup
4038
this._prepAnnotations();
4139
// accessors

src/lib/style-properties.html

+8-6
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,14 @@
8282

8383
// returns cssText of properties that consume variables/mixins
8484
collectCssText: function(rule) {
85-
var cssText = rule.parsedCssText;
86-
// NOTE: we support consumption inside mixin assignment
87-
// but not production, so strip out {...}
88-
cssText = cssText.replace(this.rx.BRACKETED, '')
85+
return this.collectConsumingCssText(rule.parsedCssText);
86+
},
87+
88+
// NOTE: we support consumption inside mixin assignment
89+
// but not production, so strip out {...}
90+
collectConsumingCssText: function(cssText) {
91+
return cssText.replace(this.rx.BRACKETED, '')
8992
.replace(this.rx.VAR_ASSIGN, '');
90-
return cssText;
9193
},
9294

9395
collectPropertiesInCssText: function(cssText, props) {
@@ -312,7 +314,7 @@
312314
_elementKeyframeTransforms: function(element, scopeSelector) {
313315
var keyframesRules = element._styles._keyframes;
314316
var keyframeTransforms = {};
315-
if (!nativeShadow) {
317+
if (!nativeShadow && keyframesRules) {
316318
// For non-ShadowDOM, we transform all known keyframes rules in
317319
// advance for the current scope. This allows us to catch keyframes
318320
// rules that appear anywhere in the stylesheet:

src/standard/styling.html

+7-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@
4545
// calculate shimmed styles (we must always do this as it
4646
// stores shimmed style data in the css rules for later use)
4747
var cssText = styleTransformer.elementStyles(this);
48-
// do we really need to output shimmed styles
49-
var needsStatic = this._needsStaticStyles(this._styles);
48+
// prepare to shim style properties.
49+
this._prepStyleProperties();
50+
// do we really need to output static shimmed styles?
51+
// only if no custom properties are used since otherwise
52+
// styles are applied via property shimming.
53+
var needsStatic = this._styles.length &&
54+
!this._needsStyleProperties();
5055
// under shady dom we always output a shimmed style (which may be
5156
// empty) so that other dynamic stylesheets can always be placed
5257
// after the element's main stylesheet.

src/standard/x-styling.html

+1-18
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,17 @@
1818
var serializeValueToAttribute = Polymer.Base.serializeValueToAttribute;
1919

2020
var propertyUtils = Polymer.StyleProperties;
21-
var styleUtil = Polymer.StyleUtil;
2221
var styleTransformer = Polymer.StyleTransformer;
2322
var styleDefaults = Polymer.StyleDefaults;
2423

2524
var nativeShadow = Polymer.Settings.useNativeShadow;
2625

2726
Polymer.Base._addFeature({
2827

29-
// Skip applying CSS if there are some mixins or variables used
30-
// since styles with mixins and variables will be added on later stages anyway,
31-
// and will include styles applied here, no need to do this twice
32-
_needsStaticStyles: function(styles) {
33-
var needsStatic;
34-
for (var i=0, l=styles.length, css; i < l; i++) {
35-
css = styleUtil.parser._clean(styles[i].textContent);
36-
needsStatic = needsStatic || Boolean(css);
37-
if (css.match(propertyUtils.rx.MIXIN_MATCH) ||
38-
css.match(propertyUtils.rx.VAR_MATCH)) {
39-
return false;
40-
}
41-
}
42-
return needsStatic;
43-
},
44-
4528
_prepStyleProperties: function() {
4629
// note: an element should produce an x-scope stylesheet
4730
// if it has any _stylePropertyNames
48-
this._ownStylePropertyNames = this._styles ?
31+
this._ownStylePropertyNames = this._styles && this._styles.length ?
4932
propertyUtils.decorateStyles(this._styles) :
5033
null;
5134
},

test/unit/styling-cross-scope-apply.html

+42-3
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,27 @@
269269
</script>
270270
</dom-module>
271271

272+
<dom-module id="x-var-produce-via-consume">
273+
<template>
274+
<style>
275+
:host {
276+
display: block;
277+
border: 10px solid orange;
278+
--foo: {
279+
color: var(--bar);
280+
}
281+
}
282+
</style>
283+
</template>
284+
<script>
285+
HTMLImports.whenReady(function() {
286+
Polymer({
287+
is: 'x-var-produce-via-consume'
288+
});
289+
});
290+
</script>
291+
</dom-module>
292+
272293

273294
<script>
274295
suite('scoped-styling-apply', function() {
@@ -361,10 +382,28 @@
361382
});
362383
});
363384

385+
test('producing a var that consumes another var preserves static styling', function() {
386+
var d = document.createElement('x-var-produce-via-consume');
387+
document.body.appendChild(d);
388+
CustomElements.takeRecords();
389+
assertComputed(d, '10px');
390+
});
391+
392+
test('producing a var that consumes results in static and not dynamic stylesheet', function() {
393+
var d = document.createElement('x-var-produce-via-consume');
394+
document.body.appendChild(d);
395+
CustomElements.takeRecords();
396+
var styleRoot = d.shadowRoot ? d.shadowRoot : document.head;
397+
var staticStyle = styleRoot.querySelector('style[scope=x-var-produce-via-consume]');
398+
assert.ok(staticStyle);
399+
assert.match(staticStyle.textContent, /display/, 'static style does not contain style content');
400+
assert.equal(styleRoot.querySelectorAll('style[scope~=x-var-produce-via-consume]').length, 1);
401+
});
402+
364403
// TODO(sorvell): fix for #1761 was reverted; include test once this issue is addressed
365-
// test('mixin values can be overridden by subsequent concrete properties', function() {
366-
// assertComputed(styled.$.override, '19px');
367-
// });
404+
test('mixin values can be overridden by subsequent concrete properties', function() {
405+
assertComputed(styled.$.override, '19px');
406+
});
368407
});
369408

370409
</script>

test/unit/styling-cross-scope-var.html

+37
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,25 @@
599599
</script>
600600
</dom-module>
601601

602+
<dom-module id="x-var-produce-via-consume">
603+
<template>
604+
<style>
605+
:host {
606+
display: block;
607+
border: 10px solid orange;
608+
--foo: var(--bar);
609+
}
610+
</style>
611+
</template>
612+
<script>
613+
HTMLImports.whenReady(function() {
614+
Polymer({
615+
is: 'x-var-produce-via-consume'
616+
});
617+
});
618+
</script>
619+
</dom-module>
620+
602621
<script>
603622
suite('scoped-styling-var', function() {
604623

@@ -864,6 +883,24 @@
864883
assertComputed(styled.$.overridesConcrete, '4px');
865884
});
866885

886+
test('producing a var that consumes another var preserves static styling', function() {
887+
var d = document.createElement('x-var-produce-via-consume');
888+
document.body.appendChild(d);
889+
CustomElements.takeRecords();
890+
assertComputed(d, '10px');
891+
});
892+
893+
test('producing a var that consumes results in static and not dynamic stylesheet', function() {
894+
var d = document.createElement('x-var-produce-via-consume');
895+
document.body.appendChild(d);
896+
CustomElements.takeRecords();
897+
var styleRoot = d.shadowRoot ? d.shadowRoot : document.head;
898+
var staticStyle = styleRoot.querySelector('style[scope=x-var-produce-via-consume]');
899+
assert.ok(staticStyle);
900+
assert.match(staticStyle.textContent, /display/, 'static style does not contain style content');
901+
assert.equal(styleRoot.querySelectorAll('style[scope~=x-var-produce-via-consume]').length, 1);
902+
});
903+
867904
});
868905

869906
</script>

0 commit comments

Comments
 (0)