Skip to content

Commit dcfbbaf

Browse files
asamuzaKdomenic
authored andcommitted
Improve getPropertyDescriptor() fallback code
getPropertyDescriptor() is used for the generic getters and setters that do not have implementations in properties/*.js. Change from just parsing CSS and setting it as-is, to handle properties more correctly by using the AST types.
1 parent c06fbab commit dcfbbaf

1 file changed

Lines changed: 49 additions & 13 deletions

File tree

lib/utils/propertyDescriptors.js

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,54 @@
22

33
const parsers = require("../parsers");
44

5-
exports.getPropertyDescriptor = function getPropertyDescriptor(property) {
6-
return {
7-
set(v) {
8-
v = parsers.parsePropertyValue(property, v, {
9-
globalObject: this._global
5+
const { AST_TYPES } = parsers;
6+
7+
const getPropertyDescriptor = (property) => ({
8+
set(v) {
9+
const value = parsers.prepareValue(v, this._global);
10+
if (parsers.hasVarFunc(value)) {
11+
this._setProperty(property, value);
12+
} else {
13+
const parsedValue = parsers.parsePropertyValue(property, v, {
14+
globalObject: this._global,
15+
inArray: true
1016
});
11-
this._setProperty(property, v);
12-
},
13-
get() {
14-
return this.getPropertyValue(property);
15-
},
16-
enumerable: true,
17-
configurable: true
18-
};
17+
if (Array.isArray(parsedValue)) {
18+
if (parsedValue.length === 1) {
19+
const [{ name, type, value: itemValue }] = parsedValue;
20+
switch (type) {
21+
case AST_TYPES.CALC: {
22+
this._setProperty(property, `${name}(${itemValue})`);
23+
break;
24+
}
25+
case AST_TYPES.GLOBAL_KEYWORD:
26+
case AST_TYPES.IDENTIFIER: {
27+
// Set the normalized name for keywords or identifiers.
28+
this._setProperty(property, name);
29+
break;
30+
}
31+
default: {
32+
// Set the prepared value for Dimension, Function, etc.
33+
this._setProperty(property, value);
34+
}
35+
}
36+
} else {
37+
// Set the prepared value for lists containing multiple values.
38+
this._setProperty(property, value);
39+
}
40+
} else if (typeof parsedValue === "string") {
41+
// Empty string.
42+
this._setProperty(property, parsedValue);
43+
}
44+
}
45+
},
46+
get() {
47+
return this.getPropertyValue(property);
48+
},
49+
enumerable: true,
50+
configurable: true
51+
});
52+
53+
module.exports = {
54+
getPropertyDescriptor
1955
};

0 commit comments

Comments
 (0)