mirror of
https://github.com/servo/servo.git
synced 2025-08-05 21:50:18 +01:00
Handle toDataURL with no context
This commit is contained in:
parent
da8952b702
commit
84ec9c4266
3 changed files with 41 additions and 39 deletions
|
@ -258,39 +258,45 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement {
|
||||||
_mime_type: Option<DOMString>,
|
_mime_type: Option<DOMString>,
|
||||||
_arguments: Vec<HandleValue>) -> Fallible<DOMString> {
|
_arguments: Vec<HandleValue>) -> Fallible<DOMString> {
|
||||||
|
|
||||||
|
// Step 1.
|
||||||
if let Some(CanvasContext::Context2d(ref context)) = *self.context.borrow() {
|
if let Some(CanvasContext::Context2d(ref context)) = *self.context.borrow() {
|
||||||
|
|
||||||
// Step 1.
|
|
||||||
if !context.origin_is_clean() {
|
if !context.origin_is_clean() {
|
||||||
return Err(Error::Security);
|
return Err(Error::Security);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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();
|
|
||||||
}
|
|
||||||
|
|
||||||
let encoded = encoded.to_base64(STANDARD);
|
|
||||||
Ok(DOMString::from(format!("data:{};base64,{}", mime_type, encoded)))
|
|
||||||
} else {
|
|
||||||
Err(Error::NotSupported)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Step 2.
|
||||||
|
if self.Width() == 0 || self.Height() == 0 {
|
||||||
|
return Ok(DOMString::from("data:,"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
[toDataURL.nocontext.html]
|
|
||||||
type: testharness
|
|
||||||
[toDataURL works before any context has been got]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
|
@ -18,12 +18,13 @@
|
||||||
<script>
|
<script>
|
||||||
var t = async_test("toDataURL works before any context has been got");
|
var t = async_test("toDataURL works before any context has been got");
|
||||||
_addTest(function(canvas, ctx) {
|
_addTest(function(canvas, ctx) {
|
||||||
|
var no_context_data = canvas.toDataURL();
|
||||||
var canvas2 = document.createElement('canvas');
|
var ctx = canvas.getContext('2d');
|
||||||
var data = canvas2.toDataURL();
|
ctx.rect(0, 0, 100, 50);
|
||||||
assert_regexp_match(data, /^data:image\/png[;,]/);
|
ctx.fillStyle = "rgba(0, 0, 0, 0)";
|
||||||
|
ctx.fill();
|
||||||
|
var data = canvas.toDataURL();
|
||||||
|
assert_equals(no_context_data, data);
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue