Skip to content
This repository was archived by the owner on Sep 14, 2021. It is now read-only.

Commit 4a9d0d0

Browse files
authored
Merge pull request #215 from webcomponents/fix-inherit-initial-fallbacks
Do not replace "inherit" and "initial" in fallbacks
2 parents 078233f + 4443d21 commit 4a9d0d0

File tree

5 files changed

+51
-14
lines changed

5 files changed

+51
-14
lines changed

apply-shim.min.js

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apply-shim.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/apply-shim.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,10 @@ class ApplyShim {
386386
* "parse" a mixin definition into a map of properties and values
387387
* cssTextToMap('border: 2px solid black') -> ('border', '2px solid black')
388388
* @param {string} text
389+
* @param {boolean=} replaceInitialOrInherit
389390
* @return {!Object<string, string>}
390391
*/
391-
_cssTextToMap(text) {
392+
_cssTextToMap(text, replaceInitialOrInherit = false) {
392393
let props = text.split(';');
393394
let property, value;
394395
let out = {};
@@ -400,7 +401,10 @@ class ApplyShim {
400401
if (sp.length > 1) {
401402
property = sp[0].trim();
402403
// some properties may have ':' in the value, like data urls
403-
value = this._replaceInitialOrInherit(property, sp.slice(1).join(':'));
404+
value = sp.slice(1).join(':');
405+
if (replaceInitialOrInherit) {
406+
value = this._replaceInitialOrInherit(property, value);
407+
}
404408
out[property] = value;
405409
}
406410
}
@@ -445,7 +449,10 @@ class ApplyShim {
445449
}
446450
let mixinAsProperties = this._consumeCssProperties('' + valueMixin, rule);
447451
let prefix = matchText.slice(0, matchText.indexOf('--'));
448-
let mixinValues = this._cssTextToMap(mixinAsProperties);
452+
// `initial` and `inherit` as properties in a map should be replaced because
453+
// these keywords are eagerly evaluated when the mixin becomes CSS Custom Properties,
454+
// and would set the variable value, rather than carry the keyword to the `var()` usage.
455+
let mixinValues = this._cssTextToMap(mixinAsProperties, true);
449456
let combinedProps = mixinValues;
450457
let mixinEntry = this._map.get(propertyName);
451458
let oldProps = mixinEntry && mixinEntry.properties;

tests/apply-shim.html

+15
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@
6464
border: 1px dotted orange;
6565
@apply --mixin;
6666
}
67+
span {
68+
border: inherit;
69+
@apply --mixin;
70+
}
71+
span {
72+
border: initial;
73+
@apply --mixin;
74+
}
6775
</style>
6876
</template>
6977

@@ -204,6 +212,13 @@
204212
var application = ast.rules[1];
205213
assert.match(application.cssText, /border:\s*var\(--mixin_-_border,\s*1px dotted orange\)/);
206214
});
215+
216+
test('inherit and initial default values are preserved', function() {
217+
var application = ast.rules[2];
218+
assert.match(application.cssText, /border:\s*var\(--mixin_-_border,\s*inherit\)/);
219+
application = ast.rules[3];
220+
assert.match(application.cssText, /border:\s*var\(--mixin_-_border,\s*initial\)/);
221+
});
207222
});
208223

209224
suite('override', function() {

tests/no-scopingshim/apply-shim.html

+15
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@
5555
border: 1px dotted orange;
5656
@apply --mixin;
5757
}
58+
span {
59+
border: inherit;
60+
@apply --mixin;
61+
}
62+
span {
63+
border: initial;
64+
@apply --mixin;
65+
}
5866
</style>
5967
</template>
6068

@@ -184,6 +192,13 @@
184192
var application = ast.rules[1];
185193
assert.match(application.cssText, /border:\s*var\(--mixin_-_border,\s*1px dotted orange\)/);
186194
});
195+
196+
test('inherit and initial default values are preserved', function () {
197+
var application = ast.rules[2];
198+
assert.match(application.cssText, /border:\s*var\(--mixin_-_border,\s*inherit\)/);
199+
application = ast.rules[3];
200+
assert.match(application.cssText, /border:\s*var\(--mixin_-_border,\s*initial\)/);
201+
});
187202
});
188203

189204
suite('override', function() {

0 commit comments

Comments
 (0)