Skip to content

Commit

Permalink
fix #987: srcset not working for source-element (#992)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnijuohz authored and devongovett committed Mar 14, 2018
1 parent f9aa325 commit c469751
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/assets/HTMLAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const ATTRS = {
'iframe',
'embed'
],
srcset: ['img', 'source'],
href: ['link', 'a'],
poster: ['video'],
'xlink:href': ['use'],
Expand Down Expand Up @@ -92,6 +93,13 @@ class HTMLAsset extends Asset {
return newSources.join(',');
}

getAttrDepHandler(attr) {
if (attr === 'srcset') {
return this.collectSrcSetDependencies;
}
return this.processSingleDependency;
}

collectDependencies() {
this.ast.walk(node => {
if (node.attrs) {
Expand All @@ -107,18 +115,14 @@ class HTMLAsset extends Asset {
}

for (let attr in node.attrs) {
if (node.tag === 'img' && attr === 'srcset') {
node.attrs[attr] = this.collectSrcSetDependencies(node.attrs[attr]);
this.isAstDirty = true;
continue;
}
let elements = ATTRS[attr];
// Check for virtual paths
if (node.tag === 'a' && node.attrs[attr].lastIndexOf('.') < 1) {
continue;
}
if (elements && elements.includes(node.tag)) {
node.attrs[attr] = this.processSingleDependency(node.attrs[attr]);
let depHandler = this.getAttrDepHandler(attr);
node.attrs[attr] = depHandler.call(this, node.attrs[attr]);
this.isAstDirty = true;
}
}
Expand Down
28 changes: 28 additions & 0 deletions test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,34 @@ describe('html', function() {
});
});

it('should detect srcset attribute of source element', async function() {
let b = await bundle(
__dirname + '/integration/html-source-srcset/index.html'
);

assertBundleTree(b, {
name: 'index.html',
assets: ['index.html'],
childBundles: [
{
type: 'png',
assets: ['100x100.png'],
childBundles: []
},
{
type: 'png',
assets: ['200x200.png'],
childBundles: []
},
{
type: 'png',
assets: ['300x300.png'],
childBundles: []
}
]
});
});

it('should support webmanifest', async function() {
let b = await bundle(__dirname + '/integration/webmanifest/index.html');

Expand Down
Binary file added test/integration/html-source-srcset/100x100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/integration/html-source-srcset/200x200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/integration/html-source-srcset/300x300.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions test/integration/html-source-srcset/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html>

<head>
<meta charset="utf-8">
</head>

<body>
<picture>
<source srcset="100x100.png 100w, 200x200.png 250w, 300x300.png 500w">
</picture>
</body>

</html>

0 comments on commit c469751

Please sign in to comment.