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

Enable vars and nested calc&var #127

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion lib/CSSStyleDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ CSSStyleDeclaration.prototype = {
this.removeProperty(name);
return;
}
var isCustomProperty = name.indexOf('--') === 0;
var isCustomProperty =
name.indexOf('--') === 0 ||
(typeof value === 'string' && /^var\(--[-\w]+,?.*\)$/.test(value));
if (isCustomProperty) {
this._setProperty(name, value, priority);
return;
Expand Down
42 changes: 42 additions & 0 deletions lib/CSSStyleDeclaration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -734,4 +734,46 @@ describe('CSSStyleDeclaration', () => {
style.setProperty('width', 'calc(100% - 100px)');
expect(style.getPropertyValue('width')).toEqual('calc(100% - 100px)');
});

test('supports nested calc', () => {
const style = new CSSStyleDeclaration();
style.setProperty('width', 'calc(100% - calc(200px - 100px))');
expect(style.getPropertyValue('width')).toEqual('calc(100% - calc(200px - 100px))');
});

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

test('supports var with fallback', () => {
const style = new CSSStyleDeclaration();
style.setProperty('width', 'var(--foo, 100px)');
expect(style.getPropertyValue('width')).toEqual('var(--foo, 100px)');
});

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

test('supports calc with var inside', () => {
const style = new CSSStyleDeclaration();
style.setProperty('width', 'calc(100% - var(--foo))');
expect(style.getPropertyValue('width')).toEqual('calc(100% - var(--foo))');
});

test('supports var with calc inside', () => {
const style = new CSSStyleDeclaration();
style.setProperty('width', 'var(--foo, calc(var(--bar) + 3px))');
expect(style.getPropertyValue('width')).toEqual('var(--foo, calc(var(--bar) + 3px))');
});

test('supports color var', () => {
const style = new CSSStyleDeclaration();
style.setProperty('color', 'var(--foo)');
expect(style.getPropertyValue('color')).toEqual('var(--foo)');
});
});
2 changes: 1 addition & 1 deletion lib/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ var stringRegEx = /^("[^"]*"|'[^']*')$/;
var colorRegEx1 = /^#([0-9a-fA-F]{3,4}){1,2}$/;
var colorRegEx2 = /^rgb\(([^)]*)\)$/;
var colorRegEx3 = /^rgba\(([^)]*)\)$/;
var calcRegEx = /^calc\(([^)]*)\)$/;
var colorRegEx4 =
/^hsla?\(\s*(-?\d+|-?\d*.\d+)\s*,\s*(-?\d+|-?\d*.\d+)%\s*,\s*(-?\d+|-?\d*.\d+)%\s*(,\s*(-?\d+|-?\d*.\d+)\s*)?\)/;
var calcRegEx = /^calc\((.*)\)$/;
var angleRegEx = /^([-+]?[0-9]*\.?[0-9]+)(deg|grad|rad)$/;

// This will return one of the above types based on the passed in string
Expand Down
44 changes: 44 additions & 0 deletions lib/parsers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,41 @@ describe('valueType', () => {

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

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

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

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

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

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

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

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

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

it('returns var from var(--foo, calc(var(--bar) * 2))', () => {
let input = 'var(--foo, calc(var(--bar) * 2))';
let output = parsers.valueType(input);

expect(output).toEqual(parsers.TYPES.KEYWORD);
});
});
describe('parseInteger', () => {
it.todo('test');
Expand Down Expand Up @@ -107,6 +142,14 @@ describe('parseColor', () => {

expect(output).toEqual('rgba(5, 5, 5, 0.5)');
});

it('should pass through vars', () => {
let input = 'var(--foo)';
let output = parsers.valueType(input);

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

it.each([
[120, 'rgb(0, 255, 0)'],
[240, 'rgb(0, 0, 255)'],
Expand All @@ -115,6 +158,7 @@ describe('parseColor', () => {
let output = parsers.parseColor(input);
expect(output).toEqual(rgbValue);
});

it.todo('Add more tests');
});
describe('parseAngle', () => {
Expand Down