Skip to content

Commit 6489e9e

Browse files
committed
Update regex splits to use regex matches for support in ie8.
1 parent 6b679fa commit 6489e9e

File tree

6 files changed

+74
-49
lines changed

6 files changed

+74
-49
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "js-url",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "A simple, lightweight url parser for JavaScript (~1.7 Kb minified, ~0.7Kb gzipped).",
55
"main": "url.js",
66
"repository": {

tests.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ else {
8686
});
8787
}
8888

89+
test('mailto', function() {
90+
deepEqual( window.url( 'email', 'mailto:rob@websanova.com' ), 'rob@websanova.com' );
91+
deepEqual( window.url( 'protocol', 'mailto:rob@websanova.com' ), 'mailto' );
92+
deepEqual( window.url( 'email', 'mailto:/rob@websanova.com' ), undefined );
93+
deepEqual( window.url( 'email', 'mailto://rob@websanova.com' ), undefined );
94+
deepEqual( window.url( 'protocol', 'mailto:/rob@websanova.com' ), 'http' );
95+
deepEqual( window.url( 'mailto:', 'mailto:/rob@websanova.com' ), undefined );
96+
});
97+
8998
test('hostname', function() {
9099
deepEqual( window.url( 'hostname', url ), 'www.domain.com' );
91100
deepEqual( window.url( 'hostname', urlIp ), '1.2.3.4' );
@@ -101,14 +110,7 @@ test('domain parts', function() {
101110

102111
test('auth', function() {
103112
deepEqual( window.url( 'auth', url ), 'rob:abcd1234' );
104-
});
105-
106-
test('user', function() {
107113
deepEqual( window.url( 'user', url ), 'rob' );
108-
deepEqual( window.url( 'email', 'mailto:rob@websanova.com' ), 'rob@websanova.com' );
109-
});
110-
111-
test('pass', function() {
112114
deepEqual( window.url( 'pass', url ), 'abcd1234' );
113115
});
114116

@@ -124,6 +126,7 @@ test('port', function() {
124126
deepEqual( window.url( 'port', 'http://domain.com:443' ), '443' );
125127
deepEqual( window.url( 'port', 'http://domain.com' ), '80' );
126128
deepEqual( window.url( 'port', 'https://domain.com' ), '443' );
129+
deepEqual( window.url( 'port', 'rob@domain.com' ), '80' );
127130
});
128131

129132
test('protocol', function() {
@@ -135,7 +138,7 @@ test('protocol', function() {
135138
deepEqual( window.url( 'protocol', 'domain.com:443' ), 'https' );
136139
deepEqual( window.url( 'protocol', 'https://domain.com:443' ), 'https' );
137140
deepEqual( window.url( 'protocol', 'https://domain.com:80' ), 'https' );
138-
deepEqual( window.url( 'protocol', 'mailto:rob@websanova.com' ), 'mailto' );
141+
deepEqual( window.url( 'protocol', 'rob@domain.com' ), 'http' );
139142
});
140143

141144
test('path', function() {
@@ -166,6 +169,9 @@ test('file', function() {
166169
deepEqual( window.url( 'file', url ), 'index.html' );
167170
deepEqual( window.url( 'filename', url ), 'index' );
168171
deepEqual( window.url( 'fileext', url ), 'html' );
172+
deepEqual( window.url( 'file', 'http://domain.com' ), undefined );
173+
deepEqual( window.url( 'filename', 'http://domain.com' ), undefined );
174+
deepEqual( window.url( 'fileext', 'http://domain.com' ), undefined );
169175
});
170176

171177
test('url parts', function() {
@@ -198,6 +204,8 @@ test('query string', function() {
198204
deepEqual( window.url( '?key', 'http://domain.com?key=value=va?key2=value' ), 'value=va?key2=value');
199205
deepEqual( window.url( '?poo', 'http://domain.com:400?poo=a:b' ), 'a:b' );
200206
deepEqual( window.url( '?poo', 'http://domain.com:400? & & &' ), undefined );
207+
deepEqual( window.url( '?test', 'http://domain.com?test=45#5' ), '45' );
208+
deepEqual( window.url( '?test', 'http://domain.com?test=45?5' ), '45?5' );
201209

202210
deepEqual( window.url( '?field[0]', 'http://domain.com?field[0]=zero&firled[1]=one' ), 'zero' );
203211
deepEqual( window.url( '?field', 'http://domain.com?field[0]=zero&field[1]=one&var=test' ), ['zero', 'one'] );
@@ -216,6 +224,7 @@ test('hash string', function() {
216224
deepEqual( window.url( '#poo', 'http://domain.com#poo' ), '' );
217225
deepEqual( window.url( '#poo', 'http://domain.com#' ), undefined );
218226
deepEqual( window.url( '#poo', 'http://domain.com' ), undefined );
227+
deepEqual( window.url( '#test', 'http://domain.com#test=45#5' ), '45#5' );
219228

220229
deepEqual( window.url( '#field[0]', 'http://domain.com#field[0]=zero&firled[1]=one' ), 'zero' );
221230
deepEqual( window.url( '#field', 'http://domain.com#field[0]=zero&field[1]=one&var=test' ), ['zero', 'one'] );

url-tld.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

url.jquery.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"websanova",
77
"url"
88
],
9-
"version": "2.0.1",
9+
"version": "2.0.2",
1010
"author": {
1111
"name": "Websanova",
1212
"email": "rob@websanova.com",

url.js

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,29 @@ window.url = (function() {
2828
arg2 = arg.substring(1);
2929

3030
for (var i in split) {
31-
field = split[i].split(/=(.*)/);
31+
field = split[i].match(/(.*?)=(.*)/);
3232

33-
if (field[0].replace(/\s/g, '') !== '') {
34-
field[1] = _d(field[1] || '');
33+
// TODO: regex should be able to handle this.
34+
if ( ! field) {
35+
field = [split[i], split[i], ''];
36+
}
37+
38+
if (field[1].replace(/\s/g, '') !== '') {
39+
field[2] = _d(field[2] || '');
3540

3641
// If we have a match just return it right away.
37-
if (arg2 === field[0]) { return field[1]; }
42+
if (arg2 === field[1]) { return field[2]; }
3843

3944
// Check for array pattern.
40-
tmp = field[0].match(/(.*)\[([0-9]+)\]/);
45+
tmp = field[1].match(/(.*)\[([0-9]+)\]/);
4146

4247
if (tmp) {
4348
params[tmp[1]] = params[tmp[1]] || [];
4449

45-
params[tmp[1]][tmp[2]] = field[1];
50+
params[tmp[1]][tmp[2]] = field[2];
4651
}
4752
else {
48-
params[field[0]] = field[1];
53+
params[field[1]] = field[2];
4954
}
5055
}
5156
}
@@ -66,69 +71,80 @@ window.url = (function() {
6671

6772
arg = arg.toString();
6873

69-
if (url.match(/^mailto:[^\/]/)) {
74+
if (tmp = url.match(/^mailto:([^\/].+)/)) {
7075
_l.protocol = 'mailto';
71-
_l.email = url.split(/mailto\:/)[1];
76+
_l.email = tmp[1];
7277
}
7378
else {
7479

75-
// Anchor.
76-
tmp = url.split(/#(.*)/);
77-
_l.hash = tmp[1] ? tmp[1] : undefined;
80+
// Hash.
81+
if (tmp = url.match(/(.*?)#(.*)/)) {
82+
_l.hash = tmp[2];
83+
url = tmp[1];
84+
}
7885

79-
// Return anchor parts.
86+
// Return hash parts.
8087
if (_l.hash && arg.match(/^#/)) { return _f(arg, _l.hash); }
81-
88+
8289
// Query
83-
tmp = tmp[0].split(/\?(.*)/);
84-
_l.query = tmp[1] ? tmp[1] : undefined;
90+
if (tmp = url.match(/(.*?)\?(.*)/)) {
91+
_l.query = tmp[2];
92+
url = tmp[1];
93+
}
8594

8695
// Return query parts.
8796
if (_l.query && arg.match(/^\?/)) { return _f(arg, _l.query); }
8897

8998
// Protocol.
90-
tmp = tmp[0].split(/\:?\/\//);
91-
_l.protocol = tmp[1] ? tmp[0].toLowerCase() : undefined;
99+
if (tmp = url.match(/(.*?)\:?\/\/(.*)/)) {
100+
_l.protocol = tmp[1].toLowerCase();
101+
url = tmp[2];
102+
}
92103

93104
// Path.
94-
tmp = (tmp[1] ? tmp[1] : tmp[0]).split(/(\/.*)/);
95-
_l.path = tmp[1] ? tmp[1] : '';
105+
if (tmp = url.match(/(.*?)(\/.*)/)) {
106+
_l.path = tmp[2];
107+
url = tmp[1];
108+
}
96109

97110
// Clean up path.
98-
_l.path = _l.path.replace(/^([^\/])/, '/$1').replace(/\/$/, '');
111+
_l.path = (_l.path || '').replace(/^([^\/])/, '/$1').replace(/\/$/, '');
99112

100113
// Return path parts.
101114
if (arg.match(/^[\-0-9]+$/)) { arg = arg.replace(/^([^\/])/, '/$1'); }
102115
if (arg.match(/^\//)) { return _i(arg, _l.path.substring(1)); }
103116

104117
// File.
105-
tmp2 = _i('/-1', _l.path.substring(1));
106-
tmp2 = tmp2.split(/\.(.*)/);
107-
108-
// Filename and fileext.
109-
if (tmp2[1]) {
110-
_l.file = tmp2[0] + '.' + tmp2[1];
111-
_l.filename = tmp2[0];
112-
_l.fileext = tmp2[1];
118+
tmp = _i('/-1', _l.path.substring(1));
119+
120+
if (tmp && (tmp = tmp.match(/(.*?)\.(.*)/))) {
121+
_l.file = tmp[0];
122+
_l.filename = tmp[1];
123+
_l.fileext = tmp[2];
113124
}
114125

115126
// Port.
116-
tmp = tmp[0].split(/\:([0-9]+)$/);
117-
_l.port = tmp[1] ? tmp[1] : undefined;
127+
if (tmp = url.match(/(.*)\:([0-9]+)$/)) {
128+
_l.port = tmp[2];
129+
url = tmp[1];
130+
}
118131

119132
// Auth.
120-
tmp = tmp[0].split(/@/);
121-
_l.auth = tmp[1] ? tmp[0] : undefined;
133+
if (tmp = url.match(/(.*?)@(.*)/)) {
134+
_l.auth = tmp[1];
135+
url = tmp[2];
136+
}
122137

123138
// User and pass.
124139
if (_l.auth) {
125-
tmp2 = _l.auth.split(/\:(.*)/);
126-
_l.user = tmp2[0];
127-
_l.pass = tmp2[1];
140+
tmp = _l.auth.match(/(.*)\:(.*)/);
141+
142+
_l.user = tmp ? tmp[1] : _l.auth;
143+
_l.pass = tmp ? tmp[2] : undefined;
128144
}
129145

130146
// Hostname.
131-
_l.hostname = (tmp[1] ? tmp[1] : tmp[0]).toLowerCase();
147+
_l.hostname = url.toLowerCase();
132148

133149
// Return hostname parts.
134150
if (arg.charAt(0) === '.') { return _i(arg, _l.hostname); }

0 commit comments

Comments
 (0)