@@ -39,7 +39,7 @@ var rules = [
3939 [ '/' , 'pathname' ] , // Extract from the back.
4040 [ '@' , 'auth' , 1 ] , // Extract from the front.
4141 [ 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.
4343 [ NaN , 'hostname' , undefined , 1 , 1 ] // Set left over.
4444] ;
4545
@@ -524,6 +524,7 @@ function toString(stringify) {
524524
525525 var query
526526 , url = this
527+ , host = url . host
527528 , protocol = url . protocol ;
528529
529530 if ( protocol && protocol . charAt ( protocol . length - 1 ) !== ':' ) protocol += ':' ;
@@ -542,7 +543,7 @@ function toString(stringify) {
542543 } else if (
543544 url . protocol !== 'file:' &&
544545 isSpecial ( url . protocol ) &&
545- ! url . host &&
546+ ! host &&
546547 url . pathname !== '/'
547548 ) {
548549 //
@@ -552,7 +553,13 @@ function toString(stringify) {
552553 result += '@' ;
553554 }
554555
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 ;
556563
557564 query = 'object' === typeof url . query ? stringify ( url . query ) : url . query ;
558565 if ( query ) result += '?' !== query . charAt ( 0 ) ? '?' + query : query ;
0 commit comments