Skip to content

Commit

Permalink
update with review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
snowystinger committed Jul 8, 2024
1 parent f006d16 commit 2f314f9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
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
12 changes: 2 additions & 10 deletions lib/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ exports.TYPES = {
ANGLE: 8,
KEYWORD: 9,
NULL_OR_EMPTY_STR: 10,
CALC: 11,
VAR: 12,
CALC: 11

Check warning on line 21 in lib/parsers.js

View workflow job for this annotation

GitHub Actions / Lint and tests (18)

Insert `,`

Check warning on line 21 in lib/parsers.js

View workflow job for this annotation

GitHub Actions / Lint and tests (20)

Insert `,`

Check warning on line 21 in lib/parsers.js

View workflow job for this annotation

GitHub Actions / Lint and tests (latest)

Insert `,`
};

// rough regular expressions
Expand All @@ -36,7 +35,6 @@ 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)$/;
var varRegEx = /^var\((.*)\)$/;

// This will return one of the above types based on the passed in string
exports.valueType = function valueType(val) {
Expand Down Expand Up @@ -69,9 +67,6 @@ exports.valueType = function valueType(val) {
if (calcRegEx.test(val)) {
return exports.TYPES.CALC;
}
if (varRegEx.test(val)) {
return exports.TYPES.VAR;
}
if (stringRegEx.test(val)) {
return exports.TYPES.STRING;
}
Expand Down Expand Up @@ -214,7 +209,7 @@ 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 || type === exports.TYPES.VAR) {
if (type === exports.TYPES.CALC) {
return val;
}

Expand Down Expand Up @@ -293,9 +288,6 @@ exports.parseString = function parseString(val) {

exports.parseColor = function parseColor(val) {
var type = exports.valueType(val);
if (type === exports.TYPES.VAR) {
return val;
}
if (type === exports.TYPES.NULL_OR_EMPTY_STR) {
return val;
}
Expand Down
9 changes: 4 additions & 5 deletions lib/parsers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,21 @@ describe('valueType', () => {
let input = 'var(--foo)';
let output = parsers.valueType(input);

expect(output).toEqual(parsers.TYPES.VAR);
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.VAR);
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.VAR);
expect(output).toEqual(parsers.TYPES.KEYWORD);
});
});
describe('parseInteger', () => {
Expand Down Expand Up @@ -143,12 +143,11 @@ 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.VAR);
expect(output).toEqual(parsers.TYPES.KEYWORD);
});

it.each([
Expand Down

0 comments on commit 2f314f9

Please sign in to comment.