Skip to content

Commit

Permalink
feat: computed calc()
Browse files Browse the repository at this point in the history
  • Loading branch information
cdoublev committed May 24, 2021
1 parent ede1584 commit 38a54db
Show file tree
Hide file tree
Showing 15 changed files with 409 additions and 126 deletions.
423 changes: 328 additions & 95 deletions lib/parsers.js

Large diffs are not rendered by default.

56 changes: 53 additions & 3 deletions lib/parsers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ describe('parseLength', () => {
});
it.todo('more tests');
});
describe('parsePercent', () => {
describe('parsePercentage', () => {
it('works with calc', () => {
expect(parsers.parsePercent('calc(1% + 1%)')).toBe('calc(2%)');
expect(parsers.parsePercentage('calc(1% + 1%)')).toBe('calc(2%)');
});
it.todo('more tests');
});
describe('parseMeasurement', () => {
describe('parseLengthOrPercentage', () => {
it.todo('test');
});
describe('parseAngle', () => {
Expand Down Expand Up @@ -167,3 +167,53 @@ describe('subImplicitSetter', () => {
describe('camelToDashed', () => {
it.todo('test');
});
describe('splitTokens', () => {
it('should split value components separated by space', () => {
expect(parsers.splitTokens('1px solid red')).toEqual([['1px', 'solid', 'red'], [' ', ' ']]);
});
it('should split value components separated by space and forward slash', () => {
expect(parsers.splitTokens('url(bg.jpq) center / 50%', /[ /]/)).toEqual([
['url(bg.jpq)', 'center', '50%'],
[' ', ' / '],
]);
});
it('should split a list of values', () => {
expect(parsers.splitTokens('1px 1px black, 2px 2px silver', /,/)).toEqual([
['1px 1px black', '2px 2px silver'],
[', '],
]);
});
it('should split color function arguments', () => {
const separators = /[,/ ]/;
expect(parsers.splitTokens('0,0,0', separators)).toEqual([['0', '0', '0'], [',', ',']]);
expect(parsers.splitTokens('0,1%,2%', separators)).toEqual([['0', '1%', '2%'], [',', ',']]);
expect(parsers.splitTokens('0 , 0 , 0', separators)).toEqual([
['0', '0', '0'],
[' , ', ' , '],
]);
expect(parsers.splitTokens('0deg, 1%, 2%', separators)).toEqual([
['0deg', '1%', '2%'],
[', ', ', '],
]);
expect(parsers.splitTokens('0deg 1% / 2%', separators)).toEqual([
['0deg', '1%', '2%'],
[' ', ' / '],
]);
});
it('should split nested function arguments', () => {
expect(parsers.splitTokens('calc(45deg * 2) to left, rgb(255, 0, 0), cyan', /,/)).toEqual([
['calc(45deg * 2) to left', 'rgb(255, 0, 0)', 'cyan'],
[', ', ', '],
]);
expect(parsers.splitTokens('calc(1 + 1) + var(--number, calc(1 + 1)) * 1', /[+*]/)).toEqual([
['calc(1 + 1)', 'var(--number, calc(1 + 1))', '1'],
[' + ', ' * '],
]);
});
it('should split function with empty arguments', () => {
expect(parsers.splitTokens(', , ,, 1%,', /,/)).toEqual([
['', '', '', '', '1%', ''],
[', ', ', ', ',', ', ', ','],
]);
});
});
6 changes: 3 additions & 3 deletions lib/properties/backgroundPosition.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { parseKeyword, parseMeasurement } = require('../parsers');
const { parseKeyword, parseLengthOrPercentage } = require('../parsers');

var valid_keywords = ['top', 'center', 'bottom', 'left', 'right'];

Expand All @@ -13,9 +13,9 @@ function parse(v) {
return undefined;
}
if (parts.length === 1) {
return parseMeasurement(parts[0]) || parseKeyword(parts[0], valid_keywords);
return parseLengthOrPercentage(parts[0]) || parseKeyword(parts[0], valid_keywords);
}
if (parseMeasurement(parts[0]) !== undefined && parseMeasurement(parts[1]) !== undefined) {
if (parseLengthOrPercentage(parts[0]) !== undefined && parseLengthOrPercentage(parts[1]) !== undefined) {
return v;
}
if (
Expand Down
4 changes: 2 additions & 2 deletions lib/properties/bottom.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

var parseMeasurement = require('../parsers').parseMeasurement;
const { parseLengthOrPercentage } = require('../parsers');

module.exports.definition = {
set: function(v) {
this._setProperty('bottom', parseMeasurement(v));
this._setProperty('bottom', parseLengthOrPercentage(v));
},
get: function() {
return this.getPropertyValue('bottom');
Expand Down
4 changes: 2 additions & 2 deletions lib/properties/clip.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { parseKeyword, parseMeasurement } = require('../parsers');
const { parseKeyword, parseLengthOrPercentage } = require('../parsers');

var shape_regex = /^rect\((.*)\)$/i;

Expand All @@ -21,7 +21,7 @@ var parse = function(val) {
return undefined;
}
var valid = parts.every(function(part, index) {
var measurement = parseMeasurement(part.toLowerCase());
var measurement = parseLengthOrPercentage(part);
parts[index] = measurement;
return measurement !== undefined;
});
Expand Down
4 changes: 2 additions & 2 deletions lib/properties/flexBasis.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

const { parseKeyword, parseMeasurement } = require('../parsers');
const { parseKeyword, parseLengthOrPercentage } = require('../parsers');

function parse(v) {
return parseKeyword(v, ['auto']) || parseMeasurement(v);
return parseKeyword(v, ['auto']) || parseLengthOrPercentage(v);
}

module.exports.parse = parse;
Expand Down
4 changes: 2 additions & 2 deletions lib/properties/fontSize.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { parseKeyword, parseMeasurement } = require('../parsers');
const { parseKeyword, parseLengthOrPercentage } = require('../parsers');

const sizes = [
// Absolute
Expand All @@ -17,7 +17,7 @@ const sizes = [
];

function parse(v) {
return parseKeyword(v, sizes) || parseMeasurement(v);
return parseKeyword(v, sizes) || parseLengthOrPercentage(v);
}

module.exports.parse = parse;
Expand Down
4 changes: 2 additions & 2 deletions lib/properties/height.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

const { parseKeyword, parseMeasurement } = require('../parsers');
const { parseKeyword, parseLengthOrPercentage } = require('../parsers');

module.exports.definition = {
set: function(v) {
this._setProperty('height', parseKeyword(v, ['auto']) || parseMeasurement(v));
this._setProperty('height', parseKeyword(v, ['auto']) || parseLengthOrPercentage(v));
},
get: function() {
return this.getPropertyValue('height');
Expand Down
4 changes: 2 additions & 2 deletions lib/properties/left.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

var parseMeasurement = require('../parsers').parseMeasurement;
const { parseLengthOrPercentage } = require('../parsers');

module.exports.definition = {
set: function(v) {
this._setProperty('left', parseMeasurement(v));
this._setProperty('left', parseLengthOrPercentage(v));
},
get: function() {
return this.getPropertyValue('left');
Expand Down
4 changes: 2 additions & 2 deletions lib/properties/lineHeight.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

const { parseKeyword, parseMeasurement, parseNumber } = require('../parsers');
const { parseKeyword, parseLengthOrPercentage, parseNumber } = require('../parsers');

function parse(v) {
return parseKeyword(v, ['normal']) || parseNumber(v) || parseMeasurement(v);
return parseKeyword(v, ['normal']) || parseNumber(v) || parseLengthOrPercentage(v);
}

module.exports.parse = parse;
Expand Down
4 changes: 2 additions & 2 deletions lib/properties/margin.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

const { implicitSetter, parseKeyword, parseMeasurement } = require('../parsers');
const { implicitSetter, parseKeyword, parseLengthOrPercentage } = require('../parsers');

function parse(v) {
return parseKeyword(v, ['auto']) || parseMeasurement(v);
return parseKeyword(v, ['auto']) || parseLengthOrPercentage(v);
}

const mySetter = implicitSetter('margin', '', parse);
Expand Down
6 changes: 3 additions & 3 deletions lib/properties/padding.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const { implicitSetter, parseKeyword, parseMeasurement } = require('../parsers');
const { implicitSetter, parseKeyword, parseLengthOrPercentage } = require('../parsers');

const mySetter = implicitSetter('padding', '', parseMeasurement);
const mySetter = implicitSetter('padding', '', parseLengthOrPercentage);
const myGlobal = implicitSetter('padding', '', function(v) {
return v;
});
Expand All @@ -23,4 +23,4 @@ module.exports.definition = {
configurable: true,
};

module.exports.parse = parseMeasurement;
module.exports.parse = parseLengthOrPercentage;
4 changes: 2 additions & 2 deletions lib/properties/right.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

var parseMeasurement = require('../parsers').parseMeasurement;
const { parseLengthOrPercentage } = require('../parsers');

module.exports.definition = {
set: function(v) {
this._setProperty('right', parseMeasurement(v));
this._setProperty('right', parseLengthOrPercentage(v));
},
get: function() {
return this.getPropertyValue('right');
Expand Down
4 changes: 2 additions & 2 deletions lib/properties/top.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

var parseMeasurement = require('../parsers').parseMeasurement;
const { parseLengthOrPercentage } = require('../parsers');

module.exports.definition = {
set: function(v) {
this._setProperty('top', parseMeasurement(v));
this._setProperty('top', parseLengthOrPercentage(v));
},
get: function() {
return this.getPropertyValue('top');
Expand Down
4 changes: 2 additions & 2 deletions lib/properties/width.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

const { parseKeyword, parseMeasurement } = require('../parsers');
const { parseKeyword, parseLengthOrPercentage } = require('../parsers');

module.exports.definition = {
set: function(v) {
this._setProperty('width', parseKeyword(v, ['auto']) || parseMeasurement(v));
this._setProperty('width', parseKeyword(v, ['auto']) || parseLengthOrPercentage(v));
},
get: function() {
return this.getPropertyValue('width');
Expand Down

0 comments on commit 38a54db

Please sign in to comment.