Skip to content

Commit

Permalink
Fix: Packages\Url: Trim leading and trailing whitespaces (#17320)
Browse files Browse the repository at this point in the history
* Fix: Packages\Url: Trim leading and trailing whitespaces

* Added unit tests for prependHTTP()
  • Loading branch information
donmhico authored and gziolo committed Sep 4, 2019
1 parent 0e55ef6 commit e420ac2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/url/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ export function removeQueryArgs( url, ...args ) {
* @return {string} The updated URL.
*/
export function prependHTTP( url ) {
url = url.trim();
if ( ! USABLE_HREF_REGEXP.test( url ) && ! EMAIL_REGEXP.test( url ) ) {
return 'http://' + url;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/url/src/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,18 @@ describe( 'prependHTTP', () => {

expect( prependHTTP( url ) ).toBe( url );
} );

it( 'should remove leading whitespace before prepending HTTP', () => {
const url = ' wordpress.org';

expect( prependHTTP( url ) ).toBe( 'http://wordpress.org' );
} );

it( 'should not have trailing whitespaces', () => {
const url = 'wordpress.org ';

expect( prependHTTP( url ) ).toBe( 'http://wordpress.org' );
} );
} );

describe( 'safeDecodeURI', () => {
Expand Down

0 comments on commit e420ac2

Please sign in to comment.