Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add https option to prependUrl util #47312

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/url/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ const actualURL = prependHTTP( 'wordpress.org' ); // http://wordpress.org
_Parameters_

- _url_ `string`: The URL to test.
- _https_ `boolean`: whether or not to use secure https protocol.

_Returns_

Expand Down
8 changes: 5 additions & 3 deletions packages/url/src/prepend-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@ const USABLE_HREF_REGEXP = /^(?:[a-z]+:|#|\?|\.|\/)/i;
/**
* Prepends "http://" to a url, if it looks like something that is meant to be a TLD.
*
* @param {string} url The URL to test.
* @param {string} url The URL to test.
*
* @param {boolean} https whether or not to use secure https protocol.
* @example
* ```js
* const actualURL = prependHTTP( 'wordpress.org' ); // http://wordpress.org
* ```
*
* @return {string} The updated URL.
*/
export function prependHTTP( url ) {
export function prependHTTP( url, https = false ) {
if ( ! url ) {
return url;
}

url = url.trim();
if ( ! USABLE_HREF_REGEXP.test( url ) && ! isEmail( url ) ) {
return 'http://' + url;
const protocol = https ? 'https://' : 'http://';
return `${ protocol }${ url }`;
}

return url;
Expand Down
10 changes: 10 additions & 0 deletions packages/url/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,16 @@ describe( 'prependHTTP', () => {

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

it( 'should use secure protocol when useHttps option is provided', () => {
const url = 'wordpress.org ';

expect(
prependHTTP( url, {
https: true,
} )
).toBe( 'https://wordpress.org' );
} );
} );

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