Skip to content

Commit 0887dba

Browse files
committed
[property shim] Make sure "initial" and "inherit" behave as they would natively
Fixes #3803
1 parent be74f7e commit 0887dba

2 files changed

Lines changed: 91 additions & 4 deletions

File tree

‎src/lib/style-properties.html‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,15 @@
9191
} else {
9292
var m, rx = this.rx.VAR_ASSIGN;
9393
var cssText = rule.parsedCssText;
94+
var value;
9495
var any;
9596
while ((m = rx.exec(cssText))) {
9697
// note: group 2 is var, 3 is mixin
97-
properties[m[1].trim()] = (m[2] || m[3]).trim();
98+
value = (m[2] || m[3]).trim();
99+
// value of 'inherit' is equivalent to not setting the property here
100+
if (value !== 'inherit') {
101+
properties[m[1].trim()] = value;
102+
}
98103
any = true;
99104
}
100105
return any;
@@ -153,10 +158,13 @@
153158
// case (2) variable
154159
var self = this;
155160
var fn = function(prefix, value, fallback, suffix) {
156-
var propertyValue = (self.valueForProperty(props[value], props) ||
161+
var propertyValue = self.valueForProperty(props[value], props);
162+
// if value is "initial", then the variable should be treated as unset
163+
if (!propertyValue || propertyValue === 'initial') {
157164
// fallback may be --a or var(--a) or literal
158-
self.valueForProperty(props[fallback] || fallback, props) ||
159-
fallback);
165+
propertyValue = self.valueForProperty(props[fallback] || fallback, props) ||
166+
fallback;
167+
}
160168
return prefix + (propertyValue || '') + suffix;
161169
};
162170
property = styleUtil.processVariableAndFallback(property, fn);

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,60 @@
773773
</script>
774774
</dom-module>
775775

776+
<dom-module id="x-inherit">
777+
<template>
778+
<style>
779+
:host {
780+
--border: inherit;
781+
border: var(--border, 10px solid black);
782+
}
783+
</style>
784+
</template>
785+
<script>
786+
HTMLImports.whenReady(function() {
787+
Polymer({
788+
is: 'x-inherit'
789+
})
790+
});
791+
</script>
792+
</dom-module>
793+
794+
<dom-module id="x-inherit-parent">
795+
<template>
796+
<style>
797+
#child {
798+
--border: 5px solid blue;
799+
}
800+
</style>
801+
<x-inherit id="child"></x-inherit>
802+
</template>
803+
<script>
804+
HTMLImports.whenReady(function() {
805+
Polymer({
806+
is: 'x-inherit-parent'
807+
})
808+
});
809+
</script>
810+
</dom-module>
811+
812+
<dom-module id="x-initial">
813+
<template>
814+
<style>
815+
:host {
816+
--border: initial;
817+
border: var(--border, 10px solid black);
818+
}
819+
</style>
820+
</template>
821+
<script>
822+
HTMLImports.whenReady(function() {
823+
Polymer({
824+
is: 'x-initial'
825+
})
826+
});
827+
</script>
828+
</dom-module>
829+
776830
<script>
777831
suite('scoped-styling-var', function() {
778832

@@ -1190,6 +1244,31 @@
11901244
assertComputed(e, 'rgb(255, 0, 0)', null, 'background-color');
11911245
assertComputed(e, '6px');
11921246
});
1247+
1248+
test('"initial" as a custom property value works as expected', function() {
1249+
if (Polymer.Settings.useNativeCSSProperties) {
1250+
this.skip();
1251+
}
1252+
var e = document.createElement('x-initial');
1253+
document.body.appendChild(e);
1254+
CustomElements.takeRecords();
1255+
assertComputed(e, '10px');
1256+
});
1257+
1258+
test('"inherit" as a custom property value works as expected', function() {
1259+
if (Polymer.Settings.useNativeCSSProperties) {
1260+
this.skip();
1261+
}
1262+
var e = document.createElement('x-inherit');
1263+
document.body.appendChild(e);
1264+
CustomElements.takeRecords();
1265+
assertComputed(e, '10px');
1266+
1267+
e = document.createElement('x-inherit-parent');
1268+
document.body.appendChild(e);
1269+
CustomElements.takeRecords();
1270+
assertComputed(e.$.child, '5px');
1271+
});
11931272
});
11941273

11951274
</script>

0 commit comments

Comments
 (0)