Skip to content

Commit 961396e

Browse files
committed
fix: clip args with calc() and separated by space
1 parent 0fac94e commit 961396e

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

lib/CSSStyleDeclaration.test.js

+5
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,11 @@ describe('properties', () => {
806806
expect(style.cssText).toBe('clip: rect(0px, 3em, 2pt, 50%);');
807807
expect(style.length).toBe(1);
808808
});
809+
test('calc()', () => {
810+
const style = new CSSStyleDeclaration();
811+
style.clip = 'rect(calc(5px + 5px) calc(20px - 10px) calc(5px * 2) calc(20px / 2))';
812+
expect(style.clip).toBe('rect(calc(10px), calc(10px), calc(10px), calc(10px))');
813+
});
809814
});
810815
describe('flex', () => {
811816
test('shorthand propagates to longhands', () => {

lib/parsers.js

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const whitespaceRegEx = new RegExp(`^${whitespace}$`);
7474
const trailingWhitespaceRegEx = new RegExp(`.*${whitespace}$`);
7575

7676
exports.ws = ws;
77+
exports.whitespace = whitespace;
7778

7879
/**
7980
* CSS types

lib/properties/clip.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
'use strict';
22

3-
const { parseKeyword, parseLengthOrPercentage } = require('../parsers');
3+
const {
4+
parseKeyword,
5+
parseLengthOrPercentage,
6+
splitTokens,
7+
whitespace,
8+
ws,
9+
} = require('../parsers');
410

5-
var shape_regex = /^rect\((.*)\)$/i;
11+
const shapeRegEx = new RegExp(`^rect\\(${ws}(.+)${ws}\\)$`, 'i');
12+
const separatorRegEx = new RegExp(`,|${whitespace}`);
613

714
function parse(val) {
815
const keyword = parseKeyword(val, ['auto']);
916
if (keyword !== null) {
1017
return keyword;
1118
}
12-
var matches = val.match(shape_regex);
19+
const matches = val.match(shapeRegEx);
1320
if (!matches) {
1421
return null;
1522
}
16-
var parts = matches[1].split(/\s*,\s*/);
23+
let [parts] = splitTokens(matches[1], separatorRegEx);
1724
if (parts.length !== 4) {
1825
return null;
1926
}

0 commit comments

Comments
 (0)