Skip to content

Commit 62b3b1b

Browse files
authored
Merge pull request #115 from jsdom/feature/calc-simple
Implement basic calc
2 parents b4c49d6 + d8048a9 commit 62b3b1b

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

lib/CSSStyleDeclaration.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -547,4 +547,10 @@ describe('CSSStyleDeclaration', () => {
547547
expect(style.getPropertyValue('--foo')).toEqual('');
548548
expect(style.getPropertyValue('--fOo')).toEqual('purple');
549549
});
550+
551+
test('supports calc', () => {
552+
const style = new CSSStyleDeclaration();
553+
style.setProperty('width', 'calc(100% - 100px)');
554+
expect(style.getPropertyValue('width')).toEqual('calc(100% - 100px)');
555+
});
550556
});

lib/parsers.js

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ exports.TYPES = {
1818
ANGLE: 8,
1919
KEYWORD: 9,
2020
NULL_OR_EMPTY_STR: 10,
21+
CALC: 11,
2122
};
2223

2324
// rough regular expressions
@@ -30,6 +31,7 @@ var stringRegEx = /^("[^"]*"|'[^']*')$/;
3031
var colorRegEx1 = /^#([0-9a-fA-F]{3,4}){1,2}$/;
3132
var colorRegEx2 = /^rgb\(([^)]*)\)$/;
3233
var colorRegEx3 = /^rgba\(([^)]*)\)$/;
34+
var calcRegEx = /^calc\(([^)]*)\)$/;
3335
var colorRegEx4 = /^hsla?\(\s*(-?\d+|-?\d*.\d+)\s*,\s*(-?\d+|-?\d*.\d+)%\s*,\s*(-?\d+|-?\d*.\d+)%\s*(,\s*(-?\d+|-?\d*.\d+)\s*)?\)/;
3436
var angleRegEx = /^([-+]?[0-9]*\.?[0-9]+)(deg|grad|rad)$/;
3537

@@ -61,6 +63,9 @@ exports.valueType = function valueType(val) {
6163
if (urlRegEx.test(val)) {
6264
return exports.TYPES.URL;
6365
}
66+
if (calcRegEx.test(val)) {
67+
return exports.TYPES.CALC;
68+
}
6469
if (stringRegEx.test(val)) {
6570
return exports.TYPES.STRING;
6671
}
@@ -70,6 +75,7 @@ exports.valueType = function valueType(val) {
7075
if (colorRegEx1.test(val)) {
7176
return exports.TYPES.COLOR;
7277
}
78+
7379
var res = colorRegEx2.exec(val);
7480
var parts;
7581
if (res !== null) {
@@ -201,6 +207,11 @@ exports.parsePercent = function parsePercent(val) {
201207

202208
// either a length or a percent
203209
exports.parseMeasurement = function parseMeasurement(val) {
210+
var type = exports.valueType(val);
211+
if (type === exports.TYPES.CALC) {
212+
return val;
213+
}
214+
204215
var length = exports.parseLength(val);
205216
if (length !== undefined) {
206217
return length;

lib/parsers.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ describe('valueType', () => {
6565

6666
expect(output).toEqual(parsers.TYPES.LENGTH);
6767
});
68+
69+
it('returns calc from calc(100px * 2)', () => {
70+
let input = 'calc(100px * 2)';
71+
let output = parsers.valueType(input);
72+
73+
expect(output).toEqual(parsers.TYPES.CALC);
74+
});
6875
});
6976
describe('parseInteger', () => {
7077
it.todo('test');

0 commit comments

Comments
 (0)