LibWeb: Throw error when calling drawImage() with a broken image

This commit is contained in:
Tim Ledbetter 2025-10-22 00:40:32 +01:00 committed by Jelle Raaijmakers
parent 96b34ea744
commit c4e56cc845
Notes: github-actions[bot] 2025-10-22 08:46:24 +00:00
8 changed files with 109 additions and 1 deletions

View file

@ -23,6 +23,7 @@
#include <LibWeb/HTML/HTMLImageElement.h>
#include <LibWeb/HTML/ImageBitmap.h>
#include <LibWeb/HTML/ImageData.h>
#include <LibWeb/HTML/ImageRequest.h>
#include <LibWeb/HTML/Path2D.h>
#include <LibWeb/HTML/TextMetrics.h>
#include <LibWeb/HTML/TraversableNavigable.h>
@ -802,7 +803,9 @@ WebIDL::ExceptionOr<CanvasImageSourceUsability> check_usability_of_image(CanvasI
auto usability = TRY(image.visit(
// HTMLOrSVGImageElement
[](GC::Root<HTMLImageElement> const& image_element) -> WebIDL::ExceptionOr<Optional<CanvasImageSourceUsability>> {
// FIXME: If image's current request's state is broken, then throw an "InvalidStateError" DOMException.
// If image's current request's state is broken, then throw an "InvalidStateError" DOMException.
if (image_element->current_request().state() == HTML::ImageRequest::State::Broken)
return WebIDL::InvalidStateError::create(image_element->realm(), "Image element state is broken"_utf16);
// If image is not fully decodable, then return bad.
if (!image_element->immutable_bitmap())

View file

@ -0,0 +1,6 @@
Harness status: OK
Found 1 tests
1 Pass
Pass Canvas test: 2d.drawImage.nonexistent

View file

@ -0,0 +1,6 @@
Harness status: OK
Found 1 tests
1 Pass
Pass Canvas test: 2d.pattern.image.broken

View file

@ -0,0 +1,6 @@
Harness status: OK
Found 1 tests
1 Pass
Pass Canvas test: 2d.pattern.image.nonexistent

View file

@ -0,0 +1,29 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<meta charset="UTF-8">
<title>Canvas test: 2d.drawImage.nonexistent</title>
<script src="../../../../resources/testharness.js"></script>
<script src="../../../../resources/testharnessreport.js"></script>
<script src="../../../../html/canvas/resources/canvas-tests.js"></script>
<link rel="stylesheet" href="../../../../html/canvas/resources/canvas-tests.css">
<body class="show_output">
<h1>2d.drawImage.nonexistent</h1>
<p class="desc"></p>
<p class="output">Actual output:</p>
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
<ul id="d"></ul>
<script>
var t = async_test("");
_addTest(function(canvas, ctx) {
var img = document.getElementById('not-found-at-all.png');
assert_throws_dom("INVALID_STATE_ERR", function() { ctx.drawImage(img, 0, 0); });
});
</script>
<img src="../../../../images/not-found-at-all.png" id="not-found-at-all.png" class="resource">

View file

@ -0,0 +1,29 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<meta charset="UTF-8">
<title>Canvas test: 2d.pattern.image.broken</title>
<script src="../../../../resources/testharness.js"></script>
<script src="../../../../resources/testharnessreport.js"></script>
<script src="../../../../html/canvas/resources/canvas-tests.js"></script>
<link rel="stylesheet" href="../../../../html/canvas/resources/canvas-tests.css">
<body class="show_output">
<h1>2d.pattern.image.broken</h1>
<p class="desc"></p>
<p class="output">Actual output:</p>
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
<ul id="d"></ul>
<script>
var t = async_test("");
_addTest(function(canvas, ctx) {
var img = document.getElementById('broken.png');
assert_throws_dom("INVALID_STATE_ERR", function() { ctx.createPattern(img, 'repeat'); });
});
</script>
<img src="../../../../images/broken.png" id="broken.png" class="resource">

View file

@ -0,0 +1,29 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<meta charset="UTF-8">
<title>Canvas test: 2d.pattern.image.nonexistent</title>
<script src="../../../../resources/testharness.js"></script>
<script src="../../../../resources/testharnessreport.js"></script>
<script src="../../../../html/canvas/resources/canvas-tests.js"></script>
<link rel="stylesheet" href="../../../../html/canvas/resources/canvas-tests.css">
<body class="show_output">
<h1>2d.pattern.image.nonexistent</h1>
<p class="desc"></p>
<p class="output">Actual output:</p>
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
<ul id="d"></ul>
<script>
var t = async_test("");
_addTest(function(canvas, ctx) {
var img = document.getElementById('no-such-image-really.png');
assert_throws_dom("INVALID_STATE_ERR", function() { ctx.createPattern(img, 'repeat'); });
});
</script>
<img src="../../../../images/no-such-image-really.png" id="no-such-image-really.png" class="resource">

Binary file not shown.