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:
+43
-16
@@ -116,26 +116,53 @@ OpenLayers.String = {
|
|||||||
if(!context) {
|
if(!context) {
|
||||||
context = window;
|
context = window;
|
||||||
}
|
}
|
||||||
var tokens = template.split("${");
|
|
||||||
var item, last, replacement;
|
// Example matching:
|
||||||
for(var i=1, len=tokens.length; i<len; i++) {
|
// str = ${foo.bar}
|
||||||
item = tokens[i];
|
// match = foo.bar
|
||||||
last = item.indexOf("}");
|
var replacer = function(str, match) {
|
||||||
if(last > 0) {
|
var replacement;
|
||||||
replacement = context[item.substring(0, last)];
|
|
||||||
if(typeof replacement == "function") {
|
// Loop through all subs. Example: ${a.b.c}
|
||||||
replacement = args ?
|
// 0 -> replacement = context[a];
|
||||||
replacement.apply(null, args) :
|
// 1 -> replacement = context[a][b];
|
||||||
replacement();
|
// 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 {
|
replacement = replacement[subs[i]];
|
||||||
tokens[i] = "${" + item;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
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
|
* Property: OpenLayers.String.numberRegEx
|
||||||
* Used to test strings as numbers.
|
* Used to test strings as numbers.
|
||||||
|
|||||||
+18
-1
@@ -89,7 +89,7 @@
|
|||||||
"", "${ ", "${", " ${", "${${", "${}", "${${}}", " ${ ${",
|
"", "${ ", "${", " ${", "${${", "${}", "${${}}", " ${ ${",
|
||||||
"}", "${${} }"
|
"}", "${${} }"
|
||||||
]
|
]
|
||||||
t.plan(6 + unchanged.length);
|
t.plan(7 + unchanged.length);
|
||||||
|
|
||||||
var format = OpenLayers.String.format;
|
var format = OpenLayers.String.format;
|
||||||
|
|
||||||
@@ -140,6 +140,23 @@
|
|||||||
"still works if arguments are not supplied"
|
"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) {
|
function test_String_isNumeric(t) {
|
||||||
|
|||||||
Reference in New Issue
Block a user