From 7c0f132459f7e660606bc827cfe4576829054554 Mon Sep 17 00:00:00 2001 From: Jon Schlinkert Date: Thu, 25 Jul 2013 13:57:17 -0400 Subject: [PATCH 1/2] resolves #964 --- bower.json | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 bower.json diff --git a/bower.json b/bower.json new file mode 100644 index 00000000..e6f0686c --- /dev/null +++ b/bower.json @@ -0,0 +1,5 @@ +{ + "name": "less", + "version": "1.4.2", + "main": ["./lib/less/index"] +} From e0561e3d9558db419a553be9018d87cea0cf8738 Mon Sep 17 00:00:00 2001 From: rjgotten Date: Wed, 7 Aug 2013 10:44:12 +0200 Subject: [PATCH 2/2] Normalize URLs in generated CSS Adds a normalizePath method to tree.evalEnv.prototype with which to normalize paths, i.e. , remove /../ or /./ segments stuck in the middle. Unit tests have been updated to reflect these changes. --- lib/less/env.js | 27 +++++++++++++++++++++++++++ lib/less/tree/import.js | 15 ++++++++++----- lib/less/tree/url.js | 2 ++ test/css/static-urls/urls.css | 6 +++--- test/css/urls.css | 6 +++--- 5 files changed, 45 insertions(+), 11 deletions(-) diff --git a/lib/less/env.js b/lib/less/env.js index 91b0027e..86f72681 100644 --- a/lib/less/env.js +++ b/lib/less/env.js @@ -89,6 +89,33 @@ return !/^(?:[a-z-]+:|\/)/.test(path); }; + tree.evalEnv.prototype.normalizePath = function( path ) { + var + segments = path.split("/").reverse(), + segment; + + path = []; + while (segments.length !== 0 ) { + segment = segments.pop(); + switch( segment ) { + case ".": + break; + case "..": + if ((path.length === 0) || (path[path.length - 1] === "..")) { + path.push( segment ); + } else { + path.pop(); + } + break; + default: + path.push( segment ); + break; + } + } + + return path.join("/"); + }; + //todo - do the same for the toCSS env //tree.toCSSEnv = function (options) { //}; diff --git a/lib/less/tree/import.js b/lib/less/tree/import.js index 9a93baaf..50be1086 100644 --- a/lib/less/tree/import.js +++ b/lib/less/tree/import.js @@ -70,13 +70,18 @@ tree.Import.prototype = { evalPath: function (env) { var path = this.path.eval(env); var rootpath = this.currentFileInfo && this.currentFileInfo.rootpath; - if (rootpath && !(path instanceof tree.URL)) { - var pathValue = path.value; - // Add the base path if the import is relative - if (pathValue && env.isPathRelative(pathValue)) { - path.value = rootpath + pathValue; + + if (!(path instanceof tree.URL)) { + if (rootpath) { + var pathValue = path.value; + // Add the base path if the import is relative + if (pathValue && env.isPathRelative(pathValue)) { + path.value = rootpath + pathValue; + } } + path.value = env.normalizePath(path.value); } + return path; }, eval: function (env) { diff --git a/lib/less/tree/url.js b/lib/less/tree/url.js index 82ef8d7a..0be75acd 100644 --- a/lib/less/tree/url.js +++ b/lib/less/tree/url.js @@ -24,6 +24,8 @@ tree.URL.prototype = { val.value = rootpath + val.value; } + val.value = ctx.normalizePath(val.value); + return new(tree.URL)(val, null); } }; diff --git a/test/css/static-urls/urls.css b/test/css/static-urls/urls.css index b5a690e9..71b36115 100644 --- a/test/css/static-urls/urls.css +++ b/test/css/static-urls/urls.css @@ -1,4 +1,4 @@ -@import "folder (1)/../css/background.css"; +@import "css/background.css"; @import "folder (1)/import-test-d.css"; @font-face { @@ -31,11 +31,11 @@ #logo { width: 100px; height: 100px; - background: url('folder (1)/../assets/logo.png'); + background: url('assets/logo.png'); } @font-face { font-family: xecret; - src: url('folder (1)/../assets/xecret.ttf'); + src: url('assets/xecret.ttf'); } #secret { font-family: xecret, sans-serif; diff --git a/test/css/urls.css b/test/css/urls.css index f736962c..bbc3beac 100644 --- a/test/css/urls.css +++ b/test/css/urls.css @@ -1,4 +1,4 @@ -@import "import/../css/background.css"; +@import "css/background.css"; @import "import/import-test-d.css"; @@ -35,11 +35,11 @@ #logo { width: 100px; height: 100px; - background: url('import/imports/../assets/logo.png'); + background: url('import/assets/logo.png'); } @font-face { font-family: xecret; - src: url('import/imports/../assets/xecret.ttf'); + src: url('import/assets/xecret.ttf'); } #secret { font-family: xecret, sans-serif;