From: Coleman Watts Date: Sun, 31 Aug 2014 10:33:35 +0000 (+0100) Subject: Fix edge-case handling in CRM.url - CLEAR YOUR CACHES FOLKS X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=f0c9c7dd3903a07413fd1c4e5c6d09111fb415e2;p=civicrm-core.git Fix edge-case handling in CRM.url - CLEAR YOUR CACHES FOLKS This fixes potential collisions between placeholder strings and the site's url, such as "init" and "placeholder". --- diff --git a/js/crm.ajax.js b/js/crm.ajax.js index 2dbcdb738f..0e8fb7fb5f 100644 --- a/js/crm.ajax.js +++ b/js/crm.ajax.js @@ -5,37 +5,33 @@ */ (function($, CRM, undefined) { /** - * @param string p - url - * @param string|object params + * @param string path + * @param string|object query * @param string mode - optionally specify "front" or "back" */ var tplURL; - CRM.url = function (p, params, mode) { - if (p == "init") { - return tplURL = params; + CRM.url = function (path, query, mode) { + if (typeof path === 'object') { + return tplURL = path; } if (!tplURL) { console && console.log && console.log('Warning: CRM.url called before initialization'); } if (!mode) { - mode = CRM.config.isFrontend ? 'front' : 'back'; + mode = CRM.config && CRM.config.isFrontend ? 'front' : 'back'; } - params = params || ''; - var frag = p.split ('?'); - var url = tplURL[mode].replace("civicrm/example", frag[0]); + query = query || ''; + var frag = path.split ('?'); + var url = tplURL[mode].replace("*path*", frag[0]); - if (typeof(params) == 'string') { - url = url.replace("placeholder", params); + if (!query) { + url = url.replace(/[?&]\*query\*/, ''); } else { - url = url.replace("placeholder", $.param(params)); + url = url.replace("*query*", typeof query === 'string' ? query : $.param(query)); } if (frag[1]) { - url += (url.indexOf('?') === (url.length - 1) ? '' : '&') + frag[1]; - } - // remove trailing "?" - if (url.indexOf('?') === (url.length - 1)) { - url = url.slice(0, (url.length - 1)); + url += (url.indexOf('?') < 0 ? '?' : '&') + frag[1]; } return url; }; diff --git a/templates/CRM/common/localization.js.tpl b/templates/CRM/common/localization.js.tpl index e7f469c2a9..75f0dbe3a8 100644 --- a/templates/CRM/common/localization.js.tpl +++ b/templates/CRM/common/localization.js.tpl @@ -37,7 +37,7 @@ {rdelim} // Initialize CRM.url and CRM.formatMoney - CRM.url('init', {ldelim}back: '{crmURL p="civicrm/example" q="placeholder" h=0 fb=1}', front: '{crmURL p="civicrm/example" q="placeholder" h=0 fe=1}'{rdelim}); + CRM.url({ldelim}back: '{crmURL p="*path*" q="*query*" h=0 fb=1}', front: '{crmURL p="*path*" q="*query*" h=0 fe=1}'{rdelim}); CRM.formatMoney('init', {$moneyFormat}); // Localize select2