Skip to content

Commit c4e56cc

Browse files
tcl3gmta
authored andcommitted
LibWeb: Throw error when calling drawImage() with a broken image
1 parent 96b34ea commit c4e56cc

File tree

8 files changed

+110
-1
lines changed

8 files changed

+110
-1
lines changed

Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <LibWeb/HTML/HTMLImageElement.h>
2424
#include <LibWeb/HTML/ImageBitmap.h>
2525
#include <LibWeb/HTML/ImageData.h>
26+
#include <LibWeb/HTML/ImageRequest.h>
2627
#include <LibWeb/HTML/Path2D.h>
2728
#include <LibWeb/HTML/TextMetrics.h>
2829
#include <LibWeb/HTML/TraversableNavigable.h>
@@ -802,7 +803,9 @@ WebIDL::ExceptionOr<CanvasImageSourceUsability> check_usability_of_image(CanvasI
802803
auto usability = TRY(image.visit(
803804
// HTMLOrSVGImageElement
804805
[](GC::Root<HTMLImageElement> const& image_element) -> WebIDL::ExceptionOr<Optional<CanvasImageSourceUsability>> {
805-
// FIXME: If image's current request's state is broken, then throw an "InvalidStateError" DOMException.
806+
// If image's current request's state is broken, then throw an "InvalidStateError" DOMException.
807+
if (image_element->current_request().state() == HTML::ImageRequest::State::Broken)
808+
return WebIDL::InvalidStateError::create(image_element->realm(), "Image element state is broken"_utf16);
806809

807810
// If image is not fully decodable, then return bad.
808811
if (!image_element->immutable_bitmap())
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Harness status: OK
2+
3+
Found 1 tests
4+
5+
1 Pass
6+
Pass Canvas test: 2d.drawImage.nonexistent
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Harness status: OK
2+
3+
Found 1 tests
4+
5+
1 Pass
6+
Pass Canvas test: 2d.pattern.image.broken
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Harness status: OK
2+
3+
Found 1 tests
4+
5+
1 Pass
6+
Pass Canvas test: 2d.pattern.image.nonexistent
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
3+
<meta charset="UTF-8">
4+
<title>Canvas test: 2d.drawImage.nonexistent</title>
5+
<script src="../../../../resources/testharness.js"></script>
6+
<script src="../../../../resources/testharnessreport.js"></script>
7+
<script src="../../../../html/canvas/resources/canvas-tests.js"></script>
8+
<link rel="stylesheet" href="../../../../html/canvas/resources/canvas-tests.css">
9+
<body class="show_output">
10+
11+
<h1>2d.drawImage.nonexistent</h1>
12+
<p class="desc"></p>
13+
14+
15+
<p class="output">Actual output:</p>
16+
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
17+
18+
<ul id="d"></ul>
19+
<script>
20+
var t = async_test("");
21+
_addTest(function(canvas, ctx) {
22+
23+
var img = document.getElementById('not-found-at-all.png');
24+
assert_throws_dom("INVALID_STATE_ERR", function() { ctx.drawImage(img, 0, 0); });
25+
26+
});
27+
</script>
28+
<img src="../../../../images/not-found-at-all.png" id="not-found-at-all.png" class="resource">
29+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
3+
<meta charset="UTF-8">
4+
<title>Canvas test: 2d.pattern.image.broken</title>
5+
<script src="../../../../resources/testharness.js"></script>
6+
<script src="../../../../resources/testharnessreport.js"></script>
7+
<script src="../../../../html/canvas/resources/canvas-tests.js"></script>
8+
<link rel="stylesheet" href="../../../../html/canvas/resources/canvas-tests.css">
9+
<body class="show_output">
10+
11+
<h1>2d.pattern.image.broken</h1>
12+
<p class="desc"></p>
13+
14+
15+
<p class="output">Actual output:</p>
16+
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
17+
18+
<ul id="d"></ul>
19+
<script>
20+
var t = async_test("");
21+
_addTest(function(canvas, ctx) {
22+
23+
var img = document.getElementById('broken.png');
24+
assert_throws_dom("INVALID_STATE_ERR", function() { ctx.createPattern(img, 'repeat'); });
25+
26+
});
27+
</script>
28+
<img src="../../../../images/broken.png" id="broken.png" class="resource">
29+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
3+
<meta charset="UTF-8">
4+
<title>Canvas test: 2d.pattern.image.nonexistent</title>
5+
<script src="../../../../resources/testharness.js"></script>
6+
<script src="../../../../resources/testharnessreport.js"></script>
7+
<script src="../../../../html/canvas/resources/canvas-tests.js"></script>
8+
<link rel="stylesheet" href="../../../../html/canvas/resources/canvas-tests.css">
9+
<body class="show_output">
10+
11+
<h1>2d.pattern.image.nonexistent</h1>
12+
<p class="desc"></p>
13+
14+
15+
<p class="output">Actual output:</p>
16+
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
17+
18+
<ul id="d"></ul>
19+
<script>
20+
var t = async_test("");
21+
_addTest(function(canvas, ctx) {
22+
23+
var img = document.getElementById('no-such-image-really.png');
24+
assert_throws_dom("INVALID_STATE_ERR", function() { ctx.createPattern(img, 'repeat'); });
25+
26+
});
27+
</script>
28+
<img src="../../../../images/no-such-image-really.png" id="no-such-image-really.png" class="resource">
29+
Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)