Skip to content

Truly independent web browser

LibWeb: Stop drawing the old image of an img without a source

Andreas Kling22f10ea88ede
diff --git a/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp b/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp
index fb1165b3d93..10027e48520 100644
--- a/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp
+++ b/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp
@@ -38,7 +38,9 @@ WebIDL::ExceptionOr<CanvasImageSourceUsability> check_usability_of_image(CanvasI
// If image is not fully decodable, then return bad.
// If image has a natural width or natural height (or both) equal to zero, then return bad.
- if (!image_provider_is_usable_for_canvas(*image_element))
+ // NB: An image whose current request is not completely available is not fully decodable, even while the
+ // request still holds image data.
+ if (image_element->current_request().state() != HTML::ImageRequest::State::CompletelyAvailable || !image_provider_is_usable_for_canvas(*image_element))
return { CanvasImageSourceUsability::Bad };
return Optional<CanvasImageSourceUsability> {};
},
diff --git a/Libraries/LibWeb/HTML/HTMLImageElement.cpp b/Libraries/LibWeb/HTML/HTMLImageElement.cpp
index 6535abf7e70..319774e13fd 100644
--- a/Libraries/LibWeb/HTML/HTMLImageElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLImageElement.cpp
@@ -886,6 +886,12 @@ void HTMLImageElement::update_the_image_data_impl(bool restart_animations, bool
// 6. Set the element's last selected source to selected source.
m_last_selected_source = selected_source;
+ // INTEROP: When the element has no source, its image stops being available right away, rather than only when the
+ // microtask below breaks the current request. So, for example, a canvas does not draw the old image in
+ // the meantime.
+ if (!selected_source.has_value() && !uses_srcset_or_picture() && m_current_request->state() == ImageRequest::State::CompletelyAvailable)
+ m_current_request->set_state(ImageRequest::State::Unavailable);
+
// 7. If selected source is not null, then:
if (selected_source.has_value()) {
// 1. Let urlString be the result of encoding-parsing-and-serializing a URL given selected source, relative to the element's node document.
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/drawing-images-to-the-canvas/2d.drawImage.incomplete.emptysrc.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/drawing-images-to-the-canvas/2d.drawImage.incomplete.emptysrc.txt
index 2428b1d6346..1fdfc87cceb 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/drawing-images-to-the-canvas/2d.drawImage.incomplete.emptysrc.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/drawing-images-to-the-canvas/2d.drawImage.incomplete.emptysrc.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Canvas test: 2d.drawImage.incomplete.emptysrc
\ No newline at end of file
+1 Pass
+Pass Canvas test: 2d.drawImage.incomplete.emptysrc
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/drawing-images-to-the-canvas/2d.drawImage.incomplete.removedsrc.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/drawing-images-to-the-canvas/2d.drawImage.incomplete.removedsrc.txt
index f2c8f025348..df56629ee45 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/drawing-images-to-the-canvas/2d.drawImage.incomplete.removedsrc.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/drawing-images-to-the-canvas/2d.drawImage.incomplete.removedsrc.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Canvas test: 2d.drawImage.incomplete.removedsrc
\ No newline at end of file
+1 Pass
+Pass Canvas test: 2d.drawImage.incomplete.removedsrc
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/fill-and-stroke-styles/2d.pattern.image.incomplete.emptysrc.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/fill-and-stroke-styles/2d.pattern.image.incomplete.emptysrc.txt
index a1b16490201..d64bb3ccc49 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/fill-and-stroke-styles/2d.pattern.image.incomplete.emptysrc.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/fill-and-stroke-styles/2d.pattern.image.incomplete.emptysrc.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Canvas test: 2d.pattern.image.incomplete.emptysrc
\ No newline at end of file
+1 Pass
+Pass Canvas test: 2d.pattern.image.incomplete.emptysrc
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/fill-and-stroke-styles/2d.pattern.image.incomplete.removedsrc.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/fill-and-stroke-styles/2d.pattern.image.incomplete.removedsrc.txt
index a44cf4c92c3..f5a5e72005c 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/fill-and-stroke-styles/2d.pattern.image.incomplete.removedsrc.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/fill-and-stroke-styles/2d.pattern.image.incomplete.removedsrc.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Canvas test: 2d.pattern.image.incomplete.removedsrc
\ No newline at end of file
+1 Pass
+Pass Canvas test: 2d.pattern.image.incomplete.removedsrc
\ No newline at end of file