Skip to content

Commit b39bf13

Browse files
committed
Start work on v2 rewrite.
1 parent 10e5d74 commit b39bf13

File tree

1 file changed

+65
-158
lines changed

1 file changed

+65
-158
lines changed

jquery.url.js

Lines changed: 65 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,181 +1,88 @@
1-
// JQuery URL Parser
2-
// Written by Mark Perkins, mark@allmarkedup.com
3-
// License: http://unlicense.org/ (i.e. do what you want with it!)
1+
;(function($) {
2+
3+
var tag2attr = {
4+
a : 'href',
5+
img : 'src',
6+
form : 'action',
7+
base : 'href',
8+
script : 'src',
9+
iframe : 'src',
10+
link : 'href'
11+
},
12+
13+
key = ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"], // keys available to query
414

5-
jQuery.url = function()
6-
{
7-
var segments = {};
15+
parser = {
16+
strict : /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, //less intuitive, more accurate to the specs
17+
loose : /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // more intuitive, fails on relative paths and deviates from specs
18+
},
819

9-
var parsed = {};
10-
11-
/**
12-
* Options object. Only the URI and strictMode values can be changed via the setters below.
13-
*/
14-
var options = {
15-
16-
url : window.location, // default URI is the page in which the script is running
17-
18-
strictMode: false, // 'loose' parsing by default
19-
20-
key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"], // keys available to query
21-
22-
q: {
23-
name: "queryKey",
24-
parser: /(?:^|&|;)([^&=;]*)=?([^&;]*)/g
25-
},
26-
27-
parser: {
28-
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, //less intuitive, more accurate to the specs
29-
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // more intuitive, fails on relative paths and deviates from specs
30-
}
20+
qs_parser = /(?:^|&|;)([^&=;]*)=?([^&;]*)/g; // supports both ampersand and semicolon-delimted query string key/value pairs
3121

32-
};
33-
34-
/**
35-
* Deals with the parsing of the URI according to the regex above.
36-
* Written by Steven Levithan - see credits at top.
37-
*/
38-
var parseUri = function()
22+
function parseUri( url, strictMode )
3923
{
40-
str = decodeURI( options.url );
24+
str = decodeURI( url );
4125

42-
var m = options.parser[ options.strictMode ? "strict" : "loose" ].exec( str );
26+
strictMode = strictMode || false;
27+
28+
var m = parser[ strictMode ? "strict" : "loose" ].exec( str );
4329
var uri = {};
4430
var i = 14;
4531

46-
while ( i-- ) {
47-
uri[ options.key[i] ] = m[i] || "";
32+
while ( i-- )
33+
{
34+
uri[ key[i] ] = m[i] || "";
4835
}
4936

50-
uri[ options.q.name ] = {};
51-
uri[ options.key[12] ].replace( options.q.parser, function ( $0, $1, $2 ) {
52-
if ($1) {
53-
uri[options.q.name][$1] = $2;
37+
uri['params'] = {};
38+
39+
uri['query'].replace( qs_parser, function ( $0, $1, $2 ){
40+
if ($1)
41+
{
42+
uri['params'][$1] = $2;
5443
}
5544
});
5645

5746
return uri;
5847
};
59-
60-
/**
61-
* Returns the value of the passed in key from the parsed URI.
62-
*
63-
* @param string key The key whose value is required
64-
*/
65-
var key = function( key )
66-
{
67-
if ( jQuery.isEmptyObject(parsed) )
68-
{
69-
setUp(); // if the URI has not been parsed yet then do this first...
70-
}
71-
if ( key == "base" )
72-
{
73-
if ( parsed.port !== null && parsed.port !== "" )
74-
{
75-
return parsed.protocol+"://"+parsed.host+":"+parsed.port+"/";
76-
}
77-
else
78-
{
79-
return parsed.protocol+"://"+parsed.host+"/";
80-
}
81-
}
8248

83-
return ( parsed[key] === "" ) ? null : parsed[key];
84-
};
85-
86-
/**
87-
* Returns the value of the required query string parameter.
88-
*
89-
* @param string item The parameter whose value is required
90-
*/
91-
var param = function( item )
49+
function getAttrName( elm )
9250
{
93-
if ( jQuery.isEmptyObject(parsed) )
94-
{
95-
setUp(); // if the URI has not been parsed yet then do this first...
96-
}
97-
if ( item === undefined )
98-
{
99-
return parsed.queryKey;
100-
}
101-
else
102-
{
103-
return ( parsed.queryKey[item] === null ) ? null : parsed.queryKey[item];
104-
}
105-
};
106-
107-
/**
108-
* 'Constructor' (not really!) function.
109-
* Called whenever the URI changes to kick off re-parsing of the URI and splitting it up into segments.
110-
*/
111-
var setUp = function()
112-
{
113-
parsed = parseUri();
114-
115-
getSegments();
116-
};
51+
var tg = elm.tagName;
52+
if ( tg !== undefined ) return tag2attr[tg.toLowerCase()];
53+
return tg;
54+
}
11755

118-
/**
119-
* Splits up the body of the URI into segments (i.e. sections delimited by '/')
120-
*/
121-
var getSegments = function()
56+
$.fn.url = function( strictMode )
12257
{
123-
var p = parsed.path;
124-
segments = []; // clear out segments array
125-
segments = parsed.path.length == 1 ? {} : ( p.charAt( p.length - 1 ) == "/" ? p.substring( 1, p.length - 1 ) : path = p.substring( 1 ) ).split("/");
58+
var url = '';
59+
60+
if ( this.length )
61+
{
62+
url = $(this).attr( getAttrName(this[0]) ) || '';
63+
}
64+
65+
return $.url({ url : url, strict : strictMode });
12666
};
12767

128-
return {
129-
130-
/**
131-
* Sets the parsing mode - either strict or loose. Set to loose by default.
132-
*
133-
* @param string mode The mode to set the parser to. Anything apart from a value of 'strict' will set it to loose!
134-
*/
135-
setMode : function( mode )
136-
{
137-
options.strictMode = mode == "strict" ? true : false;
138-
return this;
139-
},
140-
141-
/**
142-
* Sets URI to parse if you don't want to to parse the current page's URI.
143-
* Calling the function with no value for newUri resets it to the current page's URI.
144-
*
145-
* @param string newUri The URI to parse.
146-
*/
147-
setUrl : function( newUri )
148-
{
149-
options.url = newUri === undefined ? window.location : newUri;
150-
setUp();
151-
return this;
152-
},
153-
154-
/**
155-
* Returns the value of the specified URI segment. Segments are numbered from 1 to the number of segments.
156-
* For example the URI http://test.com/about/company/ segment(1) would return 'about'.
157-
*
158-
* If no integer is passed into the function it returns the number of segments in the URI.
159-
*
160-
* @param int pos The position of the segment to return. Can be empty.
161-
*/
162-
segment : function( pos )
163-
{
164-
if ( jQuery.isEmptyObject(parsed) )
165-
{
166-
setUp(); // if the URI has not been parsed yet then do this first...
167-
}
168-
if ( pos === undefined )
169-
{
170-
return segments.length;
171-
}
172-
return ( segments[pos] === "" || segments[pos] === undefined ) ? null : segments[pos];
173-
},
174-
175-
attr : key, // provides public access to private 'key' function - see above
176-
177-
param : param // provides public access to private 'param' function - see above
178-
68+
$.url = function( opts )
69+
{
70+
var url = '',
71+
strict = false;
72+
73+
if ( typeof opts === 'string' )
74+
{
75+
url = opts;
76+
}
77+
else
78+
{
79+
strict = opts.strict || strict;
80+
url = opts.url === undefined ? window.location.toString() : opts.url;
81+
}
82+
83+
console.log(url)
84+
85+
// TBC...
17986
};
18087

181-
}();
88+
})(jQuery);

0 commit comments

Comments
 (0)