Skip to content

Commit

Permalink
Implement support for <img srcset="..."> (#681)
Browse files Browse the repository at this point in the history
* Implement support for <img srcset="...">

* Refactor HTMLAsset.collectDependencies to reduce code duplication
  • Loading branch information
lbguilherme authored and devongovett committed Jan 30, 2018
1 parent 9310641 commit 29ac70b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/assets/HTMLAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,41 @@ class HTMLAsset extends Asset {
return res;
}

processSingleDependency(path) {
let assetPath = this.addURLDependency(decodeURIComponent(path));
if (!isURL(assetPath)) {
assetPath = urlJoin(this.options.publicURL, assetPath);
}
return assetPath;
}

collectSrcSetDependencies(srcset) {
const newSources = [];
for (const source of srcset.split(',')) {
const pair = source.trim().split(' ');
if (pair.length === 0) continue;
pair[0] = this.processSingleDependency(pair[0]);
newSources.push(pair.join(' '));
}
return newSources.join(',');
}

collectDependencies() {
this.ast.walk(node => {
if (node.attrs) {
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)) {
let assetPath = this.addURLDependency(
decodeURIComponent(node.attrs[attr])
);
if (!isURL(assetPath)) {
assetPath = urlJoin(this.options.publicURL, assetPath);
}
node.attrs[attr] = assetPath;
node.attrs[attr] = this.processSingleDependency(node.attrs[attr]);
this.isAstDirty = true;
}
}
Expand Down
26 changes: 26 additions & 0 deletions test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,4 +438,30 @@ describe('html', function() {
]
});
});

it('Should detect srcset attribute', async function() {
let b = await bundle(__dirname + '/integration/html-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: []
}
]
});
});
});
Binary file added test/integration/html-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-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-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.
9 changes: 9 additions & 0 deletions test/integration/html-srcset/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<img src="100x100.png" srcset="200x200.png 250w, 300x300.png 500w">
</body>
</html>

0 comments on commit 29ac70b

Please sign in to comment.