Skip to content

Commit 8fd3e93

Browse files
committed
Fix :dir selector when element uses CSS Custom Property Shim
Fixes #4925
1 parent 81383a7 commit 8fd3e93

2 files changed

Lines changed: 47 additions & 6 deletions

File tree

src/lib/style-properties.html

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,10 @@
426426
rule.keyframesName, rule.transformedKeyframesName);
427427
},
428428

429+
_hasDirOrHostContext: function(parsedSelector) {
430+
return /:host-context|:dir/.test(parsedSelector);
431+
},
432+
429433
// Strategy: x scope shim a selector e.g. to scope `.x-foo-42` (via classes):
430434
// non-host selector: .a.x-foo -> .x-foo-42 .a.x-foo
431435
// host selector: x-foo.wide -> .x-foo-42.wide
@@ -437,14 +441,22 @@
437441
_scopeSelector: function(rule, hostRx, hostSelector, viaAttr, scopeId) {
438442
rule.transformedSelector = rule.transformedSelector || rule.selector;
439443
var selector = rule.transformedSelector;
440-
var scope = viaAttr ? '[' + styleTransformer.SCOPE_NAME + '~=' +
441-
scopeId + ']' :
442-
'.' + scopeId;
444+
var scope = styleTransformer._calcElementScope(scopeId, viaAttr);
443445
var parts = selector.split(',');
444446
for (var i=0, l=parts.length, p; (i<l) && (p=parts[i]); i++) {
445-
parts[i] = p.match(hostRx) ?
446-
p.replace(hostSelector, scope) :
447-
scope + ' ' + p;
447+
// :host-context and :dir will
448+
if (this._hasDirOrHostContext(rule.parsedSelector)) {
449+
var hostScope = styleTransformer._calcElementScope(hostSelector, viaAttr);
450+
var sub = p.split(' ');
451+
for (var j = 0; j < sub.length; j++) {
452+
sub[j] = sub[j].replace(hostScope, scope);
453+
}
454+
parts[i] = sub.join(' ');
455+
} else {
456+
parts[i] = p.match(hostRx) ?
457+
p.replace(hostSelector, scope) :
458+
scope + ' ' + p;
459+
}
448460
}
449461
rule.selector = parts.join(',');
450462
},

test/unit/dir.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@
8787
</script>
8888
</dom-module>
8989

90+
<dom-module id="x-var">
91+
<template>
92+
<style>
93+
:host {
94+
display: var(--ziz, block);
95+
}
96+
:dir(rtl) {
97+
border: 10px solid rgb(123, 123, 123);
98+
}
99+
</style>
100+
</template>
101+
<script>
102+
addEventListener('WebComponentsReady', function() {
103+
Polymer({is: 'x-var'});
104+
});
105+
</script>
106+
</dom-module>
107+
90108
<test-fixture id="dir">
91109
<template>
92110
<x-dir></x-dir>
@@ -99,6 +117,12 @@
99117
</template>
100118
</test-fixture>
101119

120+
<test-fixture id="var">
121+
<template>
122+
<x-var></x-var>
123+
</template>
124+
</test-fixture>
125+
102126
<script>
103127
function assertComputed(node, expected, property) {
104128
property = property || 'border-top-width';
@@ -169,6 +193,11 @@
169193
});
170194
});
171195
});
196+
197+
test('elements with :dir and CSS Custom Properties work', function() {
198+
var el = fixture('var');
199+
assertComputed(el, '10px');
200+
})
172201
});
173202
</script>
174203
</body>

0 commit comments

Comments
 (0)