From 784b87c43fef949c4699ba8dd01385da9805cb42 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Sat, 20 Jun 2015 13:46:36 +0200 Subject: [PATCH] add `moduleUrl` option --- lib/less/contexts.js | 22 +++++++++++++++++++++- lib/less/tree/url.js | 2 +- test/css/module-url/module-url.css | 19 +++++++++++++++++++ test/css/url-args/urls.css | 5 +++++ test/index.js | 1 + test/less/module-url/folder/file.less | 6 ++++++ test/less/module-url/module-url.less | 15 +++++++++++++++ test/less/url-args/urls.less | 6 ++++++ 8 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 test/css/module-url/module-url.css create mode 100644 test/less/module-url/folder/file.less create mode 100644 test/less/module-url/module-url.less diff --git a/lib/less/contexts.js b/lib/less/contexts.js index 1d0ee6d00..8af9c0c78 100644 --- a/lib/less/contexts.js +++ b/lib/less/contexts.js @@ -48,6 +48,7 @@ var evalCopyProperties = [ 'strictUnits', // whether units need to evaluate correctly 'sourceMap', // whether to output a source map 'importMultiple', // whether we are currently importing multiple copies + 'moduleUrl', // whether urls are in module syntax (./relative, module/item) 'urlArgs', // whether to add args into url tokens 'javascriptEnabled',// option - whether JavaScript is enabled. if undefined, defaults to true 'pluginManager', // Used as the plugin manager for the session @@ -79,7 +80,19 @@ contexts.Eval.prototype.isMathOn = function () { }; contexts.Eval.prototype.isPathRelative = function (path) { - return !/^(?:[a-z-]+:|\/|#)/i.test(path); + if (this.moduleUrl) { + return /^(?:\.\.?\/)/.test(path); + } else { + return !/^(?:[a-z-]+:|\/|#)/i.test(path); + } +}; + +contexts.Eval.prototype.combinePath = function (rootpath, path) { + if (this.moduleUrl) { + return "./" + rootpath + path; + } else { + return rootpath + path; + } }; contexts.Eval.prototype.normalizePath = function( path ) { @@ -92,10 +105,17 @@ contexts.Eval.prototype.normalizePath = function( path ) { segment = segments.pop(); switch( segment ) { case ".": + // Keep leading "./" seqment in module url mode + if (this.moduleUrl && path.length === 0) { + path.push( segment ); + } break; case "..": if ((path.length === 0) || (path[path.length - 1] === "..")) { path.push( segment ); + } else if (path[path.length - 1] === ".") { + path.pop(); + path.push( segment ); } else { path.pop(); } diff --git a/lib/less/tree/url.js b/lib/less/tree/url.js index 65347af43..82a989d4e 100644 --- a/lib/less/tree/url.js +++ b/lib/less/tree/url.js @@ -30,7 +30,7 @@ URL.prototype.eval = function (context) { if (!val.quote) { rootpath = rootpath.replace(/[\(\)'"\s]/g, function(match) { return "\\" + match; }); } - val.value = rootpath + val.value; + val.value = context.combinePath(rootpath, val.value); } val.value = context.normalizePath(val.value); diff --git a/test/css/module-url/module-url.css b/test/css/module-url/module-url.css new file mode 100644 index 000000000..5ad05dad5 --- /dev/null +++ b/test/css/module-url/module-url.css @@ -0,0 +1,19 @@ +#imported-file { + background-url: url(./folder/relative/path); + background-url: url(./relative/path); + background-url: url(../relative/path); + background-url: url(module/path); +} +#correct-normalize { + background-url: url(./relative/path); + background-url: url("./relative/path"); + background-url: url('./relative/path'); + background-url: url(../relative/path); + background-url: url("../relative/path"); + background-url: url('../relative/path'); + background-url: url(./path); + background-url: url(.); + background-url: url(module); + background-url: url(module/path); + background-url: url(module/relative/path); +} diff --git a/test/css/url-args/urls.css b/test/css/url-args/urls.css index 5d8eff539..196988844 100644 --- a/test/css/url-args/urls.css +++ b/test/css/url-args/urls.css @@ -54,3 +54,8 @@ background-image: url(data:image/x-png,f9difSSFIIGFIFJD1f982FSDKAA9==); background-image: url(' data:image/x-png,f9difSSFIIGFIFJD1f982FSDKAA9=='); } +#normalize-urls { + background-image: url(normal?424242); + background-image: url(relative?424242); + background-image: url("relative?424242"); +} diff --git a/test/index.js b/test/index.js index d8a7bb176..b81f2e619 100644 --- a/test/index.js +++ b/test/index.js @@ -46,6 +46,7 @@ lessTester.runTestSet({globalVars: true, banner: "/**\n * Test\n */\n"}, "glob lessTester.runTestSet({modifyVars: true}, "modifyVars/", null, null, null, function(name, type, baseFolder) { return path.join(baseFolder, name) + '.json'; }); lessTester.runTestSet({urlArgs: '424242'}, "url-args/"); +lessTester.runTestSet({moduleUrl: true, relativeUrls: true}, "module-url/"); lessTester.runTestSet({paths: ['test/data/', 'test/less/import/']}, "include-path/"); lessTester.runTestSet({paths: 'test/data/'}, "include-path-string/"); lessTester.runTestSet({plugin: 'test/plugins/postprocess/'}, "postProcessorPlugin/"); diff --git a/test/less/module-url/folder/file.less b/test/less/module-url/folder/file.less new file mode 100644 index 000000000..f8f956c52 --- /dev/null +++ b/test/less/module-url/folder/file.less @@ -0,0 +1,6 @@ +#imported-file { + background-url: url(./relative/path); + background-url: url(../relative/path); + background-url: url(../../relative/path); + background-url: url(module/path); +} diff --git a/test/less/module-url/module-url.less b/test/less/module-url/module-url.less new file mode 100644 index 000000000..09aec9fda --- /dev/null +++ b/test/less/module-url/module-url.less @@ -0,0 +1,15 @@ +@import "./folder/file.less"; + +#correct-normalize { + background-url: url(./relative/path); + background-url: url("./relative/path"); + background-url: url('./relative/path'); + background-url: url(../relative/path); + background-url: url("../relative/path"); + background-url: url('../relative/path'); + background-url: url(./relative/../path); + background-url: url(./relative/../path/..); + background-url: url(module); + background-url: url(module/path); + background-url: url(module/path/../relative/path); +} diff --git a/test/less/url-args/urls.less b/test/less/url-args/urls.less index 2f1bd8727..6b44ac622 100644 --- a/test/less/url-args/urls.less +++ b/test/less/url-args/urls.less @@ -61,3 +61,9 @@ background-image: url( data:image/x-png,f9difSSFIIGFIFJD1f982FSDKAA9==); background-image: url( ' data:image/x-png,f9difSSFIIGFIFJD1f982FSDKAA9=='); } + +#normalize-urls { + background-image: url(normal); + background-image: url(./relative); + background-image: url("./relative"); +}