Skip to content

Commit 57c4610

Browse files
committed
Added fix for handling of spaces within parenthesis in values. Also added handling of nested parenthesis in a value so that it doesn't leave the value parenthesis state until all open parenthesis are matched with a close.
Added a test for the case I ran across this problem where I had a linear-gradient with an RGBA value for the second transition point, but transparent for the first. The space was dropped after the transparent, but not the rgba value because the closing paren of the RGBA value transitioned back into the value state from value-parenthesis.
1 parent 56d0145 commit 57c4610

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

lib/parse.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ CSSOM.parse = function parse(token) {
2424

2525
var index;
2626
var buffer = "";
27+
var valueParenthesisDepth=0;
2728

2829
var SIGNIFICANT_WHITESPACE = {
2930
"selector": true,
3031
"value": true,
32+
"value-parenthesis": true,
3133
"atRule": true,
3234
"importRule-begin": true,
3335
"importRule": true,
@@ -253,16 +255,22 @@ CSSOM.parse = function parse(token) {
253255
if (state === 'value') {
254256
// ie css expression mode
255257
if (buffer.trim() === 'expression') {
256-
var info = (new CSSOM.CSSValueExpression(token, i)).parse();
258+
var info = (new CSSOM.CSSValueExpression(token, i)).parse();
257259

258-
if (info.error) {
259-
parseError(info.error);
260-
} else {
261-
buffer += info.expression;
262-
i = info.idx;
263-
}
260+
if (info.error) {
261+
parseError(info.error);
262+
} else {
263+
buffer += info.expression;
264+
i = info.idx;
265+
}
266+
} else if (state === 'value-parenthesis') {
267+
valueParenthesisDepth++;
268+
buffer += character;
264269
} else {
265270
state = 'value-parenthesis';
271+
//always ensure this is reset to 1 on transition
272+
//from value to value-parenthesis
273+
valueParenthesisDepth=1;
266274
buffer += character;
267275
}
268276
} else {
@@ -272,7 +280,8 @@ CSSOM.parse = function parse(token) {
272280

273281
case ")":
274282
if (state === 'value-parenthesis') {
275-
state = 'value';
283+
valueParenthesisDepth--;
284+
if(valueParenthesisDepth === 0) state = 'value';
276285
}
277286
buffer += character;
278287
break;

spec/parse.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,28 @@ var TESTS = [
445445
return result;
446446
})()
447447
},
448+
{
449+
input: ".gradient{background-image: linear-gradient(transparent 50%, rgba(69, 142, 209, 0.04) 50%);}",
450+
result: (function() {
451+
var result = {
452+
cssRules: [
453+
{
454+
selectorText: '.gradient',
455+
parentRule: null,
456+
style: {
457+
0: 'background-image',
458+
"background-image": 'linear-gradient(transparent 50%, rgba(69, 142, 209, 0.04) 50%)',
459+
length: 1
460+
}
461+
}
462+
],
463+
parentStyleSheet: null
464+
};
465+
result.cssRules[0].parentStyleSheet = result;
466+
result.cssRules[0].style.parentRule = result.cssRules[0];
467+
return result;
468+
})()
469+
},
448470
{
449471
input: "@media handheld, only screen and (max-device-width: 480px) {body{max-width:480px}}",
450472
result: (function() {

0 commit comments

Comments
 (0)