From ab2b2356f1c547b076607f760701482e6bd01b88 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Thu, 8 Oct 2009 16:48:50 +0000 Subject: [PATCH] "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 --- lib/OpenLayers/Layer/HTTPRequest.js | 20 +------------- lib/OpenLayers/Util.js | 24 ++++++++++++++++ tests/Util.html | 43 +++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 19 deletions(-) diff --git a/lib/OpenLayers/Layer/HTTPRequest.js b/lib/OpenLayers/Layer/HTTPRequest.js index 938b67af4c..6846c91924 100644 --- a/lib/OpenLayers/Layer/HTTPRequest.js +++ b/lib/OpenLayers/Layer/HTTPRequest.js @@ -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" diff --git a/lib/OpenLayers/Util.js b/lib/OpenLayers/Util.js index 96e7e2ab50..a2440c5cdc 100644 --- a/lib/OpenLayers/Util.js +++ b/lib/OpenLayers/Util.js @@ -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 ''. diff --git a/tests/Util.html b/tests/Util.html index 1dbdbfe4ef..08e36fa30c 100644 --- a/tests/Util.html +++ b/tests/Util.html @@ -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 );