Skip to content

Commit c7571e5

Browse files
committed
Handle mixins with property values of inherit and initial
Fixes #3818
1 parent 14f344a commit c7571e5

File tree

2 files changed

+122
-1
lines changed

2 files changed

+122
-1
lines changed

src/lib/apply-shim.html

+34-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
// match var(--a, --b) to make var(--a, var(--b));
8181
var BAD_VAR = /var\(\s*(--[^,]*),\s*(--[^)]*)\)/g;
8282
var APPLY_NAME_CLEAN = /;\s*/m;
83+
var INITIAL_INHERIT = /^\s*(initial)|(inherit)\s*$/;
8384

8485
// separator used between mixin-name and mixin-property-name when producing properties
8586
// NOTE: plain '-' may cause collisions in user styles
@@ -102,19 +103,51 @@
102103
return mixinMap[name];
103104
}
104105

106+
var measureElement;
107+
function getInitialValueForProperty(property) {
108+
if (!measureElement) {
109+
measureElement = document.createElement('meta');
110+
measureElement.style.all = 'initial';
111+
document.head.appendChild(measureElement);
112+
}
113+
return window.getComputedStyle(measureElement).getPropertyValue(property);
114+
}
115+
116+
function replaceInitialOrInherit(property, value) {
117+
var match = INITIAL_INHERIT.exec(value);
118+
if (match) {
119+
if (match[1]) {
120+
// initial
121+
// replace `initial` with the concrete initial value for this property
122+
value = getInitialValueForProperty(property);
123+
} else {
124+
// inherit
125+
// with this purposfully illegal value, the variable will be invalid at
126+
// compute time (https://www.w3.org/TR/css-variables/#invalid-at-computed-value-time)
127+
// and for inheriting values, will behave similarly
128+
// we cannot support the same behavior for non inheriting values like 'border'
129+
value = 'apply-shim-inherit';
130+
}
131+
}
132+
return value;
133+
}
134+
105135
// "parse" a mixin definition into a map of properties and values
106136
// cssTextToMap('border: 2px solid black') -> ('border', '2px solid black')
107137
function cssTextToMap(text) {
108138
var props = text.split(';');
139+
var property, value;
109140
var out = {};
110141
for (var i = 0, p, sp; i < props.length; i++) {
111142
p = props[i];
112143
if (p) {
113144
sp = p.split(':');
114145
// ignore lines that aren't definitions like @media
115146
if (sp.length > 1) {
147+
property = sp[0].trim();
116148
// some properties may have ':' in the value, like data urls
117-
out[sp[0].trim()] = sp.slice(1).join(':');
149+
value = replaceInitialOrInherit(property, sp.slice(1).join(':'));
150+
out[property] = value;
118151
}
119152
}
120153
}

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

+88
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,71 @@
591591
</script>
592592
</dom-module>
593593

594+
<dom-module id="x-apply">
595+
<template>
596+
<style>
597+
:host {
598+
color: rgb(255, 0, 0);
599+
border: 1px solid black;
600+
@apply --initial-inherit;
601+
}
602+
</style>
603+
</template>
604+
<script>
605+
HTMLImports.whenReady(function() {
606+
Polymer({
607+
is: 'x-apply'
608+
})
609+
});
610+
</script>
611+
</dom-module>
612+
613+
<dom-module id="x-initial">
614+
<template>
615+
<style>
616+
:host {
617+
--initial-inherit: {
618+
color: initial;
619+
border: initial;
620+
};
621+
color: rgb(0, 255, 0);
622+
border: 2px solid blue;
623+
}
624+
</style>
625+
<x-apply id="child"></x-apply>
626+
</template>
627+
<script>
628+
HTMLImports.whenReady(function() {
629+
Polymer({
630+
is: 'x-initial'
631+
})
632+
});
633+
</script>
634+
</dom-module>
635+
636+
<dom-module id="x-inherit">
637+
<template>
638+
<style>
639+
:host {
640+
--initial-inherit: {
641+
color: inherit;
642+
border: inherit;
643+
};
644+
color: rgb(0, 255, 0);
645+
border: 2px solid blue;
646+
}
647+
</style>
648+
<x-apply id="child"></x-apply>
649+
</template>
650+
<script>
651+
HTMLImports.whenReady(function() {
652+
Polymer({
653+
is: 'x-inherit'
654+
})
655+
});
656+
</script>
657+
</dom-module>
658+
594659
<script>
595660
suite('scoped-styling-apply', function() {
596661
function assertComputed(element, value, property) {
@@ -811,6 +876,29 @@
811876
CustomElements.takeRecords();
812877
assertComputed(e, '20px');
813878
});
879+
880+
test('Mixins can set "inherit" for a property', function() {
881+
var e = document.createElement('x-inherit');
882+
document.body.appendChild(e);
883+
CustomElements.takeRecords();
884+
assertComputed(e.$.child, 'rgb(0, 255, 0)', 'color');
885+
if (!Polymer.Settings.useNativeCSSProperties) {
886+
assertComputed(e.$.child, '2px');
887+
} else {
888+
// this is not supported for the apply shim
889+
assert.throws(function() {
890+
assertComputed(e.$.child, '2px');
891+
})
892+
}
893+
});
894+
895+
test('Mixins can set "initial" for a property', function() {
896+
var e = document.createElement('x-initial');
897+
document.body.appendChild(e);
898+
CustomElements.takeRecords();
899+
assertComputed(e.$.child, 'rgb(0, 0, 0)', 'color');
900+
assertComputed(e.$.child, '0px');
901+
});
814902
});
815903

816904
</script>

0 commit comments

Comments
 (0)