Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a third option to disable the array parsing functionality #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
-----------------------

Expand Down Expand Up @@ -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
---------------------------------
Expand Down
35 changes: 21 additions & 14 deletions purl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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, ' '));
Expand All @@ -144,7 +144,7 @@
val = '';
}

return merge(ret, key, val);
return merge(ret, key, val, parseArrays);
}, { base: {} }).base;
}

Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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;
Expand Down