Skip to content

Commit

Permalink
Add image usability checks to drawImage()
Browse files Browse the repository at this point in the history
  • Loading branch information
pylbrecht committed Feb 3, 2020
1 parent 5f55cd5 commit ed0973f
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 34 deletions.
8 changes: 8 additions & 0 deletions components/script/canvas_state.rs
Expand Up @@ -388,6 +388,14 @@ impl CanvasState {
self.draw_offscreen_canvas(&canvas, htmlcanvas, sx, sy, sw, sh, dx, dy, dw, dh)
},
CanvasImageSource::HTMLImageElement(ref image) => {
// https://html.spec.whatwg.org/multipage/#drawing-images
// 2. Let usability be the result of checking the usability of image.
// 3. If usability is bad, then return (without drawing anything).
if !image.is_usable()? {
return Ok(());
}

// TODO(pylbrecht): is it possible for image.get_url() to return None after the usability check?
// https://html.spec.whatwg.org/multipage/#img-error
// If the image argument is an HTMLImageElement object that is in the broken state,
// then throw an InvalidStateError exception
Expand Down
22 changes: 21 additions & 1 deletion components/script/dom/htmlimageelement.rs
Expand Up @@ -13,7 +13,7 @@ use crate::dom::bindings::codegen::Bindings::HTMLImageElementBinding::HTMLImageE
use crate::dom::bindings::codegen::Bindings::MouseEventBinding::MouseEventMethods;
use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeBinding::NodeMethods;
use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::reflector::DomObject;
Expand Down Expand Up @@ -164,6 +164,26 @@ impl HTMLImageElement {
pub fn get_url(&self) -> Option<ServoUrl> {
self.current_request.borrow().parsed_url.clone()
}
// https://html.spec.whatwg.org/multipage/#check-the-usability-of-the-image-argument
pub fn is_usable(&self) -> Fallible<bool> {
// If image has an intrinsic width or intrinsic height (or both) equal to zero, then return bad.
match &self.current_request.borrow().image {
Some(image) => {
if image.width == 0 || image.height == 0 {
return Ok(false);
}
},
None => return Ok(false),
}

match self.current_request.borrow().state {
// If image's current request's state is broken, then throw an "InvalidStateError" DOMException.
State::Broken => Err(Error::InvalidState),
State::CompletelyAvailable => Ok(true),
// If image is not fully decodable, then return bad.
State::PartiallyAvailable | State::Unavailable => Ok(false),
}
}
}

/// The context required for asynchronously loading an external image.
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

@@ -1,8 +1,5 @@
[drawimage_html_image.html]
type: testharness
[Draw 100x100 image to 100x100 canvas at 0,0.]
expected: FAIL

[Test scenario 12: sx = -20, sy = -20, sw = 50, sh = 50, dx = 20, dy = 20, dw = 125, dh = 125 --- Pixel 70,99 should be light purple.]
expected: FAIL

Expand Down

0 comments on commit ed0973f

Please sign in to comment.