Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/js/css-calc-var.ts
Original file line number Diff line number Diff line change
Expand Up @@ -713,21 +713,24 @@ const resolveNode = (node: CalcASTNode[], isRoot: boolean): string => {
flatItems.push(item);
}
}
const hasComma = flatItems.includes(',');
if (isRoot) {
if (flatItems.length >= TRIA) {
if (flatItems.length >= TRIA && !hasComma) {
return sortCalcValues(flatItems, true);
}
const joined = flatItems.join('');
return joined.startsWith('calc(') ? joined : `calc(${joined})`;
const joined = flatItems.join('').replace(/,\s*/g, ', ');
return joined.startsWith('calc(') || /^[a-z-]+\(/.test(joined)
? joined
: `calc(${joined})`;
}
if (flatItems.length >= TRIA) {
if (flatItems.length >= TRIA && !hasComma) {
let serialized = sortCalcValues(flatItems, false);
if (REG_FN_VAR_START.test(serialized)) {
serialized = calc(serialized, { toCanonicalUnits: true });
}
return serialized;
}
return flatItems.join('');
return flatItems.join('').replace(/,\s*/g, ', ');
};

/**
Expand Down Expand Up @@ -810,12 +813,14 @@ export const serializeCalc = (value: string, opt: Options = {}): string => {
flatItems.push(item);
}
}
if (flatItems.length >= TRIA) {
const hasComma = flatItems.includes(',');
if (flatItems.length >= TRIA && !hasComma) {
serializedCalc = sortCalcValues(flatItems, true);
} else {
const firstItem = flatItems[0] || '';
serializedCalc =
isString(firstItem) && firstItem.startsWith('calc(')
isString(firstItem) &&
(firstItem.startsWith('calc(') || /^[a-z-]+\(/.test(firstItem))
? firstItem
: `calc(${firstItem})`;
}
Expand Down
14 changes: 14 additions & 0 deletions test/css-calc-var.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,20 @@ describe('CSS calc()', () => {
});
assert.strictEqual(res2, 'calc(0.666667)', 'result');
});

it('should get value', () => {
const res = func('min(180px, calc(80vw - 24px))', {
format: 'specifiedValue'
});
assert.strictEqual(res, 'min(180px, calc(80vw - 24px))', 'result');
});

it('should get value', () => {
const res = func('max(10px, calc(1vw + 1px))', {
format: 'specifiedValue'
});
assert.strictEqual(res, 'max(10px, calc(1vw + 1px))', 'result');
});
});

describe('serialize calc edge cases', () => {
Expand Down
Loading