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