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

Commit

Permalink
BREAKING CHANGE: stop adding width/height to images automatically (sp…
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi authored Apr 21, 2023
1 parent a950478 commit 4805f70
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 101 deletions.
20 changes: 0 additions & 20 deletions src/core/figures.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ const localizationStrings = {
const l10n = getIntlData(localizationStrings);

export function run() {
normalizeImages(document);

const tof = collectFigures();

// Create a Table of Figures if a section with id 'tof' exists.
Expand Down Expand Up @@ -120,24 +118,6 @@ function getTableOfFiguresListItem(figureId, caption) {
</li>`;
}

function normalizeImages(doc) {
doc
.querySelectorAll(
":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;
});
}

/**
* if it has a parent section, don't touch it
* if it has a class of appendix or introductory, don't touch it
Expand Down
81 changes: 0 additions & 81 deletions tests/spec/core/figures-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,87 +126,6 @@ describe("Core - Figures", () => {
expect(tofItems).toHaveSize(1);
});

describe("normalize images", () => {
const imgDataURL =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAADCAIAAADUVFKvAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gQKACEWdS72PwAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAAAMSURBVAjXY2AgDQAAADAAAceqhY4AAAAASUVORK5CYII=";
const ops = {
config: makeBasicConfig(),
body: `<section>
<img id="image-with-no-dimensions" src="${imgDataURL}">
<img id="image-with-dimensions" height=100 width=200 src="${imgDataURL}">
<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>
</section>`,
};

let doc;
beforeAll(async () => {
doc = await makeRSDoc(ops);
});

it("sets height and width for images with no height and width", async () => {
const image = doc.getElementById("image-with-no-dimensions");
expect(image).toBeTruthy();
expect(image.hasAttribute("height")).toBeTruthy();
expect(image.hasAttribute("width")).toBeTruthy();
expect(image.height).toBe(3);
expect(image.width).toBe(5);
});
it("doesn't change height and width for images with both height and width", async () => {
const image = doc.getElementById("image-with-dimensions");
expect(image).toBeTruthy();
expect(image.hasAttribute("height")).toBeTruthy();
expect(image.hasAttribute("width")).toBeTruthy();
expect(image.height).toBe(100);
expect(image.width).toBe(200);
});
it("doesn't change height and width for images with height only", async () => {
const image = doc.getElementById("image-with-height-only");
expect(image).toBeTruthy();
expect(image.hasAttribute("height")).toBeTruthy();
expect(image.hasAttribute("width")).toBeFalsy();
expect(image.height).toBe(100);
});
it("doesn't change height and width for images with width only", async () => {
const image = doc.getElementById("image-with-width-only");
expect(image).toBeTruthy();
expect(image.hasAttribute("height")).toBeFalsy();
expect(image.hasAttribute("width")).toBeTruthy();
expect(image.width).toBe(200);
});
it("doesn't change height and width for images with srcset", async () => {
const image = doc.getElementById("image-with-srcset");
expect(image).toBeTruthy();
expect(image.hasAttribute("srcset")).toBeTruthy();
expect(image.hasAttribute("height")).toBeFalsy();
expect(image.hasAttribute("width")).toBeFalsy();
expect(image.srcset).toBe(imgDataURL);
});
it("doesn't change height and width for images inside picture", async () => {
const image = doc.getElementById("image-inside-picture");
expect(image).toBeTruthy();
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 = {
config: makeBasicConfig(),
Expand Down

0 comments on commit 4805f70

Please sign in to comment.