Skip to content

Truly independent web browser

LibWeb: Select picture dimension hints before image loading

Andreas Kling669b19c20960
diff --git a/Libraries/LibWeb/HTML/HTMLImageElement.cpp b/Libraries/LibWeb/HTML/HTMLImageElement.cpp
index 1abd582d1de..4769406e38d 100644
--- a/Libraries/LibWeb/HTML/HTMLImageElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLImageElement.cpp
@@ -159,6 +159,35 @@ void HTMLImageElement::initialize_element()
});
}
+// https://html.spec.whatwg.org/multipage/images.html#update-the-source-set
+static void update_dimension_attribute_source(HTMLImageElement& image)
+{
+ // INTEROP: Chrome, Firefox and Safari expose dimension hints to synchronous style queries before the deferred
+ // image update. Select only the source element here; resolving sizes could depend on layout, and
+ // currentSrc must remain deferred.
+ if (is<HTMLPictureElement>(image.parent())) {
+ for (auto* child = image.parent()->first_child(); child != &image; child = child->next_sibling()) {
+ auto* source = as_if<HTMLSourceElement>(child);
+ if (!source)
+ continue;
+ auto srcset = source->attribute(HTML::AttributeNames::srcset);
+ if (!srcset.has_value() || parse_a_srcset_attribute(*srcset).is_empty())
+ continue;
+ if (auto media = source->attribute(HTML::AttributeNames::media); media.has_value()
+ && !CSS::RustMediaList::parse(*media).evaluate(image.document()))
+ continue;
+ if (auto type = source->attribute(HTML::AttributeNames::type); type.has_value() && !is_supported_image_type(*type))
+ continue;
+
+ // 5.9. If child has width or height attributes, set el's dimension attribute source to child.
+ // Otherwise, set el's dimension attribute source to el.
+ image.set_dimension_attribute_source(source->has_attribute(HTML::AttributeNames::width) || source->has_attribute(HTML::AttributeNames::height) ? source : nullptr);
+ return;
+ }
+ }
+ image.set_dimension_attribute_source(nullptr);
+}
+
void HTMLImageElement::adopted_from(DOM::Document& old_document)
{
old_document.unregister_viewport_client(*this);
@@ -185,6 +214,8 @@ void HTMLImageElement::moved_from(IsSubtreeRoot is_subtree_root, GC::Ptr<DOM::No
// movedNode.
if (is_subtree_root == IsSubtreeRoot::Yes && is<HTMLPictureElement>(old_ancestor.ptr()))
update_the_image_data();
+ else
+ update_dimension_attribute_source(*this);
}
// https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element:html-element-removing-steps
@@ -230,6 +261,12 @@ void HTMLImageElement::set_dimension_attribute_source(DOM::Element const* source
}
}
+void HTMLImageElement::inserted()
+{
+ Base::inserted();
+ update_dimension_attribute_source(*this);
+}
+
bool HTMLImageElement::is_presentational_hint(Utf16FlyString const& name) const
{
if (Base::is_presentational_hint(name))
@@ -843,6 +880,8 @@ bool HTMLImageElement::uses_srcset_or_picture() const
// https://html.spec.whatwg.org/multipage/images.html#update-the-image-data
void HTMLImageElement::update_the_image_data(bool restart_animations, bool maybe_omit_events)
{
+ update_dimension_attribute_source(*this);
+
auto update_the_image_data_count = ++m_update_the_image_data_count;
// 1. If the element's node document is not fully active, then:
@@ -1389,6 +1428,7 @@ void HTMLImageElement::did_set_viewport_rect(CSSPixelRect const& viewport_rect)
if (viewport_rect.size() == m_last_seen_viewport_size)
return;
m_last_seen_viewport_size = viewport_rect.size();
+ update_dimension_attribute_source(*this);
batching_dispatcher().enqueue(document(), GC::create_function(GC::Heap::the(), [this] {
react_to_changes_in_the_environment();
}));
diff --git a/Libraries/LibWeb/HTML/HTMLImageElement.h b/Libraries/LibWeb/HTML/HTMLImageElement.h
index 8b8e87f4f76..09d5f37c277 100644
--- a/Libraries/LibWeb/HTML/HTMLImageElement.h
+++ b/Libraries/LibWeb/HTML/HTMLImageElement.h
@@ -146,6 +146,7 @@ private:
virtual void finalize() override;
virtual void adopted_from(DOM::Document&) override;
+ virtual void inserted() override;
virtual void moved_from(IsSubtreeRoot, GC::Ptr<DOM::Node> old_ancestor) override;
virtual void removed_from(IsSubtreeRoot, DOM::Node* old_ancestor, DOM::Node& old_root) override;
diff --git a/Tests/LibWeb/Text/expected/HTML/picture-dimension-hints-before-image-selection.txt b/Tests/LibWeb/Text/expected/HTML/picture-dimension-hints-before-image-selection.txt
index 1432687e542..09474e1f0fa 100644
--- a/Tests/LibWeb/Text/expected/HTML/picture-dimension-hints-before-image-selection.txt
+++ b/Tests/LibWeb/Text/expected/HTML/picture-dimension-hints-before-image-selection.txt
@@ -1,11 +1,13 @@
-inserted: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
+inserted: 80px, 40px, auto 80 / 40; currentSrc unchanged: true
invalid srcset: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
nonmatching media: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
unsupported type: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
-matching source: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
-width only: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
+matching source: 80px, 40px, auto 80 / 40; currentSrc unchanged: true
+width only: 80px, auto, auto; currentSrc unchanged: true
no dimensions: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
-dimensions restored: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
+dimensions restored: 60px, 20px, auto 60 / 20; currentSrc unchanged: true
source follows image: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
-source precedes image: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
+source precedes image: 60px, 20px, auto 60 / 20; currentSrc unchanged: true
source removed: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
+image outside picture: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
+image moved into picture: 60px, 20px, auto 60 / 20; currentSrc unchanged: true
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/rendering/dimension-attributes.txt b/Tests/LibWeb/Text/expected/wpt-import/html/rendering/dimension-attributes.txt
index 23224e496e3..3a8da109bf9 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/rendering/dimension-attributes.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/rendering/dimension-attributes.txt
@@ -2,8 +2,7 @@ Harness status: OK
Found 1720 tests
-1682 Pass
-38 Fail
+1720 Pass
Pass <hr width="200"> mapping to <hr> width property
Pass <hr width="1007"> mapping to <hr> width property
Pass <hr width=" 00523 "> mapping to <hr> width property
@@ -1644,25 +1643,25 @@ Pass <marquee vspace=".%"> mapping to <marquee> marginBottom property
Pass <marquee vspace=".x"> mapping to <marquee> marginBottom property
Pass <marquee vspace=".5"> mapping to <marquee> marginBottom property
Pass <marquee vspace=".5%"> mapping to <marquee> marginBottom property
-Fail <source width="200"> mapping to <img> width property
-Fail <source width="1007"> mapping to <img> width property
-Fail <source width=" 00523 "> mapping to <img> width property
-Fail <source width="200.25"> mapping to <img> width property
-Fail <source width="200.7"> mapping to <img> width property
-Fail <source width="200."> mapping to <img> width property
-Fail <source width="200in"> mapping to <img> width property
-Fail <source width="200.25in"> mapping to <img> width property
-Fail <source width="200 %"> mapping to <img> width property
-Fail <source width="200 abc"> mapping to <img> width property
-Fail <source width="200%"> mapping to <img> width property
-Fail <source width="200%abc"> mapping to <img> width property
-Fail <source width="200.25%"> mapping to <img> width property
-Fail <source width="200.%"> mapping to <img> width property
-Fail <source width="20.25e2"> mapping to <img> width property
-Fail <source width="20.25E2"> mapping to <img> width property
-Fail <source width="0"> mapping to <img> width property
-Fail <source width="0%"> mapping to <img> width property
-Fail <source width="0px"> mapping to <img> width property
+Pass <source width="200"> mapping to <img> width property
+Pass <source width="1007"> mapping to <img> width property
+Pass <source width=" 00523 "> mapping to <img> width property
+Pass <source width="200.25"> mapping to <img> width property
+Pass <source width="200.7"> mapping to <img> width property
+Pass <source width="200."> mapping to <img> width property
+Pass <source width="200in"> mapping to <img> width property
+Pass <source width="200.25in"> mapping to <img> width property
+Pass <source width="200 %"> mapping to <img> width property
+Pass <source width="200 abc"> mapping to <img> width property
+Pass <source width="200%"> mapping to <img> width property
+Pass <source width="200%abc"> mapping to <img> width property
+Pass <source width="200.25%"> mapping to <img> width property
+Pass <source width="200.%"> mapping to <img> width property
+Pass <source width="20.25e2"> mapping to <img> width property
+Pass <source width="20.25E2"> mapping to <img> width property
+Pass <source width="0"> mapping to <img> width property
+Pass <source width="0%"> mapping to <img> width property
+Pass <source width="0px"> mapping to <img> width property
Pass <source width="-0"> mapping to <img> width property
Pass <source width="-0%"> mapping to <img> width property
Pass <source width="-200"> mapping to <img> width property
@@ -1684,25 +1683,25 @@ Pass <source width=".%"> mapping to <img> width property
Pass <source width=".x"> mapping to <img> width property
Pass <source width=".5"> mapping to <img> width property
Pass <source width=".5%"> mapping to <img> width property
-Fail <source height="200"> mapping to <img> height property
-Fail <source height="1007"> mapping to <img> height property
-Fail <source height=" 00523 "> mapping to <img> height property
-Fail <source height="200.25"> mapping to <img> height property
-Fail <source height="200.7"> mapping to <img> height property
-Fail <source height="200."> mapping to <img> height property
-Fail <source height="200in"> mapping to <img> height property
-Fail <source height="200.25in"> mapping to <img> height property
-Fail <source height="200 %"> mapping to <img> height property
-Fail <source height="200 abc"> mapping to <img> height property
-Fail <source height="200%"> mapping to <img> height property
-Fail <source height="200%abc"> mapping to <img> height property
-Fail <source height="200.25%"> mapping to <img> height property
-Fail <source height="200.%"> mapping to <img> height property
-Fail <source height="20.25e2"> mapping to <img> height property
-Fail <source height="20.25E2"> mapping to <img> height property
-Fail <source height="0"> mapping to <img> height property
-Fail <source height="0%"> mapping to <img> height property
-Fail <source height="0px"> mapping to <img> height property
+Pass <source height="200"> mapping to <img> height property
+Pass <source height="1007"> mapping to <img> height property
+Pass <source height=" 00523 "> mapping to <img> height property
+Pass <source height="200.25"> mapping to <img> height property
+Pass <source height="200.7"> mapping to <img> height property
+Pass <source height="200."> mapping to <img> height property
+Pass <source height="200in"> mapping to <img> height property
+Pass <source height="200.25in"> mapping to <img> height property
+Pass <source height="200 %"> mapping to <img> height property
+Pass <source height="200 abc"> mapping to <img> height property
+Pass <source height="200%"> mapping to <img> height property
+Pass <source height="200%abc"> mapping to <img> height property
+Pass <source height="200.25%"> mapping to <img> height property
+Pass <source height="200.%"> mapping to <img> height property
+Pass <source height="20.25e2"> mapping to <img> height property
+Pass <source height="20.25E2"> mapping to <img> height property
+Pass <source height="0"> mapping to <img> height property
+Pass <source height="0%"> mapping to <img> height property
+Pass <source height="0px"> mapping to <img> height property
Pass <source height="-0"> mapping to <img> height property
Pass <source height="-0%"> mapping to <img> height property
Pass <source height="-200"> mapping to <img> height property
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/picture-aspect-ratio.txt b/Tests/LibWeb/Text/expected/wpt-import/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/picture-aspect-ratio.txt
index 97bffb76320..2d52514415a 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/picture-aspect-ratio.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/picture-aspect-ratio.txt
@@ -2,20 +2,20 @@ Harness status: OK
Found 15 tests
-6 Pass
-9 Fail
+13 Pass
+2 Fail
Pass source width and height attributes are used to infer aspect-ratio in <picture>
Fail Computed style for width/height/aspect-ratio
Fail Source width/height should take precedence over img attributes.
Pass Make sure style gets invalidated correctly when the source gets removed.
-Fail If the <source> has only one of width/height, we don't get an aspect ratio, even if the <img> has both.
+Pass If the <source> has only one of width/height, we don't get an aspect ratio, even if the <img> has both.
Pass If we don't have width/height on the source, we fall back to width/height on the <img>.
-Fail If we only have one width attribute, we should get width mapped but no aspect ratio, even if <img> has attributes.
-Fail If we only have height attribute, we should get height mapped but no aspect ratio, even if <img> has attributes.
-Fail Dynamically changing width/height should change computed style
-Fail Changing which <source> matches should change computed style
-Fail Percentages on source should be ignored for aspect-ratio but used for width/height.
-Fail Trailing garbage should be ignored but not make the attribute invalid
+Pass If we only have one width attribute, we should get width mapped but no aspect ratio, even if <img> has attributes.
+Pass If we only have height attribute, we should get height mapped but no aspect ratio, even if <img> has attributes.
+Pass Dynamically changing width/height should change computed style
+Pass Changing which <source> matches should change computed style
+Pass Percentages on source should be ignored for aspect-ratio but used for width/height.
+Pass Trailing garbage should be ignored but not make the attribute invalid
Pass Loaded picture test: Both <source> and <img> are without width and height attributes
Pass Loaded picture test: <source> with width and height attributes, <img> without width and height attributes
Pass Loaded picture test: Both <source> and <img> are with width and height attributes
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/HTML/picture-dimension-hints-before-image-selection.html b/Tests/LibWeb/Text/input/HTML/picture-dimension-hints-before-image-selection.html
index 49328776e50..aaed89acfa2 100644
--- a/Tests/LibWeb/Text/input/HTML/picture-dimension-hints-before-image-selection.html
+++ b/Tests/LibWeb/Text/input/HTML/picture-dimension-hints-before-image-selection.html
@@ -46,5 +46,10 @@
printHints("source precedes image");
source.remove();
printHints("source removed");
+ document.body.moveBefore(image, null);
+ printHints("image outside picture");
+ picture.prepend(source);
+ picture.moveBefore(image, null);
+ printHints("image moved into picture");
});
</script>