Skip to content

Commit eed1e2c

Browse files
fix srcset parsing (#8671)
1 parent da6cbb3 commit eed1e2c

File tree

5 files changed

+37
-21
lines changed

5 files changed

+37
-21
lines changed

flow-libs/srcset.js.flow

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @flow
2+
declare module 'srcset' {
3+
declare type SrcSetDefinition = {|
4+
url: string,
5+
width?: number,
6+
density?: number,
7+
|};
8+
9+
declare type Options = {|
10+
strict?: boolean,
11+
|};
12+
13+
declare export function parse(
14+
srcset: string,
15+
options?: Options,
16+
): SrcSetDefinition[];
17+
18+
declare export function stringify(
19+
srcSetDefinitions: SrcSetDefinition[],
20+
options?: Options,
21+
): string;
22+
}

packages/core/integration-tests/test/integration/html-srcset/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
<meta charset="utf-8">
55
</head>
66
<body>
7-
<img src="100x100.png" srcset="200x200.png 250w, 300x300.png 500w">
7+
<img src="100x100.png" srcset="200x200.png?lat=1,lng=2 250w, 300x300.png 500w">
88
</body>
9-
</html>
9+
</html>

packages/transformers/html/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"posthtml": "^0.16.5",
2828
"posthtml-parser": "^0.10.1",
2929
"posthtml-render": "^3.0.0",
30-
"semver": "^5.7.1"
30+
"semver": "^5.7.1",
31+
"srcset": "4"
3132
}
3233
}

packages/transformers/html/src/dependencies.js

+6-18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import type {AST, MutableAsset} from '@parcel/types';
44
import type {PostHTMLNode} from 'posthtml';
55
import PostHTML from 'posthtml';
6+
import {parse, stringify} from 'srcset';
67
// A list of all attributes that may produce a dependency
78
// Based on https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
89
const ATTRS = {
@@ -85,24 +86,11 @@ const OPTIONS = {
8586
};
8687

8788
function collectSrcSetDependencies(asset, srcset, opts) {
88-
let newSources = [];
89-
for (const source of srcset.split(',')) {
90-
let pair = source.trim().split(' ');
91-
if (pair.length === 0) {
92-
continue;
93-
}
94-
95-
pair[0] = asset.addURLDependency(pair[0], opts);
96-
newSources.push(pair.join(' '));
97-
}
98-
99-
/**
100-
* https://html.spec.whatwg.org/multipage/images.html#srcset-attribute
101-
*
102-
* 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.
103-
* So we need to join each image candidate string with ", ".
104-
*/
105-
return newSources.join(', ');
89+
let parsed = parse(srcset).map(({url, ...v}) => ({
90+
url: asset.addURLDependency(url, opts),
91+
...v,
92+
}));
93+
return stringify(parsed);
10694
}
10795

10896
function getAttrDepHandler(attr) {

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -12086,6 +12086,11 @@ sprintf-js@~1.0.2:
1208612086
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
1208712087
integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=
1208812088

12089+
srcset@4:
12090+
version "4.0.0"
12091+
resolved "https://registry.yarnpkg.com/srcset/-/srcset-4.0.0.tgz#336816b665b14cd013ba545b6fe62357f86e65f4"
12092+
integrity sha512-wvLeHgcVHKO8Sc/H/5lkGreJQVeYMm9rlmt8PuR1xE31rIuXhuzznUUqAt8MqLhB3MqJdFzlNAfpcWnxiFUcPw==
12093+
1208912094
sshpk@^1.7.0:
1209012095
version "1.16.1"
1209112096
resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.16.1.tgz#fb661c0bef29b39db40769ee39fa70093d6f6877"

0 commit comments

Comments
 (0)