diff --git a/spec/KiwiSpec.js b/spec/KiwiSpec.js index 345952f..477c973 100644 --- a/spec/KiwiSpec.js +++ b/spec/KiwiSpec.js @@ -72,4 +72,18 @@ describe('Kiwi.compose - %{}', function() { var output = "The quick brown jumps over the lazy dog."; expect(Kiwi.compose(input, dic)).toEqual(output); }); + + it('should escape key value interpolation if ` is given before %',function() { + var input = "The quick brown `%{f} jumps over the lazy dog."; + var dic = {"d": "dog"}; + var output = "The quick brown %{f} jumps over the lazy dog."; + expect(Kiwi.compose(input, dic)).toEqual(output); + }); + + it('should escape key value interpolation for the first case but not for the 2nd.',function() { + var input = "The quick brown `%{f} %{f} jumps over the lazy dog."; + var dic = {"f": "fox"}; + var output = "The quick brown %{f} fox jumps over the lazy dog."; + expect(Kiwi.compose(input, dic)).toEqual(output); + }); }); \ No newline at end of file diff --git a/src/Kiwi.js b/src/Kiwi.js index 12924e0..9f1ac28 100644 --- a/src/Kiwi.js +++ b/src/Kiwi.js @@ -1,9 +1,9 @@ var Kiwi = (function ($) { var interpolate_general = function(text, array){ var stringToReturn = "" - ,length = text.length - ,index = 0 - ,array_length = array.length; + ,length = text.length + ,index = 0 + ,array_length = array.length; for(var i = 0; i < length; i++){ var currentChar = text[i]; @@ -48,15 +48,18 @@ var Kiwi = (function ($) { var interpolate_key_value = function(text, json){ var stringToReturn = "" - ,length = text.length - ,index = 0; + ,length = text.length + ,index = 0; for(var i = 0; i < length; i++){ var currentChar = text[i]; var nextChar = null; if(i + 1 < length) nextChar = text[i+1]; - if(currentChar === '%' && nextChar === '{'){ + if (currentChar === '`' && nextChar === '%') { + stringToReturn += '%'; + i += 1; + }else if(currentChar === '%' && nextChar === '{'){ var result = get_key_length(text, i); var key = result[0]; var key_length = result[1];