Truly independent web browser
LibWeb: Resolve sizes=auto from the rendered img
Andreas Klingf0506b239e41
diff --git a/Libraries/LibWeb/CSS/Default.css b/Libraries/LibWeb/CSS/Default.cssindex b55642031dc..770593ae131 100644--- a/Libraries/LibWeb/CSS/Default.css+++ b/Libraries/LibWeb/CSS/Default.css@@ -754,6 +754,15 @@ audio {width: 300px;}+/* 15.4.2 Images+ * https://html.spec.whatwg.org/multipage/rendering.html#img-contain-size+ */++img:is([sizes="auto" i], [sizes^="auto," i]) {+ contain: size !important;+ contain-intrinsic-size: 300px 150px;+}+/* 15.4.3 Attributes for embedded content and images* https://html.spec.whatwg.org/multipage/rendering.html#attributes-for-embedded-content-and-images*/diff --git a/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Libraries/LibWeb/CSS/Parser/Parser.cppindex a2940d47f06..74ce75e2554 100644--- a/Libraries/LibWeb/CSS/Parser/Parser.cpp+++ b/Libraries/LibWeb/CSS/Parser/Parser.cpp@@ -13,7 +13,6 @@*/#include <LibWeb/CSS/Parser/Parser.h>-#include <LibWeb/CSS/Sizing.h>#include <LibWeb/CSS/StyleSheetState.h>#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>#include <LibWeb/DOM/Document.h>@@ -99,18 +98,23 @@ RefPtr<StyleValue const> Parser::parse_as_css_value(Utf16View source, PropertyID// https://html.spec.whatwg.org/multipage/images.html#parsing-a-sizes-attributestatic Optional<double> sizes_attribute_auto_width(HTML::HTMLImageElement const* img){- // FIXME: "img is being rendered" - we just see if it has image data for now.- if (!img || !img->is_image_available() || !img->allows_auto_sizes())+ // If size is auto, and img is not null, and img allows auto-sizes:+ if (!img || !img->allows_auto_sizes())return {};- // FIXME: The spec doesn't seem to tell us how to determine the concrete size of an <img>, so use the default sizing algorithm.- // Should this use some of the methods from FormattingContext?- auto concrete_size = run_default_sizing_algorithm(- img->width(), img->height(),- { img->natural_width(), img->natural_height(), img->intrinsic_aspect_ratio() },- // NOTE: https://html.spec.whatwg.org/multipage/rendering.html#img-contain-size- CSSPixelSize { 300, 150 });- return concrete_size.width().to_double();+ // 1. If img is being rendered, then set size to the concrete object size width of img, in CSS pixels, and set img's+ // last auto-sizes width to size.+ // NB: A bare auto must come first for img to allow auto-sizes, so the parser always reaches it.+ if (auto concrete_object_size_width = img->concrete_object_size_width_if_rendered(); concrete_object_size_width.has_value()) {+ img->set_last_auto_sizes_width(*concrete_object_size_width);+ return concrete_object_size_width->to_double();+ }++ // 2. Otherwise, if img's last auto-sizes width is not null, then set size to img's last auto-sizes width.+ if (auto last_auto_sizes_width = img->last_auto_sizes_width(); last_auto_sizes_width.has_value())+ return last_auto_sizes_width->to_double();++ return {};}// AD-HOC: If the sizes are empty, this algorithm always logs a parse error and then returns 100vw.diff --git a/Libraries/LibWeb/HTML/HTMLImageElement.cpp b/Libraries/LibWeb/HTML/HTMLImageElement.cppindex 1ba9bd4f162..6535abf7e70 100644--- a/Libraries/LibWeb/HTML/HTMLImageElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLImageElement.cpp@@ -197,7 +197,8 @@ void HTMLImageElement::removed_from(IsSubtreeRoot is_subtree_root, DOM::Node* olif (is_subtree_root == IsSubtreeRoot::Yes && is<HTMLPictureElement>(old_ancestor))update_the_image_data();- // FIXME: 2. Set removedNode's last auto-sizes width to null.+ // 2. Set removedNode's last auto-sizes width to null.+ m_last_auto_sizes_width = {};}void HTMLImageElement::visit_edges(Cell::Visitor& visitor)@@ -1714,6 +1715,19 @@ void HTMLImageElement::set_source_set(SourceSet source_set)m_source_set = move(source_set);}+Optional<CSSPixels> HTMLImageElement::concrete_object_size_width_if_rendered() const+{+ Layout::ForcedReadScope read { document() };+ const_cast<DOM::Document&>(document()).update_layout_if_needed_for_node(*this, DOM::UpdateLayoutReason::HTMLImageElementWidth);++ auto const* layout_node = this->layout_node(read);+ if (!layout_node || !Painting::has_committed_box(*layout_node))+ return {};++ // FIXME: Account for object-fit, which can make the concrete object size differ from the content box.+ return Painting::content_width(*layout_node);+}+bool HTMLImageElement::allows_auto_sizes() const{// An img element allows auto-sizes if:diff --git a/Libraries/LibWeb/HTML/HTMLImageElement.h b/Libraries/LibWeb/HTML/HTMLImageElement.hindex b9f39681849..5ff270d0038 100644--- a/Libraries/LibWeb/HTML/HTMLImageElement.h+++ b/Libraries/LibWeb/HTML/HTMLImageElement.h@@ -109,6 +109,12 @@ public:// https://html.spec.whatwg.org/multipage/embedded-content.html#allows-auto-sizesbool allows_auto_sizes() const;+ Optional<CSSPixels> concrete_object_size_width_if_rendered() const;++ // https://html.spec.whatwg.org/multipage/images.html#last-auto-sizes-width+ Optional<CSSPixels> last_auto_sizes_width() const { return m_last_auto_sizes_width; }+ void set_last_auto_sizes_width(CSSPixels width) const { m_last_auto_sizes_width = width; }+// ^Layout::ImageProvidervirtual bool is_image_pending() const override;@@ -168,6 +174,10 @@ private:CORSSettingAttribute m_cors_setting { CORSSettingAttribute::NoCORS };+ // https://html.spec.whatwg.org/multipage/images.html#last-auto-sizes-width+ // NB: Parsing a sizes attribute records this through a const img, so it is mutable.+ mutable Optional<CSSPixels> m_last_auto_sizes_width;+// https://html.spec.whatwg.org/multipage/images.html#last-selected-source// Each img element has a last selected source, which must initially be null.Optional<Utf16String> m_last_selected_source;diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/sizes/sizes-auto.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/sizes/sizes-auto.txtindex 9d21a2967c2..d213243e1c1 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/sizes/sizes-auto.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/sizes/sizes-auto.txt@@ -2,15 +2,15 @@ Harness status: OKFound 74 tests-38 Pass-36 Fail+56 Pass+18 FailPass <img sizes="1px" data-ref="ref1" srcset="/images/green-1x1.png?img1 50w, /images/green-16x16.png?img1 51w">Pass <picture><source sizes="1px" srcset="/images/green-1x1.png?picture1 50w, /images/green-16x16.png?picture1 51w"><img data-ref="ref1"></picture>Pass <img sizes="" data-ref="ref2" srcset="/images/green-1x1.png?img2 50w, /images/green-16x16.png?img2 51w">Pass <picture><source sizes="" srcset="/images/green-1x1.png?picture2 50w, /images/green-16x16.png?picture2 51w"><img data-ref="ref2"></picture>-Fail <img loading="lazy" sizes="auto" width="10" data-ref="ref1" srcset="/images/green-1x1.png?img3 50w, /images/green-16x16.png?img3 51w">+Pass <img loading="lazy" sizes="auto" width="10" data-ref="ref1" srcset="/images/green-1x1.png?img3 50w, /images/green-16x16.png?img3 51w">Fail <picture><source srcset="/images/green-1x1.png?picture3 50w, /images/green-16x16.png?picture3 51w"><img loading="lazy" sizes="auto" width="10" data-ref="ref1"></picture>-Fail <img loading="lazy" sizes="AUTO" width="10" data-ref="ref1" srcset="/images/green-1x1.png?img4 50w, /images/green-16x16.png?img4 51w">+Pass <img loading="lazy" sizes="AUTO" width="10" data-ref="ref1" srcset="/images/green-1x1.png?img4 50w, /images/green-16x16.png?img4 51w">Fail <picture><source srcset="/images/green-1x1.png?picture4 50w, /images/green-16x16.png?picture4 51w"><img loading="lazy" sizes="AUTO" width="10" data-ref="ref1"></picture>Pass <img loading="lazy" width="10" data-ref="ref2" srcset="/images/green-1x1.png?img5 50w, /images/green-16x16.png?img5 51w">Pass <picture><source srcset="/images/green-1x1.png?picture5 50w, /images/green-16x16.png?picture5 51w"><img loading="lazy" width="10" data-ref="ref2"></picture>@@ -22,53 +22,53 @@ Pass <img loading="lazy" style="width: 100%; max-width: 10px" data-ref="ref2" srPass <picture><source srcset="/images/green-1x1.png?picture8 50w, /images/green-16x16.png?picture8 51w"><img loading="lazy" style="width: 100%; max-width: 10px" data-ref="ref2"></picture>Pass <img loading="lazy" sizes="auto" width="500" data-ref="ref2" srcset="/images/green-1x1.png?img9 50w, /images/green-16x16.png?img9 51w">Pass <picture><source srcset="/images/green-1x1.png?picture9 50w, /images/green-16x16.png?picture9 51w"><img loading="lazy" sizes="auto" width="500" data-ref="ref2"></picture>-Fail <img loading="lazy" sizes="auto" width="10" style="visibility: hidden" data-ref="ref1" srcset="/images/green-1x1.png?img10 50w, /images/green-16x16.png?img10 51w">+Pass <img loading="lazy" sizes="auto" width="10" style="visibility: hidden" data-ref="ref1" srcset="/images/green-1x1.png?img10 50w, /images/green-16x16.png?img10 51w">Fail <picture><source srcset="/images/green-1x1.png?picture10 50w, /images/green-16x16.png?picture10 51w"><img loading="lazy" sizes="auto" width="10" style="visibility: hidden" data-ref="ref1"></picture>-Fail <img loading="lazy" sizes="auto" width="10" style="display: inline" hidden="" data-ref="ref1" srcset="/images/green-1x1.png?img11 50w, /images/green-16x16.png?img11 51w">+Pass <img loading="lazy" sizes="auto" width="10" style="display: inline" hidden="" data-ref="ref1" srcset="/images/green-1x1.png?img11 50w, /images/green-16x16.png?img11 51w">Fail <picture><source srcset="/images/green-1x1.png?picture11 50w, /images/green-16x16.png?picture11 51w"><img loading="lazy" sizes="auto" width="10" style="display: inline" hidden="" data-ref="ref1"></picture>-Fail <img loading="lazy" sizes="auto" width="0" data-ref="ref1" srcset="/images/green-1x1.png?img12 50w, /images/green-16x16.png?img12 51w">+Pass <img loading="lazy" sizes="auto" width="0" data-ref="ref1" srcset="/images/green-1x1.png?img12 50w, /images/green-16x16.png?img12 51w">Fail <picture><source srcset="/images/green-1x1.png?picture12 50w, /images/green-16x16.png?picture12 51w"><img loading="lazy" sizes="auto" width="0" data-ref="ref1"></picture>-Fail <img loading="lazy" sizes="auto" style="width: 0px" data-ref="ref1" srcset="/images/green-1x1.png?img13 50w, /images/green-16x16.png?img13 51w">+Pass <img loading="lazy" sizes="auto" style="width: 0px" data-ref="ref1" srcset="/images/green-1x1.png?img13 50w, /images/green-16x16.png?img13 51w">Fail <picture><source srcset="/images/green-1x1.png?picture13 50w, /images/green-16x16.png?picture13 51w"><img loading="lazy" sizes="auto" style="width: 0px" data-ref="ref1"></picture>Pass <img loading="lazy" sizes="auto" data-ref="ref2" srcset="/images/green-1x1.png?img14 50w, /images/green-16x16.png?img14 51w">Pass <picture><source srcset="/images/green-1x1.png?picture14 50w, /images/green-16x16.png?picture14 51w"><img loading="lazy" sizes="auto" data-ref="ref2"></picture>-Fail <img loading="lazy" sizes="auto, 100vw" width="10" data-ref="ref1" srcset="/images/green-1x1.png?img15 50w, /images/green-16x16.png?img15 51w">+Pass <img loading="lazy" sizes="auto, 100vw" width="10" data-ref="ref1" srcset="/images/green-1x1.png?img15 50w, /images/green-16x16.png?img15 51w">Fail <picture><source srcset="/images/green-1x1.png?picture15 50w, /images/green-16x16.png?picture15 51w"><img loading="lazy" sizes="auto, 100vw" width="10" data-ref="ref1"></picture>Pass <img loading="lazy" sizes="100vw, auto" width="10" data-ref="ref2" srcset="/images/green-1x1.png?img16 50w, /images/green-16x16.png?img16 51w">Pass <picture><source sizes="100vw, auto" srcset="/images/green-1x1.png?picture16 50w, /images/green-16x16.png?picture16 51w"><img loading="lazy" width="10" data-ref="ref2"></picture>Pass <img loading="eager" sizes="auto" width="10" data-ref="ref2" srcset="/images/green-1x1.png?img17 50w, /images/green-16x16.png?img17 51w">Pass <picture><source srcset="/images/green-1x1.png?picture17 50w, /images/green-16x16.png?picture17 51w"><img loading="eager" sizes="auto" width="10" data-ref="ref2"></picture>-Fail <img loading="lazy" sizes="auto" width="100%" data-ref="ref1" srcset="/images/green-1x1.png?img18 50w, /images/green-16x16.png?img18 51w">+Pass <img loading="lazy" sizes="auto" width="100%" data-ref="ref1" srcset="/images/green-1x1.png?img18 50w, /images/green-16x16.png?img18 51w">Fail <picture><source srcset="/images/green-1x1.png?picture18 50w, /images/green-16x16.png?picture18 51w"><img loading="lazy" sizes="auto" width="100%" data-ref="ref1"></picture>Pass <img loading="lazy" sizes="auto" width="100%" data-ref="ref2" srcset="/images/green-1x1.png?img19 50w, /images/green-16x16.png?img19 51w">Pass <picture><source srcset="/images/green-1x1.png?picture19 50w, /images/green-16x16.png?picture19 51w"><img loading="lazy" sizes="auto" width="100%" data-ref="ref2"></picture>-Fail <img loading="lazy" sizes="auto" style="height: 10px; aspect-ratio: 10 / 10" data-ref="ref1" srcset="/images/green-1x1.png?img20 50w, /images/green-16x16.png?img20 51w">+Pass <img loading="lazy" sizes="auto" style="height: 10px; aspect-ratio: 10 / 10" data-ref="ref1" srcset="/images/green-1x1.png?img20 50w, /images/green-16x16.png?img20 51w">Fail <picture><source srcset="/images/green-1x1.png?picture20 50w, /images/green-16x16.png?picture20 51w"><img loading="lazy" sizes="auto" style="height: 10px; aspect-ratio: 10 / 10" data-ref="ref1"></picture>Pass <img loading="lazy" sizes="auto" style="height: 10px; aspect-ratio: 500 / 10" data-ref="ref2" srcset="/images/green-1x1.png?img21 50w, /images/green-16x16.png?img21 51w">Pass <picture><source srcset="/images/green-1x1.png?picture21 50w, /images/green-16x16.png?picture21 51w"><img loading="lazy" sizes="auto" style="height: 10px; aspect-ratio: 500 / 10" data-ref="ref2"></picture>-Fail <img loading="lazy" sizes="auto" style="min-height: 10px; aspect-ratio: 10 / 10" data-ref="ref1" srcset="/images/green-1x1.png?img22 50w, /images/green-16x16.png?img22 51w">+Pass <img loading="lazy" sizes="auto" style="min-height: 10px; aspect-ratio: 10 / 10" data-ref="ref1" srcset="/images/green-1x1.png?img22 50w, /images/green-16x16.png?img22 51w">Fail <picture><source srcset="/images/green-1x1.png?picture22 50w, /images/green-16x16.png?picture22 51w"><img loading="lazy" sizes="auto" style="min-height: 10px; aspect-ratio: 10 / 10" data-ref="ref1"></picture>Pass <img loading="lazy" sizes="auto" style="min-height: 10px; aspect-ratio: 500 / 10" data-ref="ref2" srcset="/images/green-1x1.png?img23 50w, /images/green-16x16.png?img23 51w">Pass <picture><source srcset="/images/green-1x1.png?picture23 50w, /images/green-16x16.png?picture23 51w"><img loading="lazy" sizes="auto" style="min-height: 10px; aspect-ratio: 500 / 10" data-ref="ref2"></picture>-Fail <img loading="lazy" sizes="auto" style="inline-size: 10px" data-ref="ref1" srcset="/images/green-1x1.png?img24 50w, /images/green-16x16.png?img24 51w">+Pass <img loading="lazy" sizes="auto" style="inline-size: 10px" data-ref="ref1" srcset="/images/green-1x1.png?img24 50w, /images/green-16x16.png?img24 51w">Fail <picture><source srcset="/images/green-1x1.png?picture24 50w, /images/green-16x16.png?picture24 51w"><img loading="lazy" sizes="auto" style="inline-size: 10px" data-ref="ref1"></picture>Pass <img loading="lazy" sizes="auto" style="min-inline-size: 10px" data-ref="ref2" srcset="/images/green-1x1.png?img25 50w, /images/green-16x16.png?img25 51w">Pass <picture><source srcset="/images/green-1x1.png?picture25 50w, /images/green-16x16.png?picture25 51w"><img loading="lazy" sizes="auto" style="min-inline-size: 10px" data-ref="ref2"></picture>-Fail <img loading="lazy" sizes="auto" style="block-size: 10px; aspect-ratio: 10 / 10" data-ref="ref1" srcset="/images/green-1x1.png?img26 50w, /images/green-16x16.png?img26 51w">+Pass <img loading="lazy" sizes="auto" style="block-size: 10px; aspect-ratio: 10 / 10" data-ref="ref1" srcset="/images/green-1x1.png?img26 50w, /images/green-16x16.png?img26 51w">Fail <picture><source srcset="/images/green-1x1.png?picture26 50w, /images/green-16x16.png?picture26 51w"><img loading="lazy" sizes="auto" style="block-size: 10px; aspect-ratio: 10 / 10" data-ref="ref1"></picture>-Fail <img loading="lazy" sizes="auto" style="min-block-size: 10px; aspect-ratio: 10 / 10" data-ref="ref1" srcset="/images/green-1x1.png?img27 50w, /images/green-16x16.png?img27 51w">+Pass <img loading="lazy" sizes="auto" style="min-block-size: 10px; aspect-ratio: 10 / 10" data-ref="ref1" srcset="/images/green-1x1.png?img27 50w, /images/green-16x16.png?img27 51w">Fail <picture><source srcset="/images/green-1x1.png?picture27 50w, /images/green-16x16.png?picture27 51w"><img loading="lazy" sizes="auto" style="min-block-size: 10px; aspect-ratio: 10 / 10" data-ref="ref1"></picture>-Fail <img loading="lazy" sizes="auto" style="block-size: 10px; writing-mode: vertical-rl" data-ref="ref1" srcset="/images/green-1x1.png?img28 50w, /images/green-16x16.png?img28 51w">+Pass <img loading="lazy" sizes="auto" style="block-size: 10px; writing-mode: vertical-rl" data-ref="ref1" srcset="/images/green-1x1.png?img28 50w, /images/green-16x16.png?img28 51w">Fail <picture><source srcset="/images/green-1x1.png?picture28 50w, /images/green-16x16.png?picture28 51w"><img loading="lazy" sizes="auto" style="block-size: 10px; writing-mode: vertical-rl" data-ref="ref1"></picture>Pass <img loading="lazy" sizes="auto" style="min-block-size: 10px; writing-mode: vertical-rl" data-ref="ref2" srcset="/images/green-1x1.png?img29 50w, /images/green-16x16.png?img29 51w">Pass <picture><source srcset="/images/green-1x1.png?picture29 50w, /images/green-16x16.png?picture29 51w"><img loading="lazy" sizes="auto" style="min-block-size: 10px; writing-mode: vertical-rl" data-ref="ref2"></picture>-Fail <img loading="lazy" sizes="auto" style="inline-size: 10px; aspect-ratio: 10/10; writing-mode: vertical-rl" data-ref="ref1" srcset="/images/green-1x1.png?img30 50w, /images/green-16x16.png?img30 51w">+Pass <img loading="lazy" sizes="auto" style="inline-size: 10px; aspect-ratio: 10/10; writing-mode: vertical-rl" data-ref="ref1" srcset="/images/green-1x1.png?img30 50w, /images/green-16x16.png?img30 51w">Fail <picture><source srcset="/images/green-1x1.png?picture30 50w, /images/green-16x16.png?picture30 51w"><img loading="lazy" sizes="auto" style="inline-size: 10px; aspect-ratio: 10/10; writing-mode: vertical-rl" data-ref="ref1"></picture>-Fail <img loading="lazy" sizes="auto" style="min-inline-size: 10px; aspect-ratio: 10/10; writing-mode: vertical-rl" data-ref="ref1" srcset="/images/green-1x1.png?img31 50w, /images/green-16x16.png?img31 51w">+Pass <img loading="lazy" sizes="auto" style="min-inline-size: 10px; aspect-ratio: 10/10; writing-mode: vertical-rl" data-ref="ref1" srcset="/images/green-1x1.png?img31 50w, /images/green-16x16.png?img31 51w">Fail <picture><source srcset="/images/green-1x1.png?picture31 50w, /images/green-16x16.png?picture31 51w"><img loading="lazy" sizes="auto" style="min-inline-size: 10px; aspect-ratio: 10/10; writing-mode: vertical-rl" data-ref="ref1"></picture>-Fail <img loading="lazy" sizes="auto" style="--my-width: 10px; width: var(--my-width)" data-ref="ref1" srcset="/images/green-1x1.png?img32 50w, /images/green-16x16.png?img32 51w">+Pass <img loading="lazy" sizes="auto" style="--my-width: 10px; width: var(--my-width)" data-ref="ref1" srcset="/images/green-1x1.png?img32 50w, /images/green-16x16.png?img32 51w">Fail <picture><source srcset="/images/green-1x1.png?picture32 50w, /images/green-16x16.png?picture32 51w"><img loading="lazy" sizes="auto" style="--my-width: 10px; width: var(--my-width)" data-ref="ref1"></picture>-Fail <img loading="lazy" sizes="auto" style="width: calc(5px + 5px)" data-ref="ref1" srcset="/images/green-1x1.png?img33 50w, /images/green-16x16.png?img33 51w">+Pass <img loading="lazy" sizes="auto" style="width: calc(5px + 5px)" data-ref="ref1" srcset="/images/green-1x1.png?img33 50w, /images/green-16x16.png?img33 51w">Fail <picture><source srcset="/images/green-1x1.png?picture33 50w, /images/green-16x16.png?picture33 51w"><img loading="lazy" sizes="auto" style="width: calc(5px + 5px)" data-ref="ref1"></picture>Pass <img loading="lazy" sizes="auto" style="position: absolute; left: 50%; right: 49%" data-ref="ref2" srcset="/images/green-1x1.png?img34 50w, /images/green-16x16.png?img34 51w">Pass <picture><source srcset="/images/green-1x1.png?picture34 50w, /images/green-16x16.png?picture34 51w"><img loading="lazy" sizes="auto" style="position: absolute; left: 50%; right: 49%" data-ref="ref2"></picture>