Browse Source

Added invalid fill/stroke style test

v1.x
Tj Holowaychuk 14 years ago
parent
commit
c752a54aee
  1. 50
      lib/canvas.js
  2. 25
      test/canvas.test.js

50
lib/canvas.js

@ -67,7 +67,7 @@ var cache = {};
* @api public
*/
exports.parseColor = function(str){
var parseColor = exports.parseColor = function(str){
if (cache[str]) return cache[str];
str = colors[str] || String(str);
if (0 == str.indexOf('rgba')) {
@ -127,13 +127,15 @@ Canvas.prototype.getContext = function(contextId){
};
CanvasGradient.prototype.addColorStop = function(offset, color){
var rgba = exports.parseColor(color) || [0,0,0,1];
this.addColorStopRGBA(
offset
, rgba[0]
, rgba[1]
, rgba[2]
, rgba[3]);
var rgba;
if (rgba = parseColor(color)) {
this.addColorStopRGBA(
offset
, rgba[0]
, rgba[1]
, rgba[2]
, rgba[3]);
}
};
Context2d.prototype.createLinearGradient = function(x0, y0, x1, y1){
@ -155,13 +157,15 @@ Context2d.prototype.__defineSetter__('fillStyle', function(val){
if (val instanceof CanvasGradient) {
this.setFillPattern(val);
} else if ('string' == typeof val) {
var rgba = exports.parseColor(val) || [0,0,0,1];
this.lastFillStyle = rgba;
this.setFillRGBA(
rgba[0]
, rgba[1]
, rgba[2]
, rgba[3]);
var rgba;
if (rgba = parseColor(val)) {
this.lastFillStyle = rgba;
this.setFillRGBA(
rgba[0]
, rgba[1]
, rgba[2]
, rgba[3]);
}
}
});
@ -187,13 +191,15 @@ Context2d.prototype.__defineSetter__('strokeStyle', function(val){
if (val instanceof CanvasGradient) {
this.setStrokePattern(val);
} else if ('string' == typeof val) {
var rgba = exports.parseColor(val) || [0,0,0,1];
this.lastStrokeStyle = rgba;
this.setStrokeRGBA(
rgba[0]
, rgba[1]
, rgba[2]
, rgba[3]);
var rgba;
if (rgba = parseColor(val)) {
this.lastStrokeStyle = rgba;
this.setStrokeRGBA(
rgba[0]
, rgba[1]
, rgba[2]
, rgba[3]);
}
}
});

25
test/canvas.test.js

@ -431,5 +431,30 @@ module.exports = {
, path
, 'd078f5993eb962a5b3fdde5ca0864179'
, 'Context2d#createRadialGradient() failed');
},
'test invalid {fill,stroke}Style': function(assert){
var canvas = new Canvas(200, 200)
, ctx = canvas.getContext('2d')
, path = __dirname + '/images/invalidStyle.png';
ctx.fillStyle = 'red';
ctx.strokeStyle = 'yellow';
ctx.rect(50,50,50,50);
ctx.fill();
ctx.stroke();
ctx.beginPath();
ctx.fillStyle = 'asdf';
ctx.strokeStyle = 'asdf';
ctx.rect(100,80,15,15);
ctx.fill();
ctx.stroke();
assertChecksum(
canvas
, path
, '2ba95ccadd5c38949a5ea493dbc78e08'
, 'Context2d invalid fillStyle did not retain previous value');
}
}
Loading…
Cancel
Save