Match length in pixels

This commit is contained in:
Rafael Caricio 2014-09-06 13:28:43 +02:00
parent a7981cc9dc
commit 066fa8297b
2 changed files with 50 additions and 3 deletions

View file

@ -16,6 +16,9 @@ module.exports = (function() {
'rgba', 'rgba',
'hsl', 'hsl',
'literal' 'literal'
],
metrics: [
'px'
] ]
}; };
@ -23,6 +26,7 @@ module.exports = (function() {
linearGradient: /^linear\-gradient/i, linearGradient: /^linear\-gradient/i,
radialGradient: /^radial\-gradient/i, radialGradient: /^radial\-gradient/i,
sideOrCorner: /^to (left (top|bottom)|right (top|bottom)|left|right|top|bottom)/i, sideOrCorner: /^to (left (top|bottom)|right (top|bottom)|left|right|top|bottom)/i,
pixelValue: /^([0-9]+)px/,
startCall: /^\(/, startCall: /^\(/,
endCall: /^\)/, endCall: /^\)/,
comma: /^,/ comma: /^,/
@ -143,6 +147,17 @@ module.exports = (function() {
} }
function matchColorStop() { function matchColorStop() {
var color = matchColor();
if (!color) {
error('Expected color definition');
}
color.length = matchLength();
return color;
}
function matchColor() {
return matchLiteralColor(); return matchLiteralColor();
} }
@ -158,6 +173,20 @@ module.exports = (function() {
} }
} }
function matchLength() {
return matchPixel();
}
function matchPixel() {
var captures = scan(tokens.pixelValue);
if (captures) {
return {
type: 'px',
value: captures[1]
};
}
}
function scan(regexp) { function scan(regexp) {
var captures, var captures,
blankCaptures; blankCaptures;

View file

@ -49,7 +49,7 @@ describe('gradient-parser.js', function () {
it('when there\'s one more comma in colors', function() { it('when there\'s one more comma in colors', function() {
expect(function() { expect(function() {
gradientParser('linear-gradient(red, blue,)'); gradientParser('linear-gradient(red, blue,)');
}).to.throwException(/One extra comma/); }).to.throwException(/Expected color definition/);
}); });
it('when there\'s invalid input', function() { it('when there\'s invalid input', function() {
@ -73,7 +73,7 @@ describe('gradient-parser.js', function () {
it('when there\'s missing color stops', function() { it('when there\'s missing color stops', function() {
expect(function() { expect(function() {
gradientParser('linear-gradient(to right, )'); gradientParser('linear-gradient(to right, )');
}).to.throwException(/Missing color definitions/); }).to.throwException(/Expected color definition/);
}); });
it('when there\'s missing closing call', function() { it('when there\'s missing closing call', function() {
@ -83,7 +83,7 @@ describe('gradient-parser.js', function () {
}); });
}); });
describe('when parsing a simple definition', function(){ describe('when parsing a simple definition', function() {
beforeEach(function() { beforeEach(function() {
ast = gradientParser('linear-gradient(red, blue)'); ast = gradientParser('linear-gradient(red, blue)');
subject = ast[0]; subject = ast[0];
@ -131,4 +131,22 @@ describe('gradient-parser.js', function () {
}); });
}); });
}); });
describe('parse an definition with full color stop', function() {
beforeEach(function() {
ast = gradientParser('linear-gradient(blue 10px, transparent)');
subject = ast[0];
});
describe('the first color', function() {
beforeEach(function() {
subject = subject.colorStops[0];
});
it('should have the length', function() {
expect(subject.length.type).to.equal('px');
expect(subject.length.value).to.equal('10');
});
});
});
}); });