"Create an urlAppend function that appends strings to urls and handles ? and & appropriately". r=crschmidt (closes #2297)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@9730 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -222,25 +222,7 @@ OpenLayers.Layer.HTTPRequest = OpenLayers.Class(OpenLayers.Layer, {
|
||||
}
|
||||
paramsString = OpenLayers.Util.getParameterString(allParams);
|
||||
|
||||
// requestString always starts with url
|
||||
var requestString = url;
|
||||
|
||||
if (paramsString != "") {
|
||||
var lastServerChar = url.charAt(url.length - 1);
|
||||
if ((lastServerChar == "&") || (lastServerChar == "?")) {
|
||||
requestString += paramsString;
|
||||
} else {
|
||||
if (url.indexOf('?') == -1) {
|
||||
//serverPath has no ? -- add one
|
||||
requestString += '?' + paramsString;
|
||||
} else {
|
||||
//serverPath contains ?, so must already have
|
||||
// paramsString at the end
|
||||
requestString += '&' + paramsString;
|
||||
}
|
||||
}
|
||||
}
|
||||
return requestString;
|
||||
return OpenLayers.Util.urlAppend(url, paramsString);
|
||||
},
|
||||
|
||||
CLASS_NAME: "OpenLayers.Layer.HTTPRequest"
|
||||
|
||||
@@ -628,6 +628,30 @@ OpenLayers.Util.getParameterString = function(params) {
|
||||
return paramsArray.join("&");
|
||||
};
|
||||
|
||||
/**
|
||||
* Function: urlAppend
|
||||
* Appends a parameter string to a url. This function includes the logic for
|
||||
* using the appropriate character (none, & or ?) to append to the url before
|
||||
* appending the param string.
|
||||
*
|
||||
* Parameters:
|
||||
* url - {String} The url to append to
|
||||
* paramStr - {String} The param string to append
|
||||
*
|
||||
* Returns:
|
||||
* {String} The new url
|
||||
*/
|
||||
OpenLayers.Util.urlAppend = function(url, paramStr) {
|
||||
var newUrl = url;
|
||||
if(paramStr) {
|
||||
var parts = (url + " ").split(/[?&]/);
|
||||
newUrl += (parts.pop() === " " ?
|
||||
paramStr :
|
||||
parts.length ? "&" + paramStr : "?" + paramStr);
|
||||
}
|
||||
return newUrl;
|
||||
};
|
||||
|
||||
/**
|
||||
* Property: ImgPath
|
||||
* {String} Default is ''.
|
||||
|
||||
@@ -247,6 +247,49 @@
|
||||
var params = { foo: [null, undefined, 0, "", "bar"] }
|
||||
t.eq( OpenLayers.Util.getParameterString(params), "foo=,,0,,bar", "getParameterString works fine with null values in array.");
|
||||
}
|
||||
|
||||
function test_Util_urlAppend(t) {
|
||||
|
||||
var params = "foo=bar";
|
||||
|
||||
t.plan( 7 );
|
||||
|
||||
// without ?
|
||||
var url = "http://octo.metacarta.com/cgi-bin/mapserv";
|
||||
var str = OpenLayers.Util.urlAppend(url, params);
|
||||
t.eq(str, url + '?' + params, "urlAppend() works for url sans ?");
|
||||
|
||||
|
||||
// with ?
|
||||
url = "http://octo.metacarta.com/cgi-bin/mapserv?";
|
||||
str = OpenLayers.Util.urlAppend(url, params);
|
||||
t.eq(str, url + params, "urlAppend() works for url with ?");
|
||||
|
||||
// with ?param1=5
|
||||
url = "http://octo.metacarta.com/cgi-bin/mapserv?param1=5";
|
||||
str = OpenLayers.Util.urlAppend(url, params);
|
||||
t.eq(str, url + '&' + params, "urlAppend() works for url with ?param1=5");
|
||||
|
||||
// with ?param1=5&
|
||||
url = "http://octo.metacarta.com/cgi-bin/mapserv?param1=5&";
|
||||
str = OpenLayers.Util.urlAppend(url, params);
|
||||
t.eq(str, url + params, "urlAppend() works for url with ?param1=5&");
|
||||
|
||||
// with ?param1=5¶m2=6
|
||||
url = "http://octo.metacarta.com/cgi-bin/mapserv?param1=5¶m2=6";
|
||||
str = OpenLayers.Util.urlAppend(url, params);
|
||||
t.eq(str, url + "&" + params, "urlAppend() works for url with ?param1=5¶m2=6");
|
||||
|
||||
// with empty paramStr
|
||||
url = "http://octo.metacarta.com/cgi-bin/mapserv?param1=5"
|
||||
str = OpenLayers.Util.urlAppend(url, "");
|
||||
t.eq(str, url, "urlAppend() works with empty paramStr")
|
||||
|
||||
// with null paramStr
|
||||
url = "http://octo.metacarta.com/cgi-bin/mapserv?param1=5"
|
||||
str = OpenLayers.Util.urlAppend(url, null);
|
||||
t.eq(str, url, "urlAppend() works with null paramStr")
|
||||
}
|
||||
|
||||
function test_Util_createAlphaImageDiv(t) {
|
||||
t.plan( 19 );
|
||||
|
||||
Reference in New Issue
Block a user