Parse hex colors
This commit is contained in:
parent
bf419dfe7d
commit
034a0ae448
2 changed files with 38 additions and 8 deletions
|
@ -32,7 +32,9 @@ module.exports = (function() {
|
||||||
angleValue: /^([0-9]+)deg/,
|
angleValue: /^([0-9]+)deg/,
|
||||||
startCall: /^\(/,
|
startCall: /^\(/,
|
||||||
endCall: /^\)/,
|
endCall: /^\)/,
|
||||||
comma: /^,/
|
comma: /^,/,
|
||||||
|
hexColor: /^\#([0-9a-fA-F]+)/,
|
||||||
|
literalColor: /^([a-zA-Z]+)/
|
||||||
};
|
};
|
||||||
|
|
||||||
var input = '',
|
var input = '',
|
||||||
|
@ -172,12 +174,12 @@ module.exports = (function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function matchColor() {
|
function matchColor() {
|
||||||
return matchLiteralColor();
|
return matchLiteralColor() ||
|
||||||
|
matchHexColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
function matchLiteralColor() {
|
function matchLiteralColor() {
|
||||||
var literalColors = /^([a-zA-Z]+)/,
|
var captures = scan(tokens.literalColor);
|
||||||
captures = scan(literalColors);
|
|
||||||
|
|
||||||
if (captures) {
|
if (captures) {
|
||||||
return {
|
return {
|
||||||
|
@ -187,6 +189,17 @@ module.exports = (function() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function matchHexColor() {
|
||||||
|
var captures = scan(tokens.hexColor);
|
||||||
|
|
||||||
|
if (captures) {
|
||||||
|
return {
|
||||||
|
type: 'hex',
|
||||||
|
value: captures[1]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function matchLength() {
|
function matchLength() {
|
||||||
return matchMetric(tokens.pixelValue, 'px') ||
|
return matchMetric(tokens.pixelValue, 'px') ||
|
||||||
matchMetric(tokens.percentageValue, '%') ||
|
matchMetric(tokens.percentageValue, '%') ||
|
||||||
|
|
|
@ -159,13 +159,30 @@ describe('gradient-parser.js', function () {
|
||||||
describe('parse orientation ' + orientation.type, function() {
|
describe('parse orientation ' + orientation.type, function() {
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
ast = gradientParser('linear-gradient(' + orientation.unparsedValue + ', blue, green)');
|
ast = gradientParser('linear-gradient(' + orientation.unparsedValue + ', blue, green)');
|
||||||
subject = ast[0];
|
subject = ast[0].orientation;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should parse the ' + orientation.type + ' orientation', function() {
|
it('should parse value', function() {
|
||||||
expect(subject.orientation.type).to.equal(orientation.type);
|
expect(subject.type).to.equal(orientation.type);
|
||||||
expect(subject.orientation.value).to.equal(orientation.value);
|
expect(subject.value).to.equal(orientation.value);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
[
|
||||||
|
{type: 'literal', unparsedValue: 'red', value: 'red'},
|
||||||
|
{type: 'hex', unparsedValue: '#c2c2c2', value: 'c2c2c2'}
|
||||||
|
].forEach(function(color) {
|
||||||
|
describe('parse color type '+ color.type, function() {
|
||||||
|
beforeEach(function() {
|
||||||
|
ast = gradientParser('linear-gradient(12deg, ' + color.unparsedValue + ', blue, green)');
|
||||||
|
subject = ast[0].colorStops[0];
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should parse value', function() {
|
||||||
|
expect(subject.type).to.equal(color.type);
|
||||||
|
expect(subject.value).to.equal(color.value);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue