Fix #688 fatal exception on unmatched color stop

This commit is contained in:
Niklas von Hertzen
2015-10-18 16:51:53 +03:00
parent 4b37909f09
commit 46078acf71
5 changed files with 41 additions and 16 deletions

View File

@ -1,6 +1,8 @@
var GradientContainer = require('./gradientcontainer');
var Color = require('./color');
var COLOR_STOP_REGEXP = /^(.*)\s*(\d{1,3})?(%|px)?$/;
function LinearGradientContainer(imageData) {
GradientContainer.apply(this, arguments);
this.type = this.TYPES.LINEAR;
@ -41,13 +43,16 @@ function LinearGradientContainer(imageData) {
this.y1 = 1;
}
this.colorStops = imageData.args.slice(hasDirection ? 1 : 0).map(function(colorStop) {
var colorStopMatch = colorStop.match(/((?:rgb|rgba)\(\d{1,3},\s\d{1,3},\s\d{1,3}(?:,\s[0-9\.]+)?\)|\w+)\s*(\d{1,3})?(%|px)?/);
return {
color: new Color(colorStopMatch[1]),
stop: colorStopMatch[3] === "%" ? colorStopMatch[2] / 100 : null
};
}, this);
this.colorStops = imageData.args.slice(hasDirection ? 1 : 0)
.map(function(colorStop) { return colorStop.match(COLOR_STOP_REGEXP);})
.filter(function(colorStopMatch) { return !!colorStopMatch;})
.map(function(colorStopMatch) {
return {
color: new Color(colorStopMatch[1]),
stop: colorStopMatch[3] === "%" ? colorStopMatch[2] / 100 : null
};
});
if (this.colorStops[0].stop === null) {
this.colorStops[0].stop = 0;