Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ filteredFixturesWithAttributes.forEach(({attrName, isVoid, tagName}) => it(`supp

["img", "source"].forEach(tagName => it(`supports <${tagName} src srcset="…">`, () =>
{
const input = `<${tagName} src="image1.png" srcset="image1a.png 2x, image1b.png 100w, image1c.png 100h">`;
const output = `<${tagName} src="http://domain.com/image1.png" srcset="http://domain.com/image1a.png 2x, http://domain.com/image1b.png 100w, http://domain.com/image1c.png 100h">`;
const input = `<${tagName} src="image1.png" srcset="image1a.png 2x, image1b.png 100w, image1c.png">`;
const output = `<${tagName} src="http://domain.com/image1.png" srcset="http://domain.com/image1a.png 2x, http://domain.com/image1b.png 100w, http://domain.com/image1c.png">`;

const options =
{
Expand Down
18 changes: 8 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"use strict";
const DEFAULT_OPTIONS = require("./defaultOptions");
const parseMetaRefresh = require("http-equiv-refresh");
const parseSrcset = require("parse-srcset");
const { parseSrcset, stringifySrcset } = require("srcset");

const CONTENT_ATTR = "content";
const PING_ATTR = "ping";
const SRCSET_ATTR = "srcset";

const EMPTY_STRING = "";
const EMPTY_TAG_GROUP = Object.freeze({});
const FUNCTION_TYPE = "function";
const PRETTY_DELIMITER = ", ";
Expand Down Expand Up @@ -148,17 +147,16 @@ const transformSrcset = (node, attrName, transformer) =>

if (values.length > 0)
{
const promises = values.map(({d, h, url, w}) => Promise.resolve( transformer(url, attrName, tag, node) )
.then(newUrl =>
const promises = values.map((srcset) => Promise.resolve( transformer(srcset.url, attrName, tag, node) )
.then(
/** @returns {import('srcset').SrcSetDefinition} */
newUrl =>
{
d = d !== undefined ? ` ${d}x` : EMPTY_STRING;
h = h !== undefined ? ` ${h}h` : EMPTY_STRING;
w = w !== undefined ? ` ${w}w` : EMPTY_STRING;

return `${newUrl}${w}${h}${d}`;

return { ...srcset, url: newUrl };
}));

return Promise.all(promises).then(newValues => attrs[attrName] = newValues.join(PRETTY_DELIMITER));
return Promise.all(promises).then(newValues => attrs[attrName] = stringifySrcset(newValues));
}
};

Expand Down
20 changes: 13 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
},
"dependencies": {
"http-equiv-refresh": "^2.0.1",
"parse-srcset": "^1.0.2"
"srcset": "^5.0.3"
},
"devDependencies": {
"vitest": "^4.1.5",
"@vitest/coverage-v8": "^4.1.5",
"html-tags": "^3.1.1",
"posthtml": "~0.16.6"
"posthtml": "~0.16.6",
"vitest": "^4.1.5"
},
"engines": {
"node": ">= 20"
Expand Down