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

Image size #2526

Merged
merged 3 commits into from
Apr 2, 2015
Merged
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
80 changes: 51 additions & 29 deletions lib/less-node/image-size.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,57 @@
var Dimension = require("../less/tree/dimension"),
module.exports = function(environment) {
var Dimension = require("../less/tree/dimension"),
Expression = require("../less/tree/expression"),
functionRegistry = require("./../less/functions/function-registry"),
path = require("path");
functionRegistry = require("./../less/functions/function-registry");

function imageSize(functionContext, filePathNode) {
var filePath = filePathNode.value;
var currentFileInfo = functionContext.currentFileInfo;
var currentDirectory = currentFileInfo.relativeUrls ?
function imageSize(functionContext, filePathNode) {
var filePath = filePathNode.value;
var currentFileInfo = functionContext.currentFileInfo;
var currentDirectory = currentFileInfo.relativeUrls ?
currentFileInfo.currentDirectory : currentFileInfo.entryPath;

var sizeOf = require('image-size');
filePath = path.join(currentDirectory, filePath);
return sizeOf(filePath);
}

var imageFunctions = {
"image-size": function(filePathNode) {
var size = imageSize(this, filePathNode);
return new Expression([
new Dimension(size.width, "px"),
new Dimension(size.height, "px")
]);
},
"image-width": function(filePathNode) {
var size = imageSize(this, filePathNode);
return new Dimension(size.width, "px");
},
"image-height": function(filePathNode) {
var size = imageSize(this, filePathNode);
return new Dimension(size.height, "px");
var fragmentStart = filePath.indexOf('#');
var fragment = '';
if (fragmentStart !== -1) {
fragment = filePath.slice(fragmentStart);
filePath = filePath.slice(0, fragmentStart);
}

var fileManager = environment.getFileManager(filePath, currentDirectory, functionContext.context, environment, true);

if (!fileManager) {
throw {
type: "File",
message: "Can not set up FileManager for " + filePathNode
};
}

var fileSync = fileManager.loadFileSync(filePath, currentDirectory, functionContext.context, environment);

if (fileSync.error) {
throw fileSync.error;
}

var sizeOf = require('image-size');
return sizeOf(fileSync.filename);
}
};

functionRegistry.addMultiple(imageFunctions);
var imageFunctions = {
"image-size": function(filePathNode) {
var size = imageSize(this, filePathNode);
return new Expression([
new Dimension(size.width, "px"),
new Dimension(size.height, "px")
]);
},
"image-width": function(filePathNode) {
var size = imageSize(this, filePathNode);
return new Dimension(size.width, "px");
},
"image-height": function(filePathNode) {
var size = imageSize(this, filePathNode);
return new Dimension(size.height, "px");
}
};

functionRegistry.addMultiple(imageFunctions);
};
2 changes: 1 addition & 1 deletion lib/less-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ less.writeError = function (ctx, options) {
};

// provide image-size functionality
require('./image-size');
require('./image-size')(less.environment);

module.exports = less;
3 changes: 3 additions & 0 deletions test/css/include-path/include-path.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ body {
data-uri {
property: url("data:image/svg+xml,%3Csvg%20height%3D%22100%22%20width%3D%22100%22%3E%0A%20%20%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2240%22%20stroke%3D%22black%22%20stroke-width%3D%221%22%20fill%3D%22blue%22%20%2F%3E%0A%3C%2Fsvg%3E");
}
image-size {
property: 100px 100px;
}
4 changes: 3 additions & 1 deletion test/less/include-path/include-path.less
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
data-uri {
property: data-uri('image.svg');
}

image-size {
property: image-size('image.svg');
}