Escaping key value interpolation #1

This commit is contained in:
Ziang Song 2014-07-17 10:22:35 -04:00
commit 8d65af5ff7
2 changed files with 23 additions and 6 deletions

View file

@ -72,4 +72,18 @@ describe('Kiwi.compose - %{}', function() {
var output = "The quick brown jumps over the lazy dog."; var output = "The quick brown jumps over the lazy dog.";
expect(Kiwi.compose(input, dic)).toEqual(output); 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);
});
}); });

View file

@ -1,9 +1,9 @@
var Kiwi = (function ($) { var Kiwi = (function ($) {
var interpolate_general = function(text, array){ var interpolate_general = function(text, array){
var stringToReturn = "" var stringToReturn = ""
,length = text.length ,length = text.length
,index = 0 ,index = 0
,array_length = array.length; ,array_length = array.length;
for(var i = 0; i < length; i++){ for(var i = 0; i < length; i++){
var currentChar = text[i]; var currentChar = text[i];
@ -48,15 +48,18 @@ var Kiwi = (function ($) {
var interpolate_key_value = function(text, json){ var interpolate_key_value = function(text, json){
var stringToReturn = "" var stringToReturn = ""
,length = text.length ,length = text.length
,index = 0; ,index = 0;
for(var i = 0; i < length; i++){ for(var i = 0; i < length; i++){
var currentChar = text[i]; var currentChar = text[i];
var nextChar = null; var nextChar = null;
if(i + 1 < length) nextChar = text[i+1]; 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 result = get_key_length(text, i);
var key = result[0]; var key = result[0];
var key_length = result[1]; var key_length = result[1];