Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
fix(core/figures): don't add width/height to .svg images (speced#4422)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi authored Apr 5, 2023
1 parent 1a57f26 commit d268ecd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/core/figures.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,13 @@ function normalizeImages(doc) {
":not(picture)>img:not([width]):not([height]):not([srcset])"
)
.forEach(img => {
// Don't add width and height to SVGs
// https://github.com/w3c/respec/issues/3435
if (img.src.startsWith("data:image/svg+xml;")) return;
if (img.src.endsWith(".svg")) return;

if (img.naturalHeight === 0 || img.naturalWidth === 0) return;

img.height = img.naturalHeight;
img.width = img.naturalWidth;
});
Expand Down
14 changes: 14 additions & 0 deletions tests/spec/core/figures-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ describe("Core - Figures", () => {
<img id="image-with-height-only" height=100 src="${imgDataURL}">
<img id="image-with-width-only" width=200 src="${imgDataURL}">
<img id="image-with-srcset" srcset="${imgDataURL}">
<img id="image-svg-url" src="/img/foo.svg">
<img id="image-svg-data-uri" src="data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg"/>">
<picture>
<img id="image-inside-picture" src="${imgDataURL}">
</picture>
Expand Down Expand Up @@ -192,6 +194,18 @@ describe("Core - Figures", () => {
expect(image.hasAttribute("height")).toBeFalsy();
expect(image.hasAttribute("width")).toBeFalsy();
});

it("doesn't set height or width to SVG images", async () => {
const imageSvgUrl = doc.getElementById("image-svg-url");
expect(imageSvgUrl).toBeTruthy();
expect(imageSvgUrl.hasAttribute("height")).toBeFalsy();
expect(imageSvgUrl.hasAttribute("width")).toBeFalsy();

const imageSvgDataUri = doc.getElementById("image-svg-data-uri");
expect(imageSvgDataUri).toBeTruthy();
expect(imageSvgDataUri.hasAttribute("height")).toBeFalsy();
expect(imageSvgDataUri.hasAttribute("width")).toBeFalsy();
});
});
it("localizes list of figures", async () => {
const ops = {
Expand Down

0 comments on commit d268ecd

Please sign in to comment.