Skip to content

Commit b00813c

Browse files
Steven Orvelljorgecasar
Steven Orvell
authored andcommitted
Fixes Polymer#3801. Ensure style host calculates custom properties before element. This ensures the scope's styles are prepared to be inspected by the element for matching rules.
1 parent 436f131 commit b00813c

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

src/standard/x-styling.html

+5-2
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@
118118

119119
_updateStyleProperties: function() {
120120
var info, scope = this._findStyleHost();
121+
// ensure scope properties exist before any access of scope cache.
122+
if (!scope._styleProperties) {
123+
scope._computeStyleProperties();
124+
}
121125
// install cache in host if it doesn't exist.
122126
if (!scope._styleCache) {
123127
scope._styleCache = new Polymer.StyleCache();
@@ -184,8 +188,7 @@
184188
_computeStyleProperties: function(scopeProps) {
185189
// get scope and make sure it has properties
186190
var scope = this._findStyleHost();
187-
// force scope to compute properties if they don't exist or if forcing
188-
// and it doesn't need properties
191+
// force scope to compute properties if they don't exist
189192
if (!scope._styleProperties) {
190193
scope._computeStyleProperties();
191194
}

test/runner.html

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
'unit/styling-cross-scope-unknown-host.html',
7171
'unit/style-cache.html',
7272
'unit/custom-style.html',
73+
'unit/custom-style-scope-cache.html',
7374
'unit/custom-style.html?lazyRegister=true&useNativeCSSProperties=true',
7475
'unit/custom-style.html?dom=shadow',
7576
'unit/custom-style.html?dom=shadow&lazyRegister=true&useNativeCSSProperties=true',
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<!doctype html>
2+
<!--
3+
@license
4+
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
5+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
6+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
7+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
8+
Code distributed by Google as part of the polymer project is also
9+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10+
-->
11+
<html>
12+
<head>
13+
<meta charset="utf-8">
14+
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
15+
<script src="../../../web-component-tester/browser.js"></script>
16+
<link rel="import" href="../../polymer.html">
17+
18+
</head>
19+
<body>
20+
<style is="custom-style">
21+
.c {
22+
--cache-element-border: 8px solid blue;
23+
}
24+
</style>
25+
<dom-module id="cache-element">
26+
<template>
27+
<style>
28+
:host {
29+
display: block;
30+
border: var(--cache-element-border, 4px solid orange);
31+
}
32+
</style>
33+
cache-element
34+
</template>
35+
<script>
36+
HTMLImports.whenReady(function() {
37+
Polymer({
38+
is: 'cache-element'
39+
});
40+
});
41+
</script>
42+
</dom-module>
43+
44+
<cache-element id="cache1" class="c"></cache-element>
45+
<cache-element id="cache2"></cache-element>
46+
47+
<script>
48+
49+
suite('custom-style scope cache', function() {
50+
51+
test('elements created declaratively conditionally styled via custom style receive correct properties', function() {
52+
var t1 = document.querySelector('#cache1');
53+
var t2 = document.querySelector('#cache2');;
54+
assertComputed(t1, '8px');
55+
assertComputed(t2, '4px');
56+
});
57+
58+
59+
test('elements created imperatively conditionally styled via custom style receive correct properties', function() {
60+
var t1 = document.createElement('cache-element');
61+
t1.classList.add('c');
62+
var t2 = document.createElement('cache-element');
63+
document.body.appendChild(t1);
64+
document.body.appendChild(t2);
65+
CustomElements.takeRecords();
66+
assertComputed(t1, '8px');
67+
assertComputed(t2, '4px');
68+
});
69+
70+
});
71+
72+
73+
function assertComputed(element, value, property, pseudo) {
74+
var computed = getComputedStyle(element, pseudo);
75+
property = property || 'border-top-width';
76+
assert.equal(computed[property], value, 'computed style incorrect for ' + property);
77+
}
78+
79+
</script>
80+
81+
</body>
82+
</html>

0 commit comments

Comments
 (0)