Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retina 2x <lasso-img> #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions taglib/helper-getImageInfo.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
var getLassoRenderContext = require('./getLassoRenderContext');
var lassoImage = require('lasso-image');

module.exports = function(out, path, callback) {
module.exports = function(out, path, retinaPath, callback) {
var asyncOut = out.beginAsync();

var lassoRenderContext = getLassoRenderContext(out);
var theLasso = lassoRenderContext.lasso;
getImageInfo(path, function (imageInfo) {
if (path === retinaPath) {
finalize(imageInfo);
} else {
getImageInfo(retinaPath, function (retinaImageInfo) {
finalize(imageInfo, retinaImageInfo);
});
}
});

function getImageInfo(path, cb) {
var lassoRenderContext = getLassoRenderContext(out);
var theLasso = lassoRenderContext.lasso;

lassoImage.getImageInfo(path, { lasso: theLasso }, function(err, imageInfo) {
if (err) {
return asyncOut.error(err);
lassoImage.getImageInfo(path, { lasso: theLasso }, function(err, imageInfo) {
if (err) {
return asyncOut.error(err);
}

cb(imageInfo);
});
}

function finalize(imageInfo, retinaImageInfo) {
if (retinaImageInfo) {
imageInfo.srcset = retinaImageInfo.url + ' 2x';
}

callback(asyncOut, imageInfo);
asyncOut.end();
});
};
}
};
13 changes: 11 additions & 2 deletions taglib/lasso-img-tag.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var getImageInfoHelperPath = require.resolve('./helper-getImageInfo');

module.exports = function codeGenerator(el, codegen) {

if (el.isFlagSet('lassoTransformed')) {
return el;
}
Expand All @@ -17,12 +16,21 @@ module.exports = function codeGenerator(el, codegen) {
var imageInfoVar = builder.identifier('imageInfo' + (nextVarId++));

var src = codegen.resolvePath(el.getAttributeValue('src'));
var retinaSrc;

el.setAttributeValue('src',
builder.memberExpression(
imageInfoVar,
builder.identifier('url')));

if (el.hasAttribute('srcset')) {
retinaSrc = codegen.resolvePath(el.getAttributeValue('srcset'));

el.setAttributeValue('srcset',
builder.memberExpression(
imageInfoVar,
builder.identifier('srcset')));
}

if (!el.hasAttribute('width')) {
el.setAttributeValue('width',
Expand All @@ -45,6 +53,7 @@ module.exports = function codeGenerator(el, codegen) {
return builder.functionCall(getImageInfoVar, [
builder.identifierOut(),
src,
retinaSrc || src,
builder.functionDeclaration(
null, // Callback name
[ // Callback params
Expand All @@ -55,4 +64,4 @@ module.exports = function codeGenerator(el, codegen) {
el
])
]);
};
};
1 change: 1 addition & 0 deletions test/autotests/taglib/simple/expected.marko
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<body>
<img src="/static/marko-logo-314d77d4.png" width="240" height="200">
<img src="/static/marko-logo-314d77d4.png" width="100" height="100">
<img src="/static/marko-logo-314d77d4.png" srcset="/static/marko-logo-2x-314d77d4.png 2x" width="240" height="200">
<script src="/static/taglib-simple-1097e0f6.js"></script>
</body>
</html>
Binary file added test/autotests/taglib/simple/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion test/autotests/taglib/simple/template.marko
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<body>
<lasso-img src="./marko-logo.png"/>
<lasso-img src="./marko-logo.png" width="100" height="100"/>
<lasso-img src="./marko-logo.png" srcset="./[email protected]"/>
<lasso-body/>
</body>
</html>
</html>