Skip to content

Commit

Permalink
fix srcset parsing (#8671)
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhaller committed Dec 20, 2022
1 parent da6cbb3 commit eed1e2c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
22 changes: 22 additions & 0 deletions flow-libs/srcset.js.flow
@@ -0,0 +1,22 @@
// @flow
declare module 'srcset' {
declare type SrcSetDefinition = {|
url: string,
width?: number,
density?: number,
|};

declare type Options = {|
strict?: boolean,
|};

declare export function parse(
srcset: string,
options?: Options,
): SrcSetDefinition[];

declare export function stringify(
srcSetDefinitions: SrcSetDefinition[],
options?: Options,
): string;
}
Expand Up @@ -4,6 +4,6 @@
<meta charset="utf-8">
</head>
<body>
<img src="100x100.png" srcset="200x200.png 250w, 300x300.png 500w">
<img src="100x100.png" srcset="200x200.png?lat=1,lng=2 250w, 300x300.png 500w">
</body>
</html>
</html>
3 changes: 2 additions & 1 deletion packages/transformers/html/package.json
Expand Up @@ -27,6 +27,7 @@
"posthtml": "^0.16.5",
"posthtml-parser": "^0.10.1",
"posthtml-render": "^3.0.0",
"semver": "^5.7.1"
"semver": "^5.7.1",
"srcset": "4"
}
}
24 changes: 6 additions & 18 deletions packages/transformers/html/src/dependencies.js
Expand Up @@ -3,6 +3,7 @@
import type {AST, MutableAsset} from '@parcel/types';
import type {PostHTMLNode} from 'posthtml';
import PostHTML from 'posthtml';
import {parse, stringify} from 'srcset';
// A list of all attributes that may produce a dependency
// Based on https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
const ATTRS = {
Expand Down Expand Up @@ -85,24 +86,11 @@ const OPTIONS = {
};

function collectSrcSetDependencies(asset, srcset, opts) {
let newSources = [];
for (const source of srcset.split(',')) {
let pair = source.trim().split(' ');
if (pair.length === 0) {
continue;
}

pair[0] = asset.addURLDependency(pair[0], opts);
newSources.push(pair.join(' '));
}

/**
* https://html.spec.whatwg.org/multipage/images.html#srcset-attribute
*
* If an image candidate string in srcset contains a width descriptor or a pixel density descriptor or ASCII whitespace, the following image candidate string must begin with whitespace.
* So we need to join each image candidate string with ", ".
*/
return newSources.join(', ');
let parsed = parse(srcset).map(({url, ...v}) => ({
url: asset.addURLDependency(url, opts),
...v,
}));
return stringify(parsed);
}

function getAttrDepHandler(attr) {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -12086,6 +12086,11 @@ sprintf-js@~1.0.2:
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=

srcset@4:
version "4.0.0"
resolved "https://registry.yarnpkg.com/srcset/-/srcset-4.0.0.tgz#336816b665b14cd013ba545b6fe62357f86e65f4"
integrity sha512-wvLeHgcVHKO8Sc/H/5lkGreJQVeYMm9rlmt8PuR1xE31rIuXhuzznUUqAt8MqLhB3MqJdFzlNAfpcWnxiFUcPw==

sshpk@^1.7.0:
version "1.16.1"
resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.16.1.tgz#fb661c0bef29b39db40769ee39fa70093d6f6877"
Expand Down

0 comments on commit eed1e2c

Please sign in to comment.