Bug 663194 - Calling drawImage with zero-size canvas should throw INVALID_STATE_ERR; r=sicking

This commit is contained in:
Ms2ger 2011-06-11 09:52:27 +02:00
parent a2b407958f
commit a6c4503b05
4 changed files with 74 additions and 2 deletions

View File

@ -3291,8 +3291,14 @@ nsCanvasRenderingContext2D::DrawImage(nsIDOMElement *imgElt, float a1,
return NS_ERROR_DOM_TYPE_MISMATCH_ERR;
}
double sx,sy,sw,sh;
double dx,dy,dw,dh;
nsCOMPtr<nsIContent> content = do_QueryInterface(imgElt);
nsHTMLCanvasElement* canvas = nsHTMLCanvasElement::FromContent(content);
if (canvas) {
nsIntSize size = canvas->GetSize();
if (size.width == 0 || size.height == 0) {
return NS_ERROR_DOM_INVALID_STATE_ERR;
}
}
gfxMatrix matrix;
nsRefPtr<gfxPattern> pattern;
@ -3334,6 +3340,8 @@ nsCanvasRenderingContext2D::DrawImage(nsIDOMElement *imgElt, float a1,
}
}
double sx,sy,sw,sh;
double dx,dy,dw,dh;
if (optional_argc == 0) {
dx = a1;
dy = a2;

View File

@ -79,6 +79,7 @@ _TEST_FILES_0 = \
test_2d.composite.uncovered.image.destination-in.html \
test_2d.composite.uncovered.image.source-in.html \
test_2d.composite.uncovered.image.source-out.html \
test_2d.drawImage.zerocanvas.html \
test_toDataURL_lowercase_ascii.html \
test_toDataURL_parameters.html \
test_mozGetAsFile.html \

View File

@ -0,0 +1,56 @@
<!DOCTYPE HTML>
<title>Canvas test: 2d.drawImage.zerocanvas</title>
<script src="/MochiKit/MochiKit.js"></script>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css">
<body>
<canvas id="c" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
<script>
function isPixel(ctx, x,y, r,g,b,a, pos, colour, d) {
var pixel = ctx.getImageData(x, y, 1, 1);
var pr = pixel.data[0],
pg = pixel.data[1],
pb = pixel.data[2],
pa = pixel.data[3];
ok(r-d <= pr && pr <= r+d &&
g-d <= pg && pg <= g+d &&
b-d <= pb && pb <= b+d &&
a-d <= pa && pa <= a+d,
"pixel "+pos+" is "+pr+","+pg+","+pb+","+pa+"; expected "+colour+" +/- "+d);
}
SimpleTest.waitForExplicitFinish();
MochiKit.DOM.addLoadEvent(function () {
var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 100, 50);
var canvas2 = document.createElement('canvas');
canvas2.width = 0;
canvas2.height = 10;
var _thrown = undefined; try {
ctx.drawImage(canvas2, 0, 0);
} catch (e) { _thrown = e }; ok(_thrown && _thrown.code == DOMException.INVALID_STATE_ERR, "should throw INVALID_STATE_ERR");
canvas2.width = 10;
canvas2.height = 0;
var _thrown = undefined; try {
ctx.drawImage(canvas2, 0, 0);
} catch (e) { _thrown = e }; ok(_thrown && _thrown.code == DOMException.INVALID_STATE_ERR, "should throw INVALID_STATE_ERR");
canvas2.width = 0;
canvas2.height = 0;
var _thrown = undefined; try {
ctx.drawImage(canvas2, 0, 0);
} catch (e) { _thrown = e }; ok(_thrown && _thrown.code == DOMException.INVALID_STATE_ERR, "should throw INVALID_STATE_ERR");
isPixel(ctx, 50,25, 0,255,0,255, "50,25", "0,255,0,255", 2);
SimpleTest.finish();
});
</script>

View File

@ -68,6 +68,13 @@ public:
nsHTMLCanvasElement(already_AddRefed<nsINodeInfo> aNodeInfo);
virtual ~nsHTMLCanvasElement();
static nsHTMLCanvasElement* FromContent(nsIContent* aPossibleCanvas)
{
if (!aPossibleCanvas || !aPossibleCanvas->IsHTML(nsGkAtoms::canvas)) {
return nsnull;
}
return static_cast<nsHTMLCanvasElement*>(aPossibleCanvas);
}
// nsISupports
NS_DECL_ISUPPORTS_INHERITED