Skip to content

More SVG as image with subresources test coverage #51850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!doctype html>
<html class=reftest-wait>
<title>Drawing SVG image with data: URL subresources (after window load event)</title>
<link rel="match" href="reference/image-with-nested-rects.html">
<style>
img {
width: 200px;
height: 200px;
background-color: red;
}
</style>
<img id="result" src="">
<script>
const canvas = document.createElement('canvas');
canvas.width = 200;
canvas.height = 200;
const context = canvas.getContext('2d');

context.fillStyle = "red";
context.fillRect(0, 0, canvas.width, canvas.height);

window.onload = () => {
const image = new Image();
image.src = "support/image-with-nested-data-url-images.svg";
image.onload = () => {
context.drawImage(image, 0, 0, canvas.width, canvas.height);
const resultImage = document.getElementById("result");
resultImage.src = canvas.toDataURL();
resultImage.onload = () => document.documentElement.className = "";
}
};
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!doctype html>
<html class=reftest-wait>
<title>Drawing SVG image with data: URL subresources (window load event)</title>
<link rel="match" href="reference/image-with-nested-rects.html">
<style>
img {
width: 200px;
height: 200px;
background-color: red;
}
</style>
<img id="result" src="">
<script>
const canvas = document.createElement('canvas');
canvas.width = 200;
canvas.height = 200;
const context = canvas.getContext('2d');

context.fillStyle = "red";
context.fillRect(0, 0, canvas.width, canvas.height);

const image = new Image();
image.src = "support/image-with-nested-data-url-images.svg";

window.onload = () => {
context.drawImage(image, 0, 0, canvas.width, canvas.height);
const resultImage = document.getElementById("result");
resultImage.src = canvas.toDataURL();
resultImage.onload = () => document.documentElement.className = "";
};
</script>
31 changes: 31 additions & 0 deletions svg/embedded/image-embedding-nested-data-url-from-canvas.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!doctype html>
<html class=reftest-wait>
<title>Drawing SVG image with data: URL subresources (image load event)</title>
<link rel="match" href="reference/image-with-nested-rects.html">
<style>
img {
width: 200px;
height: 200px;
background-color: red;
}
</style>
<img id="result" src="">
<script>
const canvas = document.createElement('canvas');
canvas.width = 200;
canvas.height = 200;
const context = canvas.getContext('2d');

context.fillStyle = "red";
context.fillRect(0, 0, canvas.width, canvas.height);

const image = new Image();
image.src = "support/image-with-nested-data-url-images.svg";

image.onload = () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, are you sure per spec this works? Shouldn't you image.decode().then(? What guarantees that the inner image:

  • Is decoded sync?
  • Is decoded on time?

Copy link
Member

@shallawa shallawa Apr 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the web developer should not worry about loading or decoding internal images. An image should be looked at as one entity especially if you do not have access to its internal structures (like the image in this test case). So for an image there should have only one loading time and only one decoding time.

For drawing an image into a canvas, I think this should force sync decoding. Unlike HTMLImageElement in the page, the image will be drawn into the canvas only once. If the image is not drawn because it has not been decoded yet, HTMLImageElement in the page will be repainted once it is ready. But canvas can't repaint the image.

So I think image.onload() should be enough to guarantee the image can be drawn to the canvas regardless of whether it contains internal images or not and regardless its decoding attribute is sync or async

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, Firefox probably needs to synchronously decode the nested images as part of painting the SVG, once the load event has fired (or delay the load event until they're decoded).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latter is what WebKit is doing.

context.drawImage(image, 0, 0, canvas.width, canvas.height);
const resultImage = document.getElementById("result");
resultImage.src = canvas.toDataURL();
resultImage.onload = () => document.documentElement.className = "";
};
</script>
9 changes: 9 additions & 0 deletions svg/embedded/reference/image-with-nested-rects.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<style>
img {
width: 200px;
height: 200px;
background-color: red;
}
</style>
<img src="../support/image-with-nested-rects.svg">
63 changes: 63 additions & 0 deletions svg/embedded/support/image-with-nested-data-url-images.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions svg/embedded/support/image-with-nested-rects.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.