@@ -39,7 +39,7 @@ var rules = [
39
39
[ '/' , 'pathname' ] , // Extract from the back.
40
40
[ '@' , 'auth' , 1 ] , // Extract from the front.
41
41
[ NaN , 'host' , undefined , 1 , 1 ] , // Set left over value.
42
- [ / : ( \d + ) $ / , 'port' , undefined , 1 ] , // RegExp the back.
42
+ [ / : ( \d * ) $ / , 'port' , undefined , 1 ] , // RegExp the back.
43
43
[ NaN , 'hostname' , undefined , 1 , 1 ] // Set left over.
44
44
] ;
45
45
@@ -524,6 +524,7 @@ function toString(stringify) {
524
524
525
525
var query
526
526
, url = this
527
+ , host = url . host
527
528
, protocol = url . protocol ;
528
529
529
530
if ( protocol && protocol . charAt ( protocol . length - 1 ) !== ':' ) protocol += ':' ;
@@ -542,7 +543,7 @@ function toString(stringify) {
542
543
} else if (
543
544
url . protocol !== 'file:' &&
544
545
isSpecial ( url . protocol ) &&
545
- ! url . host &&
546
+ ! host &&
546
547
url . pathname !== '/'
547
548
) {
548
549
//
@@ -552,7 +553,13 @@ function toString(stringify) {
552
553
result += '@' ;
553
554
}
554
555
555
- result += url . host + url . pathname ;
556
+ //
557
+ // Trailing colon is removed from `url.host` when it is parsed. If it still
558
+ // ends with a colon, then add back the trailing colon that was removed. This
559
+ // prevents an invalid URL from being transformed into a valid one.
560
+ //
561
+ if ( host [ host . length - 1 ] === ':' ) host += ':' ;
562
+ result += host + url . pathname ;
556
563
557
564
query = 'object' === typeof url . query ? stringify ( url . query ) : url . query ;
558
565
if ( query ) result += '?' !== query . charAt ( 0 ) ? '?' + query : query ;
0 commit comments