Skip to content

Commit a6db210

Browse files
committed
Fixes #234
1 parent 04e803a commit a6db210

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed

src/image-attrs-to-posthtml-node.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ function convertToPosthtmlNode(obj) {
5555
return node;
5656
}
5757

58+
function isValidSimpleWidthAttribute(width) {
59+
// `width` must be a single integer (not comma separated). Don’t use invalid HTML in width attribute. Use eleventy:widths if you want more complex support
60+
return (""+width) == (""+parseInt(width, 10));
61+
}
62+
5863
async function imageAttributesToPosthtmlNode(attributes, instanceOptions, globalPluginOptions) {
5964
if(!attributes.src) {
6065
throw new Error("Missing `src` attribute for `@11ty/eleventy-img`");
@@ -69,18 +74,15 @@ async function imageAttributesToPosthtmlNode(attributes, instanceOptions, global
6974
}
7075

7176
// overrides global widths
72-
if(attributes[ATTR.WIDTHS]) {
73-
if(typeof attributes[ATTR.WIDTHS] === "string") {
74-
instanceOptions.widths = attributes[ATTR.WIDTHS].split(",").map(entry => parseInt(entry, 10));
75-
delete attributes[ATTR.WIDTHS];
76-
}
77+
if(attributes.width && isValidSimpleWidthAttribute(attributes.width)) {
78+
// Support `width` but only single value
79+
instanceOptions.widths = [ parseInt(attributes.width, 10) ];
80+
} else if(attributes[ATTR.WIDTHS] && typeof attributes[ATTR.WIDTHS] === "string") {
81+
instanceOptions.widths = attributes[ATTR.WIDTHS].split(",").map(entry => parseInt(entry, 10));
7782
}
7883

79-
if(attributes[ATTR.FORMATS]) {
80-
if(typeof attributes[ATTR.FORMATS] === "string") {
81-
instanceOptions.formats = attributes[ATTR.FORMATS].split(",");
82-
delete attributes[ATTR.FORMATS];
83-
}
84+
if(attributes[ATTR.FORMATS] && typeof attributes[ATTR.FORMATS] === "string") {
85+
instanceOptions.formats = attributes[ATTR.FORMATS].split(",");
8486
}
8587

8688
let options = Object.assign({}, globalPluginOptions, instanceOptions);

test/transform-test.mjs

+51
Original file line numberDiff line numberDiff line change
@@ -422,4 +422,55 @@ test("Using the transform plugin, <img> to <picture>, keeps slot attribute #241"
422422
let results = await elev.toJSON();
423423
// TODO how to add independent class to <picture>
424424
t.is(results[0].content, `<picture><source type="image/webp" srcset="/virtual/KkPMmHd3hP-1280.webp 1280w"><img src="/virtual/KkPMmHd3hP-1280.jpeg" alt="My ugly mug" slot="image-1" width="1280" height="853"></picture>`);
425+
});
426+
427+
test("#234 Use existing `width` attribute for `widths` config", async t => {
428+
let elev = new Eleventy( "test", "test/_site", {
429+
config: eleventyConfig => {
430+
eleventyConfig.addTemplate("virtual.html", `<img src="./bio-2017.jpg" alt="My ugly mug" width="200">`);
431+
432+
eleventyConfig.addPlugin(eleventyImageTransformPlugin, {
433+
dryRun: true, // don’t write image files!
434+
});
435+
}
436+
});
437+
elev.disableLogger();
438+
439+
let results = await elev.toJSON();
440+
// TODO how to add independent class to <picture>
441+
t.is(results[0].content, `<picture><source type="image/webp" srcset="/virtual/KkPMmHd3hP-200.webp 200w"><img src="/virtual/KkPMmHd3hP-200.jpeg" alt="My ugly mug" width="200" height="133"></picture>`);
442+
});
443+
444+
test("#234 Use existing `width` attribute for `widths` config (huge width uses max intrinsic)", async t => {
445+
let elev = new Eleventy( "test", "test/_site", {
446+
config: eleventyConfig => {
447+
eleventyConfig.addTemplate("virtual.html", `<img src="./bio-2017.jpg" alt="My ugly mug" width="2000">`);
448+
449+
eleventyConfig.addPlugin(eleventyImageTransformPlugin, {
450+
dryRun: true, // don’t write image files!
451+
});
452+
}
453+
});
454+
elev.disableLogger();
455+
456+
let results = await elev.toJSON();
457+
// TODO how to add independent class to <picture>
458+
t.is(results[0].content, `<picture><source type="image/webp" srcset="/virtual/KkPMmHd3hP-1280.webp 1280w"><img src="/virtual/KkPMmHd3hP-1280.jpeg" alt="My ugly mug" width="1280" height="853"></picture>`);
459+
});
460+
461+
test("#234 Use existing `width` attribute for `widths` config (comma separated widths are ignored as invalid. Discourage invalid `width` HTML attribute, use eleventy:widths instead)", async t => {
462+
let elev = new Eleventy( "test", "test/_site", {
463+
config: eleventyConfig => {
464+
eleventyConfig.addTemplate("virtual.html", `<img src="./bio-2017.jpg" alt="My ugly mug" width="100,200">`);
465+
466+
eleventyConfig.addPlugin(eleventyImageTransformPlugin, {
467+
dryRun: true, // don’t write image files!
468+
});
469+
}
470+
});
471+
elev.disableLogger();
472+
473+
let results = await elev.toJSON();
474+
// TODO how to add independent class to <picture>
475+
t.is(results[0].content, `<picture><source type="image/webp" srcset="/virtual/KkPMmHd3hP-1280.webp 1280w"><img src="/virtual/KkPMmHd3hP-1280.jpeg" alt="My ugly mug" width="1280" height="853"></picture>`);
425476
});

0 commit comments

Comments
 (0)