Skip to content

Commit

Permalink
Fix :dir selector when element uses CSS Custom Property Shim
Browse files Browse the repository at this point in the history
Fixes #4925
  • Loading branch information
dfreedm committed Nov 7, 2017
1 parent 81383a7 commit 8fd3e93
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/lib/style-properties.html
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@
rule.keyframesName, rule.transformedKeyframesName);
},

_hasDirOrHostContext: function(parsedSelector) {
return /:host-context|:dir/.test(parsedSelector);
},

// Strategy: x scope shim a selector e.g. to scope `.x-foo-42` (via classes):
// non-host selector: .a.x-foo -> .x-foo-42 .a.x-foo
// host selector: x-foo.wide -> .x-foo-42.wide
Expand All @@ -437,14 +441,22 @@
_scopeSelector: function(rule, hostRx, hostSelector, viaAttr, scopeId) {
rule.transformedSelector = rule.transformedSelector || rule.selector;
var selector = rule.transformedSelector;
var scope = viaAttr ? '[' + styleTransformer.SCOPE_NAME + '~=' +
scopeId + ']' :
'.' + scopeId;
var scope = styleTransformer._calcElementScope(scopeId, viaAttr);
var parts = selector.split(',');
for (var i=0, l=parts.length, p; (i<l) && (p=parts[i]); i++) {
parts[i] = p.match(hostRx) ?
p.replace(hostSelector, scope) :
scope + ' ' + p;
// :host-context and :dir will
if (this._hasDirOrHostContext(rule.parsedSelector)) {
var hostScope = styleTransformer._calcElementScope(hostSelector, viaAttr);
var sub = p.split(' ');
for (var j = 0; j < sub.length; j++) {
sub[j] = sub[j].replace(hostScope, scope);
}
parts[i] = sub.join(' ');
} else {
parts[i] = p.match(hostRx) ?
p.replace(hostSelector, scope) :
scope + ' ' + p;
}
}
rule.selector = parts.join(',');
},
Expand Down
29 changes: 29 additions & 0 deletions test/unit/dir.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@
</script>
</dom-module>

<dom-module id="x-var">
<template>
<style>
:host {
display: var(--ziz, block);
}
:dir(rtl) {
border: 10px solid rgb(123, 123, 123);
}
</style>
</template>
<script>
addEventListener('WebComponentsReady', function() {
Polymer({is: 'x-var'});
});
</script>
</dom-module>

<test-fixture id="dir">
<template>
<x-dir></x-dir>
Expand All @@ -99,6 +117,12 @@
</template>
</test-fixture>

<test-fixture id="var">
<template>
<x-var></x-var>
</template>
</test-fixture>

<script>
function assertComputed(node, expected, property) {
property = property || 'border-top-width';
Expand Down Expand Up @@ -169,6 +193,11 @@
});
});
});

test('elements with :dir and CSS Custom Properties work', function() {
var el = fixture('var');
assertComputed(el, '10px');
})
});
</script>
</body>
Expand Down

0 comments on commit 8fd3e93

Please sign in to comment.