Skip to content

Commit cc3a6af

Browse files
domenicclaude
andauthored
Remove toObject option (use css-tree AST directly)
Work directly with css-tree's List objects instead of converting to plain arrays via toPlainObject(). This does not give significant performance improvements, but simplifies the code a bit. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 1257434 commit cc3a6af

4 files changed

Lines changed: 13 additions & 29 deletions

File tree

lib/CSSStyleDeclaration.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,7 @@ class CSSStyleDeclaration {
127127
}
128128
try {
129129
this._updating = true;
130-
const valueObj = parseCSS(
131-
text,
132-
{
133-
context: "declarationList",
134-
parseValue: false
135-
},
136-
true
137-
);
130+
const valueObj = parseCSS(text, { context: "declarationList", parseValue: false });
138131
if (valueObj?.children) {
139132
const properties = new Map();
140133
let shouldSkipNext = false;

lib/parsers.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,9 @@ const hasCalcFunc = (val) => {
142142
*
143143
* @param {string} val - The CSS string to parse.
144144
* @param {object} opt - The options for parsing.
145-
* @param {boolean} [toObject=false] - Whether to return a plain object.
146-
* @returns {object} The AST or a plain object.
145+
* @returns {object} The AST.
147146
*/
148-
const parseCSS = (val, opt, toObject = false) => {
149-
val = prepareValue(val);
150-
const ast = cssTree.parse(val, opt);
151-
if (toObject) {
152-
return cssTree.toPlainObject(ast);
153-
}
154-
return ast;
155-
};
147+
const parseCSS = (val, opt) => cssTree.parse(prepareValue(val), opt);
156148

157149
/**
158150
* Checks if the value is a valid property value.
@@ -212,13 +204,12 @@ const resolveCalc = (val, opt = { format: "specifiedValue" }) => {
212204
if (typeof cachedValue === "string") {
213205
return cachedValue;
214206
}
215-
const obj = parseCSS(val, { context: "value" }, true);
216-
if (!obj?.children) {
207+
const ast = parseCSS(val, { context: "value" });
208+
if (!ast?.children) {
217209
return;
218210
}
219-
const { children: items } = obj;
220211
const values = [];
221-
for (const item of items) {
212+
for (const item of ast.children) {
222213
const { type: itemType, name: itemName, value: itemValue } = item;
223214
if (itemType === AST_TYPES.FUNCTION) {
224215
const value = cssTree
@@ -301,8 +292,8 @@ const parsePropertyValue = (prop, val, opt = {}) => {
301292
if (error || !matched) {
302293
parsedValue = false;
303294
} else {
304-
const obj = cssTree.toPlainObject(ast);
305-
const items = obj.children;
295+
const items = ast.children;
296+
const itemCount = items.size;
306297
const values = [];
307298
for (const item of items) {
308299
const { children, name, type, value, unit } = item;
@@ -320,12 +311,12 @@ const parsePropertyValue = (prop, val, opt = {}) => {
320311
.generate(item)
321312
.replace(/\)(?!\)|\s|,)/g, ") ")
322313
.trim();
323-
const raw = items.length === 1 ? val : css;
314+
const raw = itemCount === 1 ? val : css;
324315
// Remove "${name}(" from the start and ")" from the end
325316
const itemValue = raw.slice(name.length + 1, -1).trim();
326317
if (name === "calc") {
327-
if (children.length === 1) {
328-
const [child] = children;
318+
if (children.size === 1) {
319+
const child = children.first;
329320
if (child.type === AST_TYPES.NUMBER) {
330321
values.push({
331322
type: AST_TYPES.CALC,

lib/properties/clip.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports.parse = (v, opt = {}) => {
2424
});
2525
const parsedValues = [];
2626
for (const item of values) {
27-
const parsedValue = parsers.parseCSS(item, { context: "value" }, true);
27+
const parsedValue = parsers.parseCSS(item, { context: "value" });
2828
const val = parsers.resolveNumericValue(parsedValue.children, {
2929
type: "length"
3030
});

test/parsers.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ describe("parseCSS", () => {
12091209
context: "declarationList",
12101210
parseValue: false
12111211
};
1212-
const output = parsers.parseCSS(input, opt, true);
1212+
const output = parsers.parseCSS(input, opt);
12131213
const [
12141214
{
12151215
important,

0 commit comments

Comments
 (0)