@@ -14,6 +14,11 @@ export class URLSearchParams {
1414 _searchParams : Map < string , string [ ] > = new Map ( ) ;
1515
1616 constructor ( params ? : Record < string , string > | string | [ string , string ] [ ] ) {
17+
18+ if ( params === null ) {
19+ return ;
20+ }
21+
1722 // URLSearchParams("key1=value1&key2=value2");
1823 if ( typeof params === 'string' ) {
1924 params
@@ -23,7 +28,7 @@ export class URLSearchParams {
2328 if ( ! pair ) {
2429 return ;
2530 }
26- const [ key , value ] = pair . split ( '=' ) . map ( decodeURIComponent ) ;
31+ const [ key , value ] = pair . split ( '=' ) . map ( part => decodeURIComponent ( part . replace ( / \+ / g , ' ' ) ) ) ;
2732 this . append ( key , value ) ;
2833 } ) ;
2934 }
@@ -67,11 +72,11 @@ export class URLSearchParams {
6772 this. _searchParams . set ( name , [ value ] ) ;
6873 }
6974
70- keys ( name : string ) : Iterator < string > {
75+ keys ( ) : Iterator < string > {
7176 return this . _searchParams . keys ( ) ;
7277 }
7378
74- values ( name : string ) : Iterator < string > {
79+ values ( ) : Iterator < string > {
7580 function * generateValues ( params : Map < string , string [ ] > ) : Iterator < string > {
7681 for ( const valueArray of params . values ( ) ) {
7782 for ( const value of valueArray ) {
@@ -82,7 +87,7 @@ export class URLSearchParams {
8287 return generateValues ( this . _searchParams ) ;
8388 }
8489
85- entries ( name : string ) : Iterator < [ string , string ] > {
90+ entries ( ) : Iterator < [ string , string ] > {
8691 function * generateEntries (
8792 params : Map < string , string [ ] > ,
8893 ) : Iterator < [ string , string ] > {
@@ -126,14 +131,16 @@ export class URLSearchParams {
126131 }
127132
128133 toString ( ) : string {
129- return Array . from ( this . _searchParams . entries ( ) )
130- . map ( ( [ key , values ] ) =>
131- values
132- . map (
133- value => `${ encodeURIComponent ( key ) } =${ encodeURIComponent ( value ) } ` ,
134- )
135- . join ( '&' ) ,
136- )
137- . join ( '&' ) ;
138- }
134+ return Array . from ( this . _searchParams . entries ( ) )
135+ . map ( ( [ key , values ] ) =>
136+ values
137+ . map (
138+ value =>
139+ `${ encodeURIComponent ( key ) . replace ( / % 2 0 / g, '+' ) } =${ encodeURIComponent ( value )
140+ . replace ( / % 2 0 / g, '+' ) } ` // Convert only spaces to '+'
141+ )
142+ . join ( '&' ) ,
143+ )
144+ . join ( '&' ) ;
145+ }
139146}
0 commit comments