Skip to content

Commit a8dfd17

Browse files
authored
[FIX] cssVariables: Don't use CSS vars within @font-face directives (#257)
Browsers do not seem to support usage of custom CSS properties (aka CSS variables) within @font-face directives. The skeleton style-sheet should therefore not contain a reference to the variables but just use the actual value provided by the LESS variable. BCP: 2270189546
1 parent cbdc91a commit a8dfd17

12 files changed

+116
-9
lines changed

lib/plugin/css-variables-collector.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const CSSVariablesCollectorPlugin = module.exports = function(config) {
1111
this.ruleStack = [];
1212
this.mixinStack = [];
1313
this.parenStack = [];
14+
this.fontFaceDirectiveStack = [];
1415
};
1516

1617
CSSVariablesCollectorPlugin.prototype = {
@@ -20,8 +21,8 @@ CSSVariablesCollectorPlugin.prototype = {
2021

2122
isReplacing: true,
2223

23-
_isInMixinOrParen() {
24-
return this.mixinStack.length > 0 || this.parenStack.length > 0;
24+
_isInMixinOrParenOrFontFaceDirective() {
25+
return this.mixinStack.length > 0 || this.parenStack.length > 0 || this.fontFaceDirectiveStack.length > 0;
2526
},
2627

2728
_isVarInRule() {
@@ -39,7 +40,7 @@ CSSVariablesCollectorPlugin.prototype = {
3940
},
4041

4142
_isRelevant() {
42-
return !this._isInMixinOrParen() && this._isVarInRule();
43+
return !this._isInMixinOrParenOrFontFaceDirective() && this._isVarInRule();
4344
},
4445

4546
toLessVariables(varsOverride) {
@@ -153,7 +154,7 @@ CSSVariablesCollectorPlugin.prototype = {
153154
visitRule(node, visitArgs) {
154155
// check rule for being a variable declaration
155156
const isVarDeclaration = typeof node.name === "string" && node.name.startsWith("@");
156-
if (!this._isInMixinOrParen() && isVarDeclaration) {
157+
if (!this._isInMixinOrParenOrFontFaceDirective() && isVarDeclaration) {
157158
// add the variable declaration to the list of vars
158159
const varName = node.name.substr(1);
159160
const isVarInLib = this._isVarInLibrary({
@@ -199,6 +200,22 @@ CSSVariablesCollectorPlugin.prototype = {
199200
return node;
200201
},
201202

203+
visitDirective(node, visitArgs) {
204+
// store the @font-face directive context
205+
if (node.name === "@font-face") {
206+
this.fontFaceDirectiveStack.push(node);
207+
}
208+
return node;
209+
},
210+
211+
visitDirectiveOut(node) {
212+
// remove @font-face directive context
213+
if (node.name === "@font-face") {
214+
this.fontFaceDirectiveStack.pop();
215+
}
216+
return node;
217+
},
218+
202219
visitUrl(node, visitArgs) {
203220
// we mark the less variables which should be updated after eval
204221
// => strangewise less variables with "none" values are also urls

test/expected/simple/test-RTL.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,17 @@
22
float: right;
33
color: #ff0000;
44
}
5+
@keyframes myKeyframes {
6+
0%,
7+
80%,
8+
100% {
9+
transform: scale(1);
10+
}
11+
40% {
12+
transform: scale(2);
13+
}
14+
}
15+
@font-face {
16+
font-family: "MyFont";
17+
src: url("MyFont.woff2") format("woff2");
18+
}

test/expected/simple/test-RTL.min.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
.myRule{float:right;color:#f00}
1+
.myRule{float:right;color:#f00}@keyframes myKeyframes{0%,80%,100%{transform:scale(1)}40%{transform:scale(2)}}@font-face{font-family:"MyFont";src:url("MyFont.woff2") format("woff2")}

test/expected/simple/test-cssvars-skeleton.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,17 @@
22
float: left;
33
color: var(--myColor);
44
}
5+
@keyframes myKeyframes {
6+
0%,
7+
80%,
8+
100% {
9+
transform: var(--myScale1);
10+
}
11+
40% {
12+
transform: var(--myScale2);
13+
}
14+
}
15+
@font-face {
16+
font-family: "MyFont";
17+
src: url("MyFont.woff2") format("woff2");
18+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
:root {
22
--myColor: #ff0000;
3+
--myFontFamily: "MyFont";
4+
--myFontUrl: url("MyFont.woff2");
5+
--myScale1: scale(1);
6+
--myScale2: scale(2);
37
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
@myColor: #ff0000;
2+
@myFontFamily: "MyFont";
3+
@myFontUrl: url("MyFont.woff2");
4+
@myScale1: scale(1);
5+
@myScale2: scale(2);
26

37
:root {
48
--myColor: @myColor;
9+
--myFontFamily: @myFontFamily;
10+
--myFontUrl: @myFontUrl;
11+
--myScale1: @myScale1;
12+
--myScale2: @myScale2;
513
}

test/expected/simple/test-inline-parameters.css

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22
float: left;
33
color: #ff0000;
44
}
5+
@keyframes myKeyframes {
6+
0%,
7+
80%,
8+
100% {
9+
transform: scale(1);
10+
}
11+
40% {
12+
transform: scale(2);
13+
}
14+
}
15+
@font-face {
16+
font-family: "MyFont";
17+
src: url("MyFont.woff2") format("woff2");
18+
}
519

620
/* Inline theming parameters */
7-
#sap-ui-theme-foo\.bar{background-image:url('data:text/plain;utf-8,%7B%22myColor%22%3A%22%23ff0000%22%7D')}
21+
#sap-ui-theme-foo\.bar{background-image:url('data:text/plain;utf-8,%7B%22myColor%22%3A%22%23ff0000%22%2C%22myFontFamily%22%3A%22%5C%22MyFont%5C%22%22%2C%22myFontUrl%22%3A%22url(%5C%22MyFont.woff2%5C%22)%22%2C%22myScale1%22%3A%22scale(1)%22%2C%22myScale2%22%3A%22scale(2)%22%7D')}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
{
2-
"myColor": "#ff0000"
2+
"myColor": "#ff0000",
3+
"myFontFamily": "\"MyFont\"",
4+
"myFontUrl": "url(\"MyFont.woff2\")",
5+
"myScale1": "scale(1)",
6+
"myScale2": "scale(2)"
37
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "myColor" : "#f00" }
1+
{ "myColor" : "#f00", "myFontFamily": "\"MyFont\"", "myFontUrl": "url(\"MyFont.woff2\")", "myScale1": "scale(1)", "myScale2": "scale(2)" }

test/expected/simple/test.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,17 @@
22
float: left;
33
color: #ff0000;
44
}
5+
@keyframes myKeyframes {
6+
0%,
7+
80%,
8+
100% {
9+
transform: scale(1);
10+
}
11+
40% {
12+
transform: scale(2);
13+
}
14+
}
15+
@font-face {
16+
font-family: "MyFont";
17+
src: url("MyFont.woff2") format("woff2");
18+
}

0 commit comments

Comments
 (0)