From 796d37cc663a6f940b4e2cb9191628284f6f0bed Mon Sep 17 00:00:00 2001 From: jurcovicovam Date: Fri, 5 Sep 2014 16:47:37 +0200 Subject: [PATCH] Iterated string interpolation Solves issue #2094 . --- lib/less/tree/quoted.js | 23 +++++++++++++++-------- test/css/strings.css | 12 ++++++++++++ test/less/strings.less | 16 ++++++++++++++++ 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/lib/less/tree/quoted.js b/lib/less/tree/quoted.js index b8f63b9c..da7bc6b7 100644 --- a/lib/less/tree/quoted.js +++ b/lib/less/tree/quoted.js @@ -20,14 +20,21 @@ tree.Quoted.prototype = { }, toCSS: tree.toCSS, eval: function (env) { - var that = this; - var value = this.value.replace(/`([^`]+)`/g, function (_, exp) { - return new(tree.JavaScript)(exp, that.index, true).eval(env).value; - }).replace(/@\{([\w-]+)\}/g, function (_, name) { - var v = new(tree.Variable)('@' + name, that.index, that.currentFileInfo).eval(env, true); - return (v instanceof tree.Quoted) ? v.value : v.toCSS(); - }); - return new(tree.Quoted)(this.quote + value + this.quote, value, this.escaped, this.index, this.currentFileInfo); + var that = this; + var value = this.value.replace(/`([^`]+)`/g, function (_, exp) { + return new(tree.JavaScript)(exp, that.index, true).eval(env).value; + }); + var evaluatedValue = value; + var interpolationReplacementFnc = function (_, name) { + var v = new(tree.Variable)('@' + name, that.index, that.currentFileInfo).eval(env, true); + return (v instanceof tree.Quoted) ? v.value : v.toCSS(); + }; + do { + value = evaluatedValue; + evaluatedValue = value.replace(/@\{([\w-]+)\}/g, interpolationReplacementFnc); + } while (value!==evaluatedValue); + value = evaluatedValue; + return new(tree.Quoted)(this.quote + value + this.quote, value, this.escaped, this.index, this.currentFileInfo); }, compare: function (x) { if (!x.toCSS) { diff --git a/test/css/strings.css b/test/css/strings.css index cd6d6020..92430862 100644 --- a/test/css/strings.css +++ b/test/css/strings.css @@ -41,3 +41,15 @@ .watermark { family: Univers, Arial, Verdana, San-Serif; } +#iterated-interpolation .mixin { + width: 100px; + weird: 100px; + width-str: "100px"; + weird-str: "100px"; +} +#iterated-interpolation .interpolation-mixin { + width: 100px; + weird: 100px; + width-str: "100px"; + weird-str: "100px"; +} diff --git a/test/less/strings.less b/test/less/strings.less index c43e368d..43746053 100644 --- a/test/less/strings.less +++ b/test/less/strings.less @@ -55,3 +55,19 @@ @family: ~"Univers, @{test}"; family: @family; } +#iterated-interpolation { + @box-small: 10px; + @box-large: 100px; + + .mixin { // both ruleset and mixin + width: ~"@{box-@{suffix}}"; + weird: ~"@{box}-@{suffix}}"; + width-str: "@{box-@{suffix}}"; + weird-str: "@{box}-@{suffix}}"; + @box: ~"@{box"; + @suffix: large; + } + .interpolation-mixin { + .mixin(); //call the above as mixin + } +}