diff --git a/components/layout/construct.rs b/components/layout/construct.rs index dd4ef419bbd..36ab116d0ca 100644 --- a/components/layout/construct.rs +++ b/components/layout/construct.rs @@ -400,6 +400,7 @@ impl<'a, ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNode> Some(LayoutNodeType::Element(LayoutElementType::HTMLImageElement)) => { let image_info = Box::new(ImageFragmentInfo::new( node.image_url(), + node.image_density(), node, &self.layout_context, )); @@ -408,6 +409,7 @@ impl<'a, ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNode> Some(LayoutNodeType::Element(LayoutElementType::HTMLObjectElement)) => { let image_info = Box::new(ImageFragmentInfo::new( node.object_data(), + None, node, &self.layout_context, )); @@ -1471,6 +1473,7 @@ impl<'a, ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNode> ImageUrlOrNone::Url(ref url_value) => { let image_info = Box::new(ImageFragmentInfo::new( url_value.url().map(|u| u.clone()), + None, node, &self.layout_context, )); diff --git a/components/layout/fragment.rs b/components/layout/fragment.rs index 2e952a501fb..65fd277fdde 100644 --- a/components/layout/fragment.rs +++ b/components/layout/fragment.rs @@ -393,6 +393,7 @@ impl ImageFragmentInfo { /// sense to me. pub fn new( url: Option, + density: Option, node: &N, layout_context: &LayoutContext, ) -> ImageFragmentInfo { @@ -400,15 +401,33 @@ impl ImageFragmentInfo { layout_context.get_or_request_image_or_meta(node.opaque(), url, UsePlaceholder::Yes) }); + let current_pixel_density = density.unwrap_or(1f64); + let (image, metadata) = match image_or_metadata { - Some(ImageOrMetadataAvailable::ImageAvailable(i, _)) => ( - Some(i.clone()), - Some(ImageMetadata { - height: i.height, - width: i.width, - }), - ), - Some(ImageOrMetadataAvailable::MetadataAvailable(m)) => (None, Some(m)), + Some(ImageOrMetadataAvailable::ImageAvailable(i, _)) => { + let height = (i.height as f64 / current_pixel_density) as u32; + let width = (i.width as f64 / current_pixel_density) as u32; + ( + Some(Arc::new(Image { + height: height, + width: width, + ..(*i).clone() + })), + Some(ImageMetadata { + height: height, + width: width, + }), + ) + }, + Some(ImageOrMetadataAvailable::MetadataAvailable(m)) => { + ( + None, + Some(ImageMetadata { + height: (m.height as f64 / current_pixel_density) as u32, + width: (m.width as f64 / current_pixel_density) as u32, + }), + ) + }, None => (None, None), }; diff --git a/components/layout_thread/dom_wrapper.rs b/components/layout_thread/dom_wrapper.rs index 204ff023c97..8fecbdbf53a 100644 --- a/components/layout_thread/dom_wrapper.rs +++ b/components/layout_thread/dom_wrapper.rs @@ -1041,6 +1041,11 @@ impl<'ln> ThreadSafeLayoutNode for ServoThreadSafeLayoutNode<'ln> { this.image_url() } + fn image_density(&self) -> Option { + let this = unsafe { self.get_jsmanaged() }; + this.image_density() + } + fn canvas_data(&self) -> Option { let this = unsafe { self.get_jsmanaged() }; this.canvas_data() diff --git a/components/script/dom/htmlimageelement.rs b/components/script/dom/htmlimageelement.rs index 81d328cc968..1c49185ad0d 100644 --- a/components/script/dom/htmlimageelement.rs +++ b/components/script/dom/htmlimageelement.rs @@ -706,7 +706,7 @@ impl HTMLImageElement { } /// Step 13-17 of html.spec.whatwg.org/multipage/#update-the-image-data - fn prepare_image_request(&self, url: &ServoUrl, src: &DOMString) { + fn prepare_image_request(&self, url: &ServoUrl, src: &DOMString, selected_pixel_density: f64) { match self.image_request.get() { ImageRequestPhase::Pending => { if let Some(pending_url) = self.pending_request.borrow().parsed_url.clone() { @@ -731,15 +731,18 @@ impl HTMLImageElement { // TODO: queue a task to restart animation, if restart-animation is set return } + pending_request.current_pixel_density = Some(selected_pixel_density); self.image_request.set(ImageRequestPhase::Pending); self.init_image_request(&mut pending_request, &url, &src); }, (_, State::Broken) | (_, State::Unavailable) => { // Step 17 + current_request.current_pixel_density = Some(selected_pixel_density); self.init_image_request(&mut current_request, &url, &src); }, (_, _) => { // step 17 + pending_request.current_pixel_density = Some(selected_pixel_density); self.image_request.set(ImageRequestPhase::Pending); self.init_image_request(&mut pending_request, &url, &src); }, @@ -755,12 +758,9 @@ impl HTMLImageElement { let window = document.window(); let task_source = window.dom_manipulation_task_source(); let this = Trusted::new(self); - let src = match self.select_image_source() { - Some(src) => { - // Step 8 - // TODO: Handle pixel density. - src.0 - }, + let (src, pixel_density) = match self.select_image_source() { + // Step 8 + Some(data) => data, None => { // Step 9. // FIXME(nox): Why are errors silenced here? @@ -816,7 +816,7 @@ impl HTMLImageElement { match parsed_url { Ok(url) => { // Step 13-17 - self.prepare_image_request(&url, &src); + self.prepare_image_request(&url, &src, pixel_density); }, Err(_) => { // Step 12.1-12.5. @@ -1231,6 +1231,9 @@ pub trait LayoutHTMLImageElementHelpers { #[allow(unsafe_code)] unsafe fn image_url(&self) -> Option; + #[allow(unsafe_code)] + unsafe fn image_density(&self) -> Option; + fn get_width(&self) -> LengthOrPercentageOrAuto; fn get_height(&self) -> LengthOrPercentageOrAuto; } @@ -1246,6 +1249,11 @@ impl LayoutHTMLImageElementHelpers for LayoutDom { (*self.unsafe_get()).current_request.borrow_for_layout().parsed_url.clone() } + #[allow(unsafe_code)] + unsafe fn image_density(&self) -> Option { + (*self.unsafe_get()).current_request.borrow_for_layout().current_pixel_density.clone() + } + #[allow(unsafe_code)] fn get_width(&self) -> LengthOrPercentageOrAuto { unsafe { @@ -1350,20 +1358,22 @@ impl HTMLImageElementMethods for HTMLImageElement { // https://html.spec.whatwg.org/multipage/#dom-img-naturalwidth fn NaturalWidth(&self) -> u32 { - let ref metadata = self.current_request.borrow().metadata; + let request = self.current_request.borrow(); + let pixel_density = request.current_pixel_density.unwrap_or(1f64); - match *metadata { - Some(ref metadata) => metadata.width, + match request.metadata { + Some(ref metadata) => (metadata.width as f64 / pixel_density) as u32, None => 0, } } // https://html.spec.whatwg.org/multipage/#dom-img-naturalheight fn NaturalHeight(&self) -> u32 { - let ref metadata = self.current_request.borrow().metadata; + let request = self.current_request.borrow(); + let pixel_density = request.current_pixel_density.unwrap_or(1f64); - match *metadata { - Some(ref metadata) => metadata.height, + match request.metadata { + Some(ref metadata) => (metadata.height as f64 / pixel_density) as u32, None => 0, } } diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index 09c4930ee29..c94f590c42b 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -1030,6 +1030,7 @@ pub trait LayoutNodeHelpers { fn text_content(&self) -> String; fn selection(&self) -> Option>; fn image_url(&self) -> Option; + fn image_density(&self) -> Option; fn canvas_data(&self) -> Option; fn svg_data(&self) -> Option; fn iframe_browsing_context_id(&self) -> Option; @@ -1173,6 +1174,15 @@ impl LayoutNodeHelpers for LayoutDom { } } + #[allow(unsafe_code)] + fn image_density(&self) -> Option { + unsafe { + self.downcast::() + .expect("not an image!") + .image_density() + } + } + fn canvas_data(&self) -> Option { self.downcast::() .map(|canvas| canvas.data()) diff --git a/components/script_layout_interface/wrapper_traits.rs b/components/script_layout_interface/wrapper_traits.rs index a23a417cb6d..23f9ff4a3b8 100644 --- a/components/script_layout_interface/wrapper_traits.rs +++ b/components/script_layout_interface/wrapper_traits.rs @@ -249,6 +249,9 @@ pub trait ThreadSafeLayoutNode: Clone + Copy + Debug + GetLayoutData + NodeInfo /// If this is an image element, returns its URL. If this is not an image element, fails. fn image_url(&self) -> Option; + /// If this is an image element, returns its current-pixel-density. If this is not an image element, fails. + fn image_density(&self) -> Option; + fn canvas_data(&self) -> Option; fn svg_data(&self) -> Option; diff --git a/tests/wpt/metadata/html/semantics/embedded-content/the-img-element/adoption.html.ini b/tests/wpt/metadata/html/semantics/embedded-content/the-img-element/adoption.html.ini index 0cc1f123d22..21672e83393 100644 --- a/tests/wpt/metadata/html/semantics/embedded-content/the-img-element/adoption.html.ini +++ b/tests/wpt/metadata/html/semantics/embedded-content/the-img-element/adoption.html.ini @@ -1,5 +1,6 @@ [adoption.html] type: testharness + expected: TIMEOUT [adoption is from appendChild] expected: FAIL diff --git a/tests/wpt/metadata/html/semantics/embedded-content/the-img-element/current-pixel-density/basic.html.ini b/tests/wpt/metadata/html/semantics/embedded-content/the-img-element/current-pixel-density/basic.html.ini index 875fd3365f3..7304eeb41fb 100644 --- a/tests/wpt/metadata/html/semantics/embedded-content/the-img-element/current-pixel-density/basic.html.ini +++ b/tests/wpt/metadata/html/semantics/embedded-content/the-img-element/current-pixel-density/basic.html.ini @@ -1,20 +1,5 @@ [basic.html] type: testharness - [] - expected: FAIL - - [] - expected: FAIL - - [] - expected: FAIL - - [] - expected: FAIL - - [] - expected: FAIL - [] expected: FAIL @@ -24,9 +9,3 @@ [] expected: FAIL - [] - expected: FAIL - - [] - expected: FAIL - diff --git a/tests/wpt/metadata/html/semantics/embedded-content/the-img-element/non-active-document.html.ini b/tests/wpt/metadata/html/semantics/embedded-content/the-img-element/non-active-document.html.ini index 6dc30b21283..d129c20cf72 100644 --- a/tests/wpt/metadata/html/semantics/embedded-content/the-img-element/non-active-document.html.ini +++ b/tests/wpt/metadata/html/semantics/embedded-content/the-img-element/non-active-document.html.ini @@ -1,5 +1,6 @@ [non-active-document.html] type: testharness + expected: CRASH [DOMParser] expected: FAIL