diff --git a/components/button/index.js b/components/button/index.js index 9600b55aba423a..7edbd0befa6060 100644 --- a/components/button/index.js +++ b/components/button/index.js @@ -19,7 +19,7 @@ function Button( { href, isPrimary, isLarge, isToggled, className, buttonRef, .. ...tagProps, ...additionalProps, className: classes, - ref: buttonRef + ref: buttonRef, } ); } diff --git a/editor/selectors.js b/editor/selectors.js index 2ae19ccd03f4f2..ddacc7b1851247 100644 --- a/editor/selectors.js +++ b/editor/selectors.js @@ -2,7 +2,11 @@ * External dependencies */ import { first, last } from 'lodash'; -import qs from 'qs'; + +/** + * Internal dependencies + */ +import { addQueryArgs } from './utils/url'; export function getEditorMode( state ) { return state.mode; @@ -44,12 +48,7 @@ export function getEditedPostPreviewLink( state ) { return null; } - const queryStringPosition = link.indexOf( '?' ); - const baseUrl = queryStringPosition !== -1 ? link.substring( 0, queryStringPosition ) : link; - const args = queryStringPosition !== -1 ? qs.parse( link.substring( queryStringPosition + 1 ) ) : {}; - args.preview = 'true'; - - return `${ baseUrl }?${ qs.stringify( args ) }`; + return addQueryArgs( link, { preview: 'true' } ); } export function getBlock( state, uid ) { diff --git a/editor/test/selectors.js b/editor/test/selectors.js index 40606b048c6133..d1d7c3d7ab3230 100644 --- a/editor/test/selectors.js +++ b/editor/test/selectors.js @@ -199,25 +199,15 @@ describe( 'selectors', () => { expect( getEditedPostPreviewLink( state ) ).to.be.null(); } ); - it( 'should return the correct url adding a query string', () => { + it( 'should return the correct url adding a preview parameter to the query string', () => { const state = { currentPost: { - link: 'https://andalouses.com/beach' - } + link: 'https://andalouses.com/beach', + }, }; expect( getEditedPostPreviewLink( state ) ).to.equal( 'https://andalouses.com/beach?preview=true' ); } ); - - it( 'should return the correct url concatening the query string', () => { - const state = { - currentPost: { - link: 'https://andalouses.com/?p=1' - } - }; - - expect( getEditedPostPreviewLink( state ) ).to.equal( 'https://andalouses.com/?p=1&preview=true' ); - } ); } ); describe( 'getBlock', () => { diff --git a/editor/utils/test/url.js b/editor/utils/test/url.js new file mode 100644 index 00000000000000..2688667e361dd8 --- /dev/null +++ b/editor/utils/test/url.js @@ -0,0 +1,25 @@ +/** + * External dependencies + */ +import { expect } from 'chai'; + +/** + * Internal dependencies + */ +import { addQueryArgs } from '../url'; + +describe( 'addQueryArgs', () => { + it( 'should append args to an URL without query string', () => { + const url = 'https://andalouses.com/beach'; + const args = { sun: 'true', sand: 'false' }; + + expect( addQueryArgs( url, args ) ).to.eql( 'https://andalouses.com/beach?sun=true&sand=false' ); + } ); + + it( 'should append args to an URL with query string', () => { + const url = 'https://andalouses.com/beach?night=false'; + const args = { sun: 'true', sand: 'false' }; + + expect( addQueryArgs( url, args ) ).to.eql( 'https://andalouses.com/beach?night=false&sun=true&sand=false' ); + } ); +} ); diff --git a/editor/utils/url.js b/editor/utils/url.js new file mode 100644 index 00000000000000..d806ea2c552de8 --- /dev/null +++ b/editor/utils/url.js @@ -0,0 +1,20 @@ +/** + * External dependencies + */ +import { parse, format } from 'url'; + +/** + * Appends arguments to the query string of the url + * + * @param {String} url URL + * @param {Object} args Query Args + * + * @return {String} Updated URL + */ +export function addQueryArgs( url, args ) { + const parsedUrl = parse( url, true ); + const query = { ...parsedUrl.query, ...args }; + delete parsedUrl.search; + + return format( { ...parsedUrl, query } ); +} diff --git a/package.json b/package.json index 23b66c26eb62f5..f9d5cf293845d4 100644 --- a/package.json +++ b/package.json @@ -76,7 +76,6 @@ "jed": "^1.1.1", "js-beautify": "^1.6.12", "lodash": "^4.17.4", - "qs": "^6.4.0", "react": "^15.5.4", "react-autosize-textarea": "^0.4.2", "react-click-outside": "^2.3.0",