Bug 517056. Drawing an incomplete image to the canvas shouldn't throw; should instead silently do nothing. r=vlad

This commit is contained in:
Boris Zbarsky 2010-06-18 12:23:05 -04:00
parent f305d5893c
commit 037bfe5f69
5 changed files with 84 additions and 3 deletions

View File

@ -3105,8 +3105,10 @@ nsCanvasRenderingContext2D::DrawImage(nsIDOMElement *imgElt, float a1,
PRUint32 sfeFlags = nsLayoutUtils::SFE_WANT_FIRST_FRAME;
nsLayoutUtils::SurfaceFromElementResult res =
nsLayoutUtils::SurfaceFromElement(imgElt, sfeFlags);
if (!res.mSurface)
return NS_ERROR_NOT_AVAILABLE;
if (!res.mSurface) {
// Spec says to silently do nothing if the element is still loading.
return res.mIsStillLoading ? NS_OK : NS_ERROR_NOT_AVAILABLE;
}
#ifndef WINCE
// On non-CE, force a copy if we're using drawImage with our destination

View File

@ -65,6 +65,7 @@ _TEST_FILES_0 = \
image_green.png \
image_green-redirect \
image_green-redirect^headers^ \
test_drawImageIncomplete.html \
$(NULL)
# xor and lighter aren't well handled by cairo; they mostly work, but we don't want

View File

@ -0,0 +1,60 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=517056
-->
<head>
<title>Test for Bug 517056</title>
<script type="application/javascript" src="/MochiKit/packed.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=517056">Mozilla Bug 517056</a>
<p id="display">
<canvas id="c" width="1" height="1"></canvas>
</p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 517056 **/
var ctx = $("c").getContext('2d');
ctx.fillStyle = "black";
ctx.fillRect(0, 0, 1, 1);
var data = ctx.getImageData(0, 0, 1, 1).data;
is(data[0], 0, "Red channel of black should be 0");
is(data[1], 0, "Green channel of black should be 0");
is(data[2], 0, "Blue channel of black should be 0")
is(data[3], 255, "Alpha channel of black should be opaque");
var img = new Image();
// Force a new URI every time, so that we don't run into stupid caching issues.
img.src = "image_green-1x1.png?" + (new Date + 0) + Math.random();
// This shouldn't throw
ctx.drawImage(img, 0, 0);
var data = ctx.getImageData(0, 0, 1, 1).data;
is(data[0], 0, "Red channel of black should be 0 and image should have been ignored");
is(data[1], 0, "Green channel of black should be 0 and image should have been ignored");
is(data[2], 0, "Blue channel of black should be 0 and image should have been ignored")
is(data[3], 255, "Alpha channel of black should be opaque and image should have been ignored");
SimpleTest.waitForExplicitFinish();
img.onload = function() {
ctx.drawImage(img, 0, 0);
var data = ctx.getImageData(0, 0, 1, 1).data;
is(data[0], 0, "Red channel of green should be 0");
is(data[1], 255, "Green channel of green should be 255");
is(data[2], 0, "Blue channel of green should be 0")
is(data[3], 255, "Alpha channel of green should be opaque");
SimpleTest.finish();
}
</script>
</pre>
</body>
</html>

View File

@ -3441,6 +3441,14 @@ nsLayoutUtils::SurfaceFromElement(nsIDOMElement *aElement,
if (node && ve) {
nsHTMLVideoElement *video = static_cast<nsHTMLVideoElement*>(ve.get());
unsigned short readyState;
if (NS_SUCCEEDED(ve->GetReadyState(&readyState)) &&
(readyState == nsIDOMHTMLMediaElement::HAVE_NOTHING ||
readyState == nsIDOMHTMLMediaElement::HAVE_METADATA)) {
result.mIsStillLoading = PR_TRUE;
return result;
}
// If it doesn't have a principal, just bail
nsCOMPtr<nsIPrincipal> principal = video->GetCurrentPrincipal();
if (!principal)
@ -3492,8 +3500,13 @@ nsLayoutUtils::SurfaceFromElement(nsIDOMElement *aElement,
PRUint32 status;
imgRequest->GetImageStatus(&status);
if ((status & imgIRequest::STATUS_LOAD_COMPLETE) == 0)
if ((status & imgIRequest::STATUS_LOAD_COMPLETE) == 0) {
// Spec says to use GetComplete, but that only works on
// nsIDOMHTMLImageElement, and we support all sorts of other stuff
// here. Do this for now pending spec clarification.
result.mIsStillLoading = (status & imgIRequest::STATUS_ERROR) == 0;
return result;
}
// In case of data: URIs, we want to ignore principals;
// they should have the originating content's principal,

View File

@ -1144,6 +1144,8 @@ public:
};
struct SurfaceFromElementResult {
SurfaceFromElementResult() : mIsStillLoading(PR_FALSE) {}
/* mSurface will contain the resulting surface, or will be NULL on error */
nsRefPtr<gfxASurface> mSurface;
/* The size of the surface */
@ -1152,6 +1154,9 @@ public:
nsCOMPtr<nsIPrincipal> mPrincipal;
/* Whether the element was "write only", that is, the bits should not be exposed to content */
PRBool mIsWriteOnly;
/* Whether the element was still loading. Some consumers need to handle
this case specially. */
PRBool mIsStillLoading;
};
static SurfaceFromElementResult SurfaceFromElement(nsIDOMElement *aElement,