Skip to content

Commit 2f5f129

Browse files
Merge pull request #37 from rafaelcaricio/fix-repeating-linear-gradient
fix: verify repeating-linear-gradient parsing
2 parents 77fb969 + d03061d commit 2f5f129

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

build/node.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ GradientParser.stringify = (function() {
142142
return result;
143143
},
144144

145+
'visit_object': function(obj) {
146+
if (obj.width && obj.height) {
147+
return visitor.visit(obj.width) + ' ' + visitor.visit(obj.height);
148+
}
149+
return '';
150+
},
151+
145152
'visit': function(element) {
146153
if (!element) {
147154
return '';
@@ -150,6 +157,8 @@ GradientParser.stringify = (function() {
150157

151158
if (element instanceof Array) {
152159
return visitor.visit_array(element, result);
160+
} else if (typeof element === 'object' && !element.type) {
161+
return visitor.visit_object(element);
153162
} else if (element.type) {
154163
var nodeVisitor = visitor['visit_' + element.type];
155164
if (nodeVisitor) {
@@ -361,7 +370,7 @@ GradientParser.parse = (function() {
361370
var ellipse = match('shape', /^(ellipse)/i, 0);
362371

363372
if (ellipse) {
364-
ellipse.style = matchDistance() || matchExtentKeyword();
373+
ellipse.style = matchPositioning() || matchDistance() || matchExtentKeyword();
365374
}
366375

367376
return ellipse;
@@ -529,7 +538,11 @@ GradientParser.parse = (function() {
529538
}
530539

531540
return function(code) {
532-
input = code.toString();
541+
input = code.toString().trim();
542+
// Remove trailing semicolon if present
543+
if (input.endsWith(';')) {
544+
input = input.slice(0, -1);
545+
}
533546
return getAST();
534547
};
535548
})();

build/web.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ GradientParser.parse = (function() {
192192
var ellipse = match('shape', /^(ellipse)/i, 0);
193193

194194
if (ellipse) {
195-
ellipse.style = matchDistance() || matchExtentKeyword();
195+
ellipse.style = matchPositioning() || matchDistance() || matchExtentKeyword();
196196
}
197197

198198
return ellipse;
@@ -360,7 +360,11 @@ GradientParser.parse = (function() {
360360
}
361361

362362
return function(code) {
363-
input = code.toString();
363+
input = code.toString().trim();
364+
// Remove trailing semicolon if present
365+
if (input.endsWith(';')) {
366+
input = input.slice(0, -1);
367+
}
364368
return getAST();
365369
};
366370
})();
@@ -509,6 +513,13 @@ GradientParser.stringify = (function() {
509513
return result;
510514
},
511515

516+
'visit_object': function(obj) {
517+
if (obj.width && obj.height) {
518+
return visitor.visit(obj.width) + ' ' + visitor.visit(obj.height);
519+
}
520+
return '';
521+
},
522+
512523
'visit': function(element) {
513524
if (!element) {
514525
return '';
@@ -517,6 +528,8 @@ GradientParser.stringify = (function() {
517528

518529
if (element instanceof Array) {
519530
return visitor.visit_array(element, result);
531+
} else if (typeof element === 'object' && !element.type) {
532+
return visitor.visit_object(element);
520533
} else if (element.type) {
521534
var nodeVisitor = visitor['visit_' + element.type];
522535
if (nodeVisitor) {

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/parser.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,26 @@ describe('lib/parser.js', function () {
311311
});
312312
});
313313

314+
describe('parse gradient strings', function() {
315+
it('should parse repeating linear gradient with bottom right direction', function() {
316+
const gradient = 'repeating-linear-gradient(to bottom right,rgb(254, 158, 150) 0%,rgb(172, 79, 115) 100%)';
317+
const ast = gradients.parse(gradient);
318+
319+
expect(ast[0].type).to.equal('repeating-linear-gradient');
320+
expect(ast[0].orientation.type).to.equal('directional');
321+
expect(ast[0].orientation.value).to.equal('bottom right');
322+
323+
expect(ast[0].colorStops).to.have.length(2);
324+
expect(ast[0].colorStops[0].type).to.equal('rgb');
325+
expect(ast[0].colorStops[0].value).to.eql(['254', '158', '150']);
326+
expect(ast[0].colorStops[0].length.type).to.equal('%');
327+
expect(ast[0].colorStops[0].length.value).to.equal('0');
328+
329+
expect(ast[0].colorStops[1].type).to.equal('rgb');
330+
expect(ast[0].colorStops[1].value).to.eql(['172', '79', '115']);
331+
expect(ast[0].colorStops[1].length.type).to.equal('%');
332+
expect(ast[0].colorStops[1].length.value).to.equal('100');
333+
});
334+
});
335+
314336
});

0 commit comments

Comments
 (0)