Skip to content

Commit 5ed7bfc

Browse files
committed
fix(issue:3777) improve custom property initial
* Improves CSS custom property initial value support, including nesting scenarios to better address issue less#3777.
1 parent fe7ab14 commit 5ed7bfc

File tree

7 files changed

+66
-9
lines changed

7 files changed

+66
-9
lines changed

packages/less/src/less/functions/math-helper.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import Dimension from '../tree/dimension';
44

55
const MathHelper = (fn, unit, n) => {
66
if (n instanceof Call && n.name === 'var') {
7-
if (n.args && n.args.length === 1) {
8-
return new Call(fn.name, [new CustomProperty(n.args[0].toCSS(), n._index, n._fileInfo)], n._index, n._fileInfo);
7+
if (n.args && n.args.length >= 1) {
8+
return new Call(fn.name, [new CustomProperty(n.args[0].toCSS(), n.args[1] ? n.args[1].toCSS() : null, n._index, n._fileInfo)], n._index, n._fileInfo);
99
} else {
1010
throw { type: 'Argument', message: 'var must contain one expression' };
1111
}

packages/less/src/less/functions/number.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Dimension from '../tree/dimension';
22
import Anonymous from '../tree/anonymous';
33
import mathHelper from './math-helper.js';
4+
import Call from '../tree/call';
45

56
const minMax = function (isMin, args) {
67
args = Array.prototype.slice.call(args);
@@ -78,6 +79,10 @@ export default {
7879
return new Dimension(a.value % b.value, a.unit);
7980
},
8081
pow: function(x, y) {
82+
if (x instanceof Call || y instanceof Call) {
83+
// Must return Node
84+
return new Anonymous(x.toCSS() + ', ' + y.toCSS());
85+
}
8186
if (typeof x === 'number' && typeof y === 'number') {
8287
x = new Dimension(x);
8388
y = new Dimension(y);

packages/less/src/less/tree/call.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Node from './node';
22
import Anonymous from './anonymous';
33
import FunctionCaller from '../functions/function-caller';
4+
import CustomProperty from './custom-property';
45

56
//
67
// A function call node.
@@ -98,11 +99,34 @@ Call.prototype = Object.assign(new Node(), {
9899

99100
genCSS(context, output) {
100101
output.add(`${this.name}(`, this.fileInfo(), this.getIndex());
102+
let isCustomProperty = false;
103+
let customExpressionCount = 0;
101104

102105
for (let i = 0; i < this.args.length; i++) {
103106
this.args[i].genCSS(context, output);
107+
108+
if (this.args[i] instanceof CustomProperty
109+
|| ((i + 2 < this.args.length && this.args[i + 2] instanceof CustomProperty))) {
110+
if (isCustomProperty) {
111+
isCustomProperty = false;
112+
} else {
113+
isCustomProperty = true;
114+
customExpressionCount = 1;
115+
}
116+
}
117+
118+
if (customExpressionCount === 3) {
119+
isCustomProperty = false;
120+
customExpressionCount = 0;
121+
}
122+
104123
if (i + 1 < this.args.length) {
105-
output.add(', ');
124+
if (!isCustomProperty) {
125+
output.add(', ');
126+
} else {
127+
output.add(' ');
128+
customExpressionCount++;
129+
}
106130
}
107131
}
108132

packages/less/src/less/tree/custom-property.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/* eslint-disable no-prototype-builtins */
22
import Node from './node';
33

4-
const CustomProperty = function (name, index, currentFileInfo) {
4+
const CustomProperty = function (name, initialValue, index, currentFileInfo) {
55
this.name = name;
6+
this.initialValue = initialValue;
67
this._index = index;
78
this._fileInfo = currentFileInfo;
89
};
@@ -11,7 +12,7 @@ CustomProperty.prototype = Object.assign(new Node(), {
1112
type: 'CustomProperty',
1213

1314
genCSS: function (context, output) {
14-
output.add('var(' + this.name + ')');
15+
output.add('var(' + this.name + (this.initialValue ? ', ' + this.initialValue : '') + ')');
1516
}
1617
});
1718

packages/less/src/less/tree/operation.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import Color from './color';
33
import Dimension from './dimension';
44
import * as Constants from '../constants';
55
import Call from './call';
6+
import CustomProperty from './custom-property';
7+
import Anonymous from './anonymous';
68
const MATH = Constants.Math;
79

810

@@ -38,6 +40,12 @@ Operation.prototype = Object.assign(new Node(), {
3840
if (b instanceof Dimension && a instanceof Color) {
3941
b = b.toColor();
4042
}
43+
if (a instanceof Dimension && b instanceof CustomProperty) {
44+
return [a, new Anonymous(op), b];
45+
}
46+
if (b instanceof Dimension && a instanceof CustomProperty) {
47+
return [a, new Anonymous(op), b];
48+
}
4149
if (!a.operate || !b.operate) {
4250
if (
4351
(a instanceof Operation || b instanceof Operation)
@@ -78,7 +86,7 @@ Operation.prototype = Object.assign(new Node(), {
7886
},
7987

8088
evalVariable: function (context, operand) {
81-
if (operand.name === 'var' && operand.args.length === 1) {
89+
if (operand.name === 'var' && operand.args.length >= 1) {
8290
var varName = operand.args[0].toCSS();
8391
var variable = this.find(context.frames, function (frame) {
8492
var v = frame.variable(varName);
@@ -92,7 +100,7 @@ Operation.prototype = Object.assign(new Node(), {
92100
return (new Call('_SELF', [v.value])).eval(context);
93101
}
94102
else {
95-
return v.value.eval(context);
103+
return new CustomProperty(v.name, operand.args[1] ? operand.args[1].toCSS() : null, 0, {});
96104
}
97105
}
98106
});

packages/test-data/css/_main/custom-property.css

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44
}
55
.test2 {
66
--some-var: 5%;
7-
prop: min(95%, 10px);
7+
prop: min(100% - var(--some-var), 10px);
88
}
99
.test3 {
1010
--some-var: 55%;
11-
prop: min(60%, 15px);
11+
prop: min(var(--some-var) + 5%, 15px);
1212
}
1313
.test4 {
1414
color: red;
1515
--other-color: green;
1616
}
17+
.test5 {
18+
--font-level: 5;
19+
prop: var(--font-ratio-min), var(--font-level, 0);
20+
}
21+
.test6 {
22+
--some-var: 5px;
23+
prop: min(100% - var(--some-var, var(--somevar, 0)), 10px);
24+
}

packages/test-data/less/_main/custom-property.less

+11
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,14 @@
1717
color: red;
1818
--other-color: green;
1919
}
20+
21+
.test5 {
22+
--font-level: 5;
23+
prop: pow(var(--font-ratio-min), var(--font-level, 0))
24+
}
25+
26+
27+
.test6 {
28+
--some-var: 5px;
29+
prop: min(100% - var(--some-var, var(--somevar, 0)), 10px);
30+
}

0 commit comments

Comments
 (0)