Handle toDataURL with no context

This commit is contained in:
David Zbarsky 2015-11-28 16:09:00 -08:00
parent da8952b702
commit 84ec9c4266
3 changed files with 41 additions and 39 deletions

View file

@ -258,12 +258,12 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement {
_mime_type: Option<DOMString>,
_arguments: Vec<HandleValue>) -> Fallible<DOMString> {
if let Some(CanvasContext::Context2d(ref context)) = *self.context.borrow() {
// Step 1.
if let Some(CanvasContext::Context2d(ref context)) = *self.context.borrow() {
if !context.origin_is_clean() {
return Err(Error::Security);
}
}
// Step 2.
if self.Width() == 0 || self.Height() == 0 {
@ -271,11 +271,20 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement {
}
// Step 3.
let raw_data = match *self.context.borrow() {
Some(CanvasContext::Context2d(ref context)) => {
let window = window_from_node(self);
let image_data = try!(context.GetImageData(Finite::wrap(0f64), Finite::wrap(0f64),
Finite::wrap(self.Width() as f64),
Finite::wrap(self.Height() as f64)));
let raw_data = image_data.get_data_array(&GlobalRef::Window(window.r()));
image_data.get_data_array(&GlobalRef::Window(window.r()))
}
None => {
// Each pixel is fully-transparent black.
vec![0; (self.Width() * self.Height() * 4) as usize]
}
_ => return Err(Error::NotSupported) // WebGL
};
// Only handle image/png for now.
let mime_type = "image/png";
@ -288,9 +297,6 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement {
let encoded = encoded.to_base64(STANDARD);
Ok(DOMString::from(format!("data:{};base64,{}", mime_type, encoded)))
} else {
Err(Error::NotSupported)
}
}
}

View file

@ -1,5 +0,0 @@
[toDataURL.nocontext.html]
type: testharness
[toDataURL works before any context has been got]
expected: FAIL

View file

@ -18,12 +18,13 @@
<script>
var t = async_test("toDataURL works before any context has been got");
_addTest(function(canvas, ctx) {
var canvas2 = document.createElement('canvas');
var data = canvas2.toDataURL();
assert_regexp_match(data, /^data:image\/png[;,]/);
var no_context_data = canvas.toDataURL();
var ctx = canvas.getContext('2d');
ctx.rect(0, 0, 100, 50);
ctx.fillStyle = "rgba(0, 0, 0, 0)";
ctx.fill();
var data = canvas.toDataURL();
assert_equals(no_context_data, data);
});
</script>