Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keep property definition using calc and var #141

Closed
wants to merge 3 commits into from
Closed
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
12 changes: 12 additions & 0 deletions lib/CSSStyleDeclaration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,4 +553,16 @@ describe('CSSStyleDeclaration', () => {
style.setProperty('width', 'calc(100% - 100px)');
expect(style.getPropertyValue('width')).toEqual('calc(100% - 100px)');
});

test('supports var', () => {
const style = new CSSStyleDeclaration();
style.setProperty('width', 'var(--value)');
expect(style.getPropertyValue('width')).toEqual('var(--value)');
});

test('supports color var', () => {
const style = new CSSStyleDeclaration();
style.setProperty('color', 'var(--foo)');
expect(style.getPropertyValue('color')).toEqual('var(--foo)');
});
});
13 changes: 10 additions & 3 deletions lib/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ exports.TYPES = {
KEYWORD: 9,
NULL_OR_EMPTY_STR: 10,
CALC: 11,
VARIABLE: 12
};

// rough regular expressions
Expand All @@ -31,7 +32,8 @@ var stringRegEx = /^("[^"]*"|'[^']*')$/;
var colorRegEx1 = /^#([0-9a-fA-F]{3,4}){1,2}$/;
var colorRegEx2 = /^rgb\(([^)]*)\)$/;
var colorRegEx3 = /^rgba\(([^)]*)\)$/;
var calcRegEx = /^calc\(([^)]*)\)$/;
var calcRegEx = /^calc\((.*)\)$/;
var variableRegEx = /^var\(--([^)]*)\)$/;
var colorRegEx4 = /^hsla?\(\s*(-?\d+|-?\d*.\d+)\s*,\s*(-?\d+|-?\d*.\d+)%\s*,\s*(-?\d+|-?\d*.\d+)%\s*(,\s*(-?\d+|-?\d*.\d+)\s*)?\)/;
var angleRegEx = /^([-+]?[0-9]*\.?[0-9]+)(deg|grad|rad)$/;

Expand Down Expand Up @@ -66,6 +68,9 @@ exports.valueType = function valueType(val) {
if (calcRegEx.test(val)) {
return exports.TYPES.CALC;
}
if (variableRegEx.test(val)) {
return exports.TYPES.VARIABLE;
}
if (stringRegEx.test(val)) {
return exports.TYPES.STRING;
}
Expand Down Expand Up @@ -208,7 +213,8 @@ exports.parsePercent = function parsePercent(val) {
// either a length or a percent
exports.parseMeasurement = function parseMeasurement(val) {
var type = exports.valueType(val);
if (type === exports.TYPES.CALC) {
if (type === exports.TYPES.CALC ||
type === exports.TYPES.VARIABLE) {
return val;
}

Expand Down Expand Up @@ -287,7 +293,8 @@ exports.parseString = function parseString(val) {

exports.parseColor = function parseColor(val) {
var type = exports.valueType(val);
if (type === exports.TYPES.NULL_OR_EMPTY_STR) {
if (type === exports.TYPES.NULL_OR_EMPTY_STR
|| type === exports.TYPES.VARIABLE) {
return val;
}
var red,
Expand Down
69 changes: 69 additions & 0 deletions lib/parsers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ describe('valueType', () => {

expect(output).toEqual(parsers.TYPES.CALC);
});

it('returns calc from calc(100px * (6 - 4))', () => {
let input = 'calc(100px * (6 - 4))';
let output = parsers.valueType(input);

expect(output).toEqual(parsers.TYPES.CALC);
});

it('returns variable from var(--value)', () => {
let input = 'var(--value)';
let output = parsers.valueType(input);

expect(output).toEqual(parsers.TYPES.VARIABLE);
});
});
describe('parseInteger', () => {
it.todo('test');
Expand All @@ -86,6 +100,55 @@ describe('parsePercent', () => {
it.todo('test');
});
describe('parseMeasurement', () => {
it('accepts px', () => {
let input = '15px';
let output = parsers.parseMeasurement(input);

expect(output).toEqual('15px');
});
it('accepts percent', () => {
let input = '15%';
let output = parsers.parseMeasurement(input);

expect(output).toEqual('15%');
});
it('defaults to undefined', () => {
let input = '15';
let output = parsers.parseMeasurement(input);

expect(output).toEqual(undefined);
});
it('accepts zero without unit', () => {
let input = '0';
let output = parsers.parseMeasurement(input);

expect(output).toEqual('0px');
});
it('rejects other unit', () => {
let input = '15deg';
let output = parsers.parseMeasurement(input);

expect(output).toEqual(undefined);
});
it('keeps value definition starting with calc', () => {
let input = 'calc(100% / 4)';
let output = parsers.parseMeasurement(input);

expect(output).toEqual('calc(100% / 4)');
});
it('keeps value definition starting with var', () => {
let input = 'var(--value)';
let output = parsers.parseMeasurement(input);

expect(output).toEqual('var(--value)');
});
it('keeps value definition combining calc and var', () => {
let input = 'calc((20 - 18) * 2 * var(--value))';
let output = parsers.parseMeasurement(input);

expect(output).toEqual('calc((20 - 18) * 2 * var(--value))');
});

it.todo('test');
});
describe('parseUrl', () => {
Expand All @@ -107,6 +170,12 @@ describe('parseColor', () => {

expect(output).toEqual('rgba(5, 5, 5, 0.5)');
});
it('should keep value definition when it starts with var', () => {
let input = 'var(--value)';
let output = parsers.parseColor(input);

expect(output).toEqual('var(--value)');
});

it.todo('Add more tests');
});
Expand Down