@@ -4,9 +4,8 @@ function URLPolyfill(url, baseURL) {
4
4
if ( typeof url != 'string' )
5
5
throw new TypeError ( 'URL must be a string' ) ;
6
6
var m = String ( url ) . replace ( / ^ \s + | \s + $ / g, "" ) . match ( / ^ ( [ ^ : \/ ? # ] + : ) ? (?: \/ \/ (?: ( [ ^ : @ \/ ? # ] * ) (?: : ( [ ^ : @ \/ ? # ] * ) ) ? @ ) ? ( ( [ ^ : \/ ? # ] * ) (?: : ( \d * ) ) ? ) ) ? ( [ ^ ? # ] * ) ( \? [ ^ # ] * ) ? ( # [ \s \S ] * ) ? / ) ;
7
- if ( ! m ) {
8
- throw new RangeError ( ) ;
9
- }
7
+ if ( ! m )
8
+ throw new RangeError ( 'Invalid URL format' ) ;
10
9
var protocol = m [ 1 ] || "" ;
11
10
var username = m [ 2 ] || "" ;
12
11
var password = m [ 3 ] || "" ;
@@ -18,44 +17,40 @@ function URLPolyfill(url, baseURL) {
18
17
var hash = m [ 9 ] || "" ;
19
18
if ( baseURL !== undefined ) {
20
19
var base = baseURL instanceof URLPolyfill ? baseURL : new URLPolyfill ( baseURL ) ;
21
- var flag = protocol === "" && host === "" && username === "" ;
22
- if ( flag && pathname === "" && search === "" ) {
20
+ var flag = ! protocol && ! host && ! username ;
21
+ if ( flag && ! pathname && ! search )
23
22
search = base . search ;
24
- }
25
- if ( flag && pathname . charAt ( 0 ) !== "/" ) {
26
- pathname = ( pathname !== "" ? ( ( ( base . host !== "" || base . username !== "" ) && base . pathname === "" ? "/" : "" ) + base . pathname . slice ( 0 , base . pathname . lastIndexOf ( "/" ) + 1 ) + pathname ) : base . pathname ) ;
27
- }
23
+ if ( flag && pathname [ 0 ] !== "/" )
24
+ pathname = ( pathname ? ( ( ( base . host || base . username ) && ! base . pathname ? "/" : "" ) + base . pathname . slice ( 0 , base . pathname . lastIndexOf ( "/" ) + 1 ) + pathname ) : base . pathname ) ;
28
25
// dot segments removal
29
26
var output = [ ] ;
30
27
pathname . replace ( / ^ ( \. \. ? ( \/ | $ ) ) + / , "" )
31
28
. replace ( / \/ ( \. ( \/ | $ ) ) + / g, "/" )
32
29
. replace ( / \/ \. \. $ / , "/../" )
33
30
. replace ( / \/ ? [ ^ \/ ] * / g, function ( p ) {
34
- if ( p === "/.." ) {
31
+ if ( p === "/.." )
35
32
output . pop ( ) ;
36
- } else {
33
+ else
37
34
output . push ( p ) ;
38
- }
39
35
} ) ;
40
- pathname = output . join ( "" ) . replace ( / ^ \/ / , pathname . charAt ( 0 ) === "/" ? "/" : "" ) ;
36
+ pathname = output . join ( "" ) . replace ( / ^ \/ / , pathname [ 0 ] === "/" ? "/" : "" ) ;
41
37
if ( flag ) {
42
38
port = base . port ;
43
39
hostname = base . hostname ;
44
40
host = base . host ;
45
41
password = base . password ;
46
42
username = base . username ;
47
43
}
48
- if ( protocol === "" ) {
44
+ if ( ! protocol )
49
45
protocol = base . protocol ;
50
- }
51
46
}
52
47
53
48
// convert windows file URLs to use /
54
49
if ( protocol == 'file:' )
55
50
pathname = pathname . replace ( / \\ / g, '/' ) ;
56
51
57
- this . origin = protocol + ( protocol !== "" || host !== "" ? "//" : "" ) + host ;
58
- this . href = protocol + ( protocol !== "" || host ! == "" ? "//" : "" ) + ( username !== "" ? username + ( password !== "" ? ":" + password : "" ) + "@" : "" ) + host + pathname + search + hash ;
52
+ this . origin = host ? protocol + ( protocol !== "" || host !== "" ? "//" : "" ) + host : "" ;
53
+ this . href = protocol + ( protocol && host || protocol == "file: " ? "//" : "" ) + ( username !== "" ? username + ( password !== "" ? ":" + password : "" ) + "@" : "" ) + host + pathname + search + hash ;
59
54
this . protocol = protocol ;
60
55
this . username = username ;
61
56
this . password = password ;
0 commit comments