Skip to content

Commit

Permalink
URL: Tolerate query string retrieval from path
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Mar 13, 2020
1 parent 42d2b36 commit 8dfe648
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/url/src/get-query-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
export function getQueryString( url ) {
let query;
try {
query = new URL( url ).search.substring( 1 );
query = new URL( url, 'http://example.com' ).search.substring( 1 );
} catch ( error ) {}

if ( query ) {
Expand Down
8 changes: 8 additions & 0 deletions packages/url/src/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@ describe( 'getQueryString', () => {
).toBe( 'foo=bar&foo=baz?test' );
} );

it( 'returns the query string of a path', () => {
expect( getQueryString( '/wp-json/wp/v2/posts?type=page' ) ).toBe(
'type=page'
);

expect( getQueryString( '/wp-json/wp/v2/posts' ) ).toBeUndefined();
} );

it( 'returns undefined when the provided does not contain a url query string', () => {
expect( getQueryString( '' ) ).toBeUndefined();
expect(
Expand Down

0 comments on commit 8dfe648

Please sign in to comment.