diff --git a/lib/less-node/image-size.js b/lib/less-node/image-size.js index a4d0797fa..660c4db34 100644 --- a/lib/less-node/image-size.js +++ b/lib/less-node/image-size.js @@ -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); \ No newline at end of file + 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); +}; diff --git a/lib/less-node/index.js b/lib/less-node/index.js index c2401de83..9baaf8246 100644 --- a/lib/less-node/index.js +++ b/lib/less-node/index.js @@ -69,6 +69,6 @@ less.writeError = function (ctx, options) { }; // provide image-size functionality -require('./image-size'); +require('./image-size')(less.environment); module.exports = less; diff --git a/test/css/include-path/include-path.css b/test/css/include-path/include-path.css index 586cb6174..e3f58e66f 100644 --- a/test/css/include-path/include-path.css +++ b/test/css/include-path/include-path.css @@ -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; +} diff --git a/test/less/include-path/include-path.less b/test/less/include-path/include-path.less index bd1f1d197..3e9e76529 100644 --- a/test/less/include-path/include-path.less +++ b/test/less/include-path/include-path.less @@ -3,4 +3,6 @@ data-uri { property: data-uri('image.svg'); } - +image-size { + property: image-size('image.svg'); +}