Skip to content

Commit

Permalink
Merge pull request #1884 from seven-phases-max/replace-func-improvement
Browse files Browse the repository at this point in the history
Minor `replace` and `%` funcs improvement.
  • Loading branch information
lukeapage committed Feb 18, 2014
2 parents 3945027 + 768a5cb commit 9dbb18c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
19 changes: 9 additions & 10 deletions lib/less/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,26 +218,25 @@ tree.functions = {
escape: function (str) {
return new(tree.Anonymous)(encodeURI(str.value).replace(/=/g, "%3D").replace(/:/g, "%3A").replace(/#/g, "%23").replace(/;/g, "%3B").replace(/\(/g, "%28").replace(/\)/g, "%29"));
},
replace: function (subject, pattern, replacement, flags) {
var str = subject.value;
replace: function (string, pattern, replacement, flags) {
var result = string.value;

str = str.replace(new RegExp(pattern.value, flags ? flags.value : ""), replacement.value);

return new(tree.Quoted)('"' + str + '"', str);
result = result.replace(new RegExp(pattern.value, flags ? flags.value : ''), replacement.value);
return new(tree.Quoted)(string.quote || '', result, string.escaped);
},
'%': function (quoted /* arg, arg, ...*/) {
'%': function (string /* arg, arg, ...*/) {
var args = Array.prototype.slice.call(arguments, 1),
str = quoted.value;
result = string.value;

for (var i = 0; i < args.length; i++) {
/*jshint loopfunc:true */
str = str.replace(/%[sda]/i, function(token) {
result = result.replace(/%[sda]/i, function(token) {
var value = token.match(/s/i) ? args[i].value : args[i].toCSS();
return token.match(/[A-Z]$/) ? encodeURIComponent(value) : value;
});
}
str = str.replace(/%%/g, '%');
return new(tree.Quoted)('"' + str + '"', str);
result = result.replace(/%%/g, '%');
return new(tree.Quoted)(string.quote || '', result, string.escaped);
},
unit: function (val, unit) {
if(!(val instanceof tree.Dimension)) {
Expand Down
5 changes: 5 additions & 0 deletions test/css/functions.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@
replace: "Hello, World!";
replace-captured: "This is a new string.";
replace-with-flags: "2 + 2 = 4";
replace-single-quoted: 'foo-2';
replace-escaped-string: bar-2;
replace-keyword: baz-2;
format: "rgb(32, 128, 64)";
format-string: "hello world";
format-multiple: "hello earth 2";
format-url-encode: "red is %23ff0000";
format-single-quoted: 'hello single world';
format-escaped-string: hello escaped world;
eformat: rgb(32, 128, 64);
unitless: 12;
unit: 14em;
Expand Down
7 changes: 6 additions & 1 deletion test/less/functions.less
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@
replace: replace("Hello, Mars.", "Mars\.", "World!");
replace-captured: replace("This is a string.", "(string)\.$", "new $1.");
replace-with-flags: replace("One + one = 4", "one", "2", "gi");
replace-single-quoted: replace('foo-1', "1", "2");
replace-escaped-string: replace(~"bar-1", "1", "2");
replace-keyword: replace(baz-1, "1", "2");
format: %("rgb(%d, %d, %d)", @r, 128, 64);
format-string: %("hello %s", "world");
format-multiple: %("hello %s %d", "earth", 2);
format-url-encode: %('red is %A', #ff0000);
format-url-encode: %("red is %A", #ff0000);
format-single-quoted: %('hello %s', "single world");
format-escaped-string: %(~"hello %s", "escaped world");
eformat: e(%("rgb(%d, %d, %d)", @r, 128, 64));

unitless: unit(12px);
Expand Down

0 comments on commit 9dbb18c

Please sign in to comment.