From ff1e6584f5c7fae365e0cbb7873876c4199c9def Mon Sep 17 00:00:00 2001 From: Stefan Jonasson Date: Thu, 23 Jan 2014 12:23:51 +0100 Subject: [PATCH] Added a third option to disable the array parsing functionality --- README.md | 22 ++++++++++++++++++++-- purl.js | 35 +++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index b23fcaa..43d91e3 100644 --- a/README.md +++ b/README.md @@ -24,12 +24,12 @@ There are a few different ways to choose what URL to parse: ``` javascript /*---- jQuery version -----*/ var url = $.url(); // parse the current page URL -var url = $.url('http://allmarkedup.com'); // pass in a URI as a string and parse that +var url = $.url('http://allmarkedup.com'); // pass in a URI as a string and parse that var url = $('#myElement').url(); // extract the URL from the selected element and parse that - will work on any element with a `src`, `href` or `action` attribute. /*---- plain JS version -----*/ var url = purl(); // parse the current page URL -var url = purl('http://allmarkedup.com'); // pass in a URI as a string and parse that +var url = purl('http://allmarkedup.com'); // pass in a URI as a string and parse that ``` URL attributes @@ -122,6 +122,7 @@ Note that the `.param()` method will work on both ampersand-split and semicolon- *As of version 2.2 the param method now handles array-style query string params.* + URL segments ----------------------- @@ -177,6 +178,23 @@ var url = purl(true); // parse the current page URL in strict mode var url = purl('http://allmarkedup.com',true); // pass in a URI as a string and parse that in strict mode ``` +Array parameters parsing +-------------------- +As of version 2.2 the param method now handles array-style query string params. +If the arrays have non-sequential numeric keys it will not work well with jQuery.param() function. +If you do not need the array parsing functionality can be disabled with the third param. + +``` javascript +/*---- jQuery version -----*/ +var url = $.url(false, false); // parse the current page URL in normal mode with array parsing disabled +var url = $.url('http://allmarkedup.com', false, false); // pass in a URI as a string and parse that in normal mode with array parsing disabled +var url = $('#myElement').url(false, false); // extract the URL from the selected element and parse that in normal mode with array parsing disabled + +/*---- plain JS version -----*/ +var url = purl(false, false); // parse the current page URL in normal mode with array parsing disabled +var url = purl('http://allmarkedup.com', false, false); // pass in a URI as a string and parse that in normal mode with array parsing disabled +``` + A note on improperly encoded URLs --------------------------------- diff --git a/purl.js b/purl.js index b5799c6..b40ab6f 100644 --- a/purl.js +++ b/purl.js @@ -36,18 +36,18 @@ isint = /^[0-9]+$/; - function parseUri( url, strictMode ) { + function parseUri( url, strictMode, parseArrays ) { var str = decodeURI( url ), - res = parser[ strictMode || false ? 'strict' : 'loose' ].exec( str ), - uri = { attr : {}, param : {}, seg : {} }, - i = 14; + res = parser[ strictMode || false ? 'strict' : 'loose' ].exec( str ), + uri = { attr : {}, param : {}, seg : {} }, + i = 14; while ( i-- ) { uri.attr[ key[i] ] = res[i] || ''; } // build query and fragment parameters - uri.param['query'] = parseString(uri.attr['query']); + uri.param['query'] = parseString(uri.attr['query'], parseArrays); uri.param['fragment'] = parseString(uri.attr['fragment']); // split path and fragement into segments @@ -108,8 +108,8 @@ } } - function merge(parent, key, val) { - if (~key.indexOf(']')) { + function merge(parent, key, val, parseArrays) { + if (~key.indexOf(']') && parseArrays) { var parts = key.split('['); parse(parts, parent, 'base', val); } else { @@ -125,7 +125,7 @@ return parent; } - function parseString(str) { + function parseString(str, parseArrays) { return reduce(String(str).split(/&|;/), function(ret, pair) { try { pair = decodeURIComponent(pair.replace(/\+/g, ' ')); @@ -144,7 +144,7 @@ val = ''; } - return merge(ret, key, val); + return merge(ret, key, val, parseArrays); }, { base: {} }).base; } @@ -194,17 +194,24 @@ return key_array; } - function purl( url, strictMode ) { + function purl( url, strictMode, parseArrays ) { if ( arguments.length === 1 && url === true ) { strictMode = true; url = undefined; + } else if (arguments.length === 2 && (typeof(url) !== 'string')) { + strictMode = url; + parseArrays = strictMode; + url = undefined; } strictMode = strictMode || false; + + parseArrays = parseArrays == undefined ? true : parseArrays; + url = url || window.location.toString(); return { - data : parseUri(url, strictMode), + data : parseUri(url, strictMode, parseArrays), // get various attributes from the URI attr : function( attr ) { @@ -245,15 +252,15 @@ }; } - + purl.jQuery = function($){ if ($ != null) { - $.fn.url = function( strictMode ) { + $.fn.url = function( strictMode , parseArrays ) { var url = ''; if ( this.length ) { url = $(this).attr( getAttrName(this[0]) ) || ''; } - return purl( url, strictMode ); + return purl( url, strictMode, parseArrays ); }; $.url = purl;