Skip to content

Truly independent web browser

LibWeb: Update the image data of an img inserted into a picture

Andreas Kling25d67d9905d7
diff --git a/Libraries/LibWeb/HTML/HTMLImageElement.cpp b/Libraries/LibWeb/HTML/HTMLImageElement.cpp
index bc7368f12d4..20c98bc820b 100644
--- a/Libraries/LibWeb/HTML/HTMLImageElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLImageElement.cpp
@@ -226,8 +226,10 @@ void HTMLImageElement::removed_from(IsSubtreeRoot is_subtree_root, DOM::Node* ol
// 1. If isSubtreeRoot is true and oldAncestor is a picture element, then count this as a relevant mutation for
// removedNode.
- if (is_subtree_root == IsSubtreeRoot::Yes && is<HTMLPictureElement>(old_ancestor))
+ if (is_subtree_root == IsSubtreeRoot::Yes && is<HTMLPictureElement>(old_ancestor)) {
+ m_is_child_of_picture = false;
update_the_image_data();
+ }
// 2. Set removedNode's last auto-sizes width to null.
m_last_auto_sizes_width = {};
@@ -267,10 +269,18 @@ void HTMLImageElement::set_dimension_attribute_source(DOM::Element const* source
}
}
+// https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element:html-element-insertion-steps
void HTMLImageElement::inserted()
{
Base::inserted();
update_dimension_attribute_source(*this);
+
+ // The img HTML element insertion steps, given insertedNode, are:
+ // 1. If insertedNode's parent is a picture element, then, count this as a relevant mutation for insertedNode.
+ // INTEROP: Other browsers and WPT only count this for an img that is inserted into its picture, not for one whose
+ // picture is inserted somewhere else.
+ if (is<HTMLPictureElement>(parent()) && !exchange(m_is_child_of_picture, true))
+ update_the_image_data();
}
bool HTMLImageElement::is_presentational_hint(Utf16FlyString const& name) const
diff --git a/Libraries/LibWeb/HTML/HTMLImageElement.h b/Libraries/LibWeb/HTML/HTMLImageElement.h
index e67850cb585..3b2559dda6b 100644
--- a/Libraries/LibWeb/HTML/HTMLImageElement.h
+++ b/Libraries/LibWeb/HTML/HTMLImageElement.h
@@ -206,6 +206,8 @@ private:
CSSPixelSize m_last_seen_viewport_size;
+ bool m_is_child_of_picture { false };
+
// https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element:dimension-attribute-source
// Each img element has a dimension attribute source, which must initially be the img element itself.
GC::Ptr<DOM::Element const> m_dimension_attribute_source;
diff --git a/Tests/LibWeb/Text/expected/HTML/picture-children-inserted-together.txt b/Tests/LibWeb/Text/expected/HTML/picture-children-inserted-together.txt
new file mode 100644
index 00000000000..66a6ee64793
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/picture-children-inserted-together.txt
@@ -0,0 +1,2 @@
+append: true
+replaceChildren: true
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/relevant-mutations.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/relevant-mutations.txt
index d8ecead7ca0..023b034dbb2 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/relevant-mutations.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/relevant-mutations.txt
@@ -2,8 +2,8 @@ Harness status: OK
Found 113 tests
-108 Pass
-5 Fail
+109 Pass
+4 Fail
Pass src set
Pass src changed
Pass src removed
@@ -60,7 +60,7 @@ Pass referrerpolicy no-referrer to absent, src already set
Pass referrerpolicy no-referrer to empty, src already set
Pass referrerpolicy no-referrer to no-referrer-when-downgrade, src already set
Pass referrerpolicy no-referrer to invalid, src already set
-Fail inserted into picture
+Pass inserted into picture
Pass removed from picture
Pass parent is picture, previous source inserted
Pass parent is picture, previous source removed
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/update-the-source-set.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/update-the-source-set.txt
index dbbbe310dcd..f1f7f6126c1 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/update-the-source-set.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/update-the-source-set.txt
@@ -2,8 +2,8 @@ Harness status: OK
Found 89 tests
-87 Pass
-2 Fail
+88 Pass
+1 Fail
Pass <img data-expect="">
Pass <img src="" data-expect="">
Pass <img src="data:,a" data-expect="data:,a">
@@ -92,4 +92,4 @@ Pass <picture><img data-expect=""><source srcset="data:,b"></picture>
Pass <picture><span><source srcset="data:,b"><img data-expect=""></span></picture>
Pass <picture><span><source srcset="data:,b"><img src="data:,a" data-expect="data:,a"></span></picture>
Pass <picture><source srcset="data:,b"><span><img src="data:,a" data-expect="data:,a"></span></picture>
-Fail <picture><source srcset="data:,b"><img data-expect="data:,b"></picture>
\ No newline at end of file
+Pass <picture><source srcset="data:,b"><img data-expect="data:,b"></picture>
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/HTML/picture-children-inserted-together.html b/Tests/LibWeb/Text/input/HTML/picture-children-inserted-together.html
new file mode 100644
index 00000000000..f4e11195eea
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/picture-children-inserted-together.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<body>
+<script src="../include.js"></script>
+<script>
+ const gif = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==";
+
+ function loadInto(picture, insert) {
+ return new Promise(resolve => {
+ const source = document.createElement("source");
+ source.srcset = gif;
+ const image = document.createElement("img");
+ image.onload = () => resolve(image.currentSrc === gif);
+ insert(picture, source, image);
+ });
+ }
+
+ asyncTest(async done => {
+ const picture = document.createElement("picture");
+ document.body.append(picture);
+ println(`append: ${await loadInto(picture, (picture, source, image) => picture.append(source, image))}`);
+ println(`replaceChildren: ${await loadInto(picture, (picture, source, image) => picture.replaceChildren(source, image))}`);
+ done();
+ });
+</script>