Skip to content
Permalink
Browse files

Handle toDataURL with no context

  • Loading branch information
dzbarsky committed Dec 24, 2015
1 parent da8952b commit 84ec9c42669709b0ce5e86da37d0850e639ab37d
@@ -258,39 +258,45 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement {
_mime_type: Option<DOMString>,
_arguments: Vec<HandleValue>) -> Fallible<DOMString> {

// Step 1.
if let Some(CanvasContext::Context2d(ref context)) = *self.context.borrow() {

// Step 1.
if !context.origin_is_clean() {
return Err(Error::Security);
}
}

// Step 2.
if self.Width() == 0 || self.Height() == 0 {
return Ok(DOMString::from("data:,"));
}
// Step 2.
if self.Width() == 0 || self.Height() == 0 {
return Ok(DOMString::from("data:,"));
}

// Step 3.
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()));

// Only handle image/png for now.
let mime_type = "image/png";

let mut encoded = Vec::new();
{
let encoder: PNGEncoder<&mut Vec<u8>> = PNGEncoder::new(&mut encoded);
encoder.encode(&raw_data, self.Width(), self.Height(), ColorType::RGBA(8)).unwrap();
// 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)));
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
};

let encoded = encoded.to_base64(STANDARD);
Ok(DOMString::from(format!("data:{};base64,{}", mime_type, encoded)))
} else {
Err(Error::NotSupported)
// Only handle image/png for now.
let mime_type = "image/png";

let mut encoded = Vec::new();
{
let encoder: PNGEncoder<&mut Vec<u8>> = PNGEncoder::new(&mut encoded);
encoder.encode(&raw_data, self.Width(), self.Height(), ColorType::RGBA(8)).unwrap();
}

let encoded = encoded.to_base64(STANDARD);
Ok(DOMString::from(format!("data:{};base64,{}", mime_type, encoded)))
}
}

This file was deleted.

@@ -18,12 +18,13 @@ <h1>toDataURL.nocontext</h1>
<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>

0 comments on commit 84ec9c4

Please sign in to comment.
You can’t perform that action at this time.