Skip to content

Commit 852e70a

Browse files
committed
fix: clip args with calc() and separated by comma
1 parent f46dc58 commit 852e70a

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

lib/CSSStyleDeclaration.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,11 @@ describe('properties', () => {
510510
expect(style.length).toEqual(1);
511511
expect(style.cssText).toEqual('clip: rect(0px, 3em, 2pt, 50%);');
512512
});
513+
test('calc()', () => {
514+
const style = new CSSStyleDeclaration();
515+
style.clip = 'rect(calc(5px + 5px) calc(20px - 10px) calc(5px * 2) calc(20px / 2))';
516+
expect(style.clip).toEqual('rect(calc(10px), calc(10px), calc(10px), calc(10px))');
517+
});
513518
});
514519
describe('flex', () => {
515520
test('valid', () => {

lib/parsers.js

Lines changed: 1 addition & 0 deletions
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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
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
var parse = function(val) {
815
if (val === '') {
@@ -12,11 +19,11 @@ var parse = function(val) {
1219
if (keyword !== undefined) {
1320
return keyword;
1421
}
15-
var matches = val.match(shape_regex);
22+
var matches = val.match(shapeRegEx);
1623
if (!matches) {
1724
return undefined;
1825
}
19-
var parts = matches[1].split(/\s*,\s*/);
26+
let [parts] = splitTokens(matches[1], separatorRegEx);
2027
if (parts.length !== 4) {
2128
return undefined;
2229
}

0 commit comments

Comments
 (0)