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

URL: Fix getQueryString incorrect handling of hash fragment #20738

Merged
merged 3 commits into from
Mar 12, 2020
Merged
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
4 changes: 4 additions & 0 deletions packages/url/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Master

### Bug Fixes

- `getQueryString` now correctly considers hash fragments when considering whether to return a query string. Previously, `getQueryString( 'https://example.com/#?foo' )` would wrongly return `'foo'` as its result. A hash fragment is always the last segment of a URL, and the querystring must always precede it ([see reference specification](https://url.spec.whatwg.org/#absolute-url-with-fragment-string)).

## 2.11.0 (2020-02-10)

### Bug Fixes
Expand Down
3 changes: 1 addition & 2 deletions packages/url/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ Returns the query string part of the URL.
_Usage_

```js
const queryString1 = getQueryString( 'http://localhost:8080/this/is/a/test?query=true#fragment' ); // 'query=true'
const queryString2 = getQueryString( 'https://wordpress.org#fragment?query=false&search=hello' ); // 'query=false&search=hello'
const queryString = getQueryString( 'http://localhost:8080/this/is/a/test?query=true#fragment' ); // 'query=true'
```

_Parameters_
Expand Down
13 changes: 8 additions & 5 deletions packages/url/src/get-query-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
*
* @example
* ```js
* const queryString1 = getQueryString( 'http://localhost:8080/this/is/a/test?query=true#fragment' ); // 'query=true'
* const queryString2 = getQueryString( 'https://wordpress.org#fragment?query=false&search=hello' ); // 'query=false&search=hello'
* const queryString = getQueryString( 'http://localhost:8080/this/is/a/test?query=true#fragment' ); // 'query=true'
* ```
*
* @return {string|void} The query string part of the URL.
*/
export function getQueryString( url ) {
const matches = /^\S+?\?([^\s#]+)/.exec( url );
if ( matches ) {
return matches[ 1 ];
let query;
try {
query = new URL( url ).search.substring( 1 );
} catch ( error ) {}

if ( query ) {
return query;
}
}
27 changes: 13 additions & 14 deletions packages/url/src/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,6 @@ describe( 'isValidPath', () => {

describe( 'getQueryString', () => {
it( 'returns the query string of a URL', () => {
expect(
getQueryString(
'https://user:password@www.test-this.com:1020/test-path/file.extension#anchor?query=params&more'
)
).toBe( 'query=params&more' );
expect(
getQueryString(
'http://user:password@www.test-this.com:1020/test-path/file.extension?query=params&more#anchor'
Expand Down Expand Up @@ -305,16 +300,21 @@ describe( 'getQueryString', () => {
'https://andalouses.example/beach?foo[]=bar&foo[]=baz'
)
).toBe( 'foo[]=bar&foo[]=baz' );
expect( getQueryString( 'test.com?foo[]=bar&foo[]=baz' ) ).toBe(
expect( getQueryString( 'https://test.com?foo[]=bar&foo[]=baz' ) ).toBe(
'foo[]=bar&foo[]=baz'
);
expect( getQueryString( 'test.com?foo=bar&foo=baz?test' ) ).toBe(
'foo=bar&foo=baz?test'
);
expect(
getQueryString( 'https://test.com?foo=bar&foo=baz?test' )
).toBe( 'foo=bar&foo=baz?test' );
} );

it( 'returns undefined when the provided does not contain a url query string', () => {
expect( getQueryString( '' ) ).toBeUndefined();
expect(
getQueryString(
'https://user:password@www.test-this.com:1020/test-path/file.extension#anchor?query=params&more'
)
).toBeUndefined();
expect(
getQueryString( 'https://wordpress.org/test-path#anchor' )
).toBeUndefined();
Expand All @@ -326,11 +326,10 @@ describe( 'getQueryString', () => {
).toBeUndefined();
expect( getQueryString( 'https://wordpress.org/' ) ).toBeUndefined();
expect( getQueryString( 'https://localhost:8080' ) ).toBeUndefined();
expect( getQueryString( 'https://' ) ).toBeUndefined();
expect( getQueryString( 'https:///test' ) ).toBeUndefined();
expect( getQueryString( 'https://#' ) ).toBeUndefined();
expect( getQueryString( 'https://?' ) ).toBeUndefined();
expect( getQueryString( 'test.com' ) ).toBeUndefined();
expect( getQueryString( 'invalid' ) ).toBeUndefined();
expect(
getQueryString( 'https://example.com/empty?' )
).toBeUndefined();
} );
} );

Expand Down