Skip to content

Commit

Permalink
amp-autocomplete: preserve existing query parameters in src (#26589)
Browse files Browse the repository at this point in the history
Currently, if the `query` attribute is specified, existing query parameters in the
source url are replaced with the specified value.
  • Loading branch information
JoyceBabu committed Feb 13, 2020
1 parent c701777 commit cd15c73
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
5 changes: 2 additions & 3 deletions extensions/amp-autocomplete/0.1/amp-autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
batchFetchJsonFor,
requestForBatchFetch,
} from '../../../src/batched-json';
import {addParamToUrl} from '../../../src/url';
import {createCustomEvent} from '../../../src/event-helper';
import {dev, user, userAssert} from '../../../src/log';
import {dict, hasOwn, map, ownProperty} from '../../../src/utils/object';
Expand Down Expand Up @@ -439,9 +440,7 @@ export class AmpAutocomplete extends AMP.BaseElement {
* @private
*/
generateSrc_(opt_query = '') {
const encodedQueryKey = encodeURIComponent(this.queryKey_);
const encodedQuery = encodeURIComponent(opt_query);
return `${this.srcBase_}?${encodedQueryKey}=${encodedQuery}`;
return addParamToUrl(this.srcBase_, this.queryKey_, opt_query);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions extensions/amp-autocomplete/0.1/test/test-amp-autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -996,5 +996,18 @@ describes.realWin(
);
});
});

it('should preserve existing query parameters when generating src values from "query" attribute', () => {
return impl.layoutCallback().then(() => {
impl.queryKey_ = 'q';
impl.srcBase_ = 'https://www.data.com/?param=1';
expect(impl.generateSrc_('')).to.equal(
'https://www.data.com/?param=1&q='
);
expect(impl.generateSrc_('abc')).to.equal(
'https://www.data.com/?param=1&q=abc'
);
});
});
}
);

0 comments on commit cd15c73

Please sign in to comment.