Skip to content

Commit c469751

Browse files
gnijuohzdevongovett
authored andcommitted
fix #987: srcset not working for source-element (#992)
1 parent f9aa325 commit c469751

File tree

6 files changed

+52
-6
lines changed

6 files changed

+52
-6
lines changed

β€Žsrc/assets/HTMLAsset.jsβ€Ž

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const ATTRS = {
1919
'iframe',
2020
'embed'
2121
],
22+
srcset: ['img', 'source'],
2223
href: ['link', 'a'],
2324
poster: ['video'],
2425
'xlink:href': ['use'],
@@ -92,6 +93,13 @@ class HTMLAsset extends Asset {
9293
return newSources.join(',');
9394
}
9495

96+
getAttrDepHandler(attr) {
97+
if (attr === 'srcset') {
98+
return this.collectSrcSetDependencies;
99+
}
100+
return this.processSingleDependency;
101+
}
102+
95103
collectDependencies() {
96104
this.ast.walk(node => {
97105
if (node.attrs) {
@@ -107,18 +115,14 @@ class HTMLAsset extends Asset {
107115
}
108116

109117
for (let attr in node.attrs) {
110-
if (node.tag === 'img' && attr === 'srcset') {
111-
node.attrs[attr] = this.collectSrcSetDependencies(node.attrs[attr]);
112-
this.isAstDirty = true;
113-
continue;
114-
}
115118
let elements = ATTRS[attr];
116119
// Check for virtual paths
117120
if (node.tag === 'a' && node.attrs[attr].lastIndexOf('.') < 1) {
118121
continue;
119122
}
120123
if (elements && elements.includes(node.tag)) {
121-
node.attrs[attr] = this.processSingleDependency(node.attrs[attr]);
124+
let depHandler = this.getAttrDepHandler(attr);
125+
node.attrs[attr] = depHandler.call(this, node.attrs[attr]);
122126
this.isAstDirty = true;
123127
}
124128
}

β€Žtest/html.jsβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,34 @@ describe('html', function() {
470470
});
471471
});
472472

473+
it('should detect srcset attribute of source element', async function() {
474+
let b = await bundle(
475+
__dirname + '/integration/html-source-srcset/index.html'
476+
);
477+
478+
assertBundleTree(b, {
479+
name: 'index.html',
480+
assets: ['index.html'],
481+
childBundles: [
482+
{
483+
type: 'png',
484+
assets: ['100x100.png'],
485+
childBundles: []
486+
},
487+
{
488+
type: 'png',
489+
assets: ['200x200.png'],
490+
childBundles: []
491+
},
492+
{
493+
type: 'png',
494+
assets: ['300x300.png'],
495+
childBundles: []
496+
}
497+
]
498+
});
499+
});
500+
473501
it('should support webmanifest', async function() {
474502
let b = await bundle(__dirname + '/integration/webmanifest/index.html');
475503

255 Bytes
Loading
449 Bytes
Loading
771 Bytes
Loading
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
</head>
7+
8+
<body>
9+
<picture>
10+
<source srcset="100x100.png 100w, 200x200.png 250w, 300x300.png 500w">
11+
</picture>
12+
</body>
13+
14+
</html>

0 commit comments

Comments
Β (0)