Fix for "BaseTypes.String.Format does not support sub-elements", patch + tests

from rdewit, r=me, (Closes #1956)


git-svn-id: http://svn.openlayers.org/trunk/openlayers@8900 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
crschmidt
2009-02-27 00:44:20 +00:00
parent f04f203499
commit b625e9f12f
2 changed files with 61 additions and 17 deletions

View File

@@ -116,25 +116,52 @@ OpenLayers.String = {
if(!context) {
context = window;
}
var tokens = template.split("${");
var item, last, replacement;
for(var i=1, len=tokens.length; i<len; i++) {
item = tokens[i];
last = item.indexOf("}");
if(last > 0) {
replacement = context[item.substring(0, last)];
if(typeof replacement == "function") {
replacement = args ?
replacement.apply(null, args) :
replacement();
// Example matching:
// str = ${foo.bar}
// match = foo.bar
var replacer = function(str, match) {
var replacement;
// Loop through all subs. Example: ${a.b.c}
// 0 -> replacement = context[a];
// 1 -> replacement = context[a][b];
// 2 -> replacement = context[a][b][c];
var subs = match.split(/\.+/);
for (var i=0; i< subs.length; i++) {
if (i == 0) {
replacement = context;
}
tokens[i] = replacement + item.substring(++last);
} else {
tokens[i] = "${" + item;
replacement = replacement[subs[i]];
}
}
return tokens.join("");
if(typeof replacement == "function") {
replacement = args ?
replacement.apply(null, args) :
replacement();
}
// If replacement is undefined, return the string 'undefined'.
// This is a workaround for a bugs in browsers not properly
// dealing with non-participating groups in regular expressions:
// http://blog.stevenlevithan.com/archives/npcg-javascript
if (typeof replacement == 'undefined') {
return 'undefined';
} else {
return replacement;
}
};
return template.replace(OpenLayers.String.tokenRegEx, replacer);
},
/**
* Property: OpenLayers.String.tokenRegEx
* Used to find tokens in a string.
* Examples: ${a}, ${a.b.c}, ${a-b}, ${5}
*/
tokenRegEx: /\${([\w.]+?)}/g,
/**
* Property: OpenLayers.String.numberRegEx

View File

@@ -89,7 +89,7 @@
"", "${ ", "${", " ${", "${${", "${}", "${${}}", " ${ ${",
"}", "${${} }"
]
t.plan(6 + unchanged.length);
t.plan(7 + unchanged.length);
var format = OpenLayers.String.format;
@@ -140,6 +140,23 @@
"still works if arguments are not supplied"
);
// test contexts where attribute values can be objects
var context = {
a: {
b: {
c: 'd',
e: function() {
return 'f';
}
}
}
};
t.eq(
format("${a.b.c} ${a.b.e} ${a.b.q} ${a} ${a...b...c}", context),
"d f undefined [object Object] d",
"attribute values that are objects are supported"
);
}
function test_String_isNumeric(t) {