From d70b1ff177fb3d886473841daab6ee28d4ce1653 Mon Sep 17 00:00:00 2001 From: Mike Brocchi Date: Wed, 8 May 2019 21:13:50 -0400 Subject: [PATCH] fix(docs-infra): Handle search criteria from Chrome search providers (#30345) fixes #30242 PR Close #30345 --- .../app/search/search-box/search-box.component.spec.ts | 10 ++++++++++ aio/src/app/search/search-box/search-box.component.ts | 7 ++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/aio/src/app/search/search-box/search-box.component.spec.ts b/aio/src/app/search/search-box/search-box.component.spec.ts index 1c41741a24..ca35bb90b4 100644 --- a/aio/src/app/search/search-box/search-box.component.spec.ts +++ b/aio/src/app/search/search-box/search-box.component.spec.ts @@ -44,6 +44,16 @@ describe('SearchBoxComponent', () => { expect(host.searchHandler).toHaveBeenCalledWith('initial search'); expect(component.searchBox.nativeElement.value).toEqual('initial search'); }))); + + it('should decode the search query from the location service (chrome search provider format)', + fakeAsync(inject([LocationService], (location: MockLocationService) => { + location.search.and.returnValue({ search: 'initial+search' }); + component.ngAfterViewInit(); + expect(location.search).toHaveBeenCalled(); + tick(300); + expect(host.searchHandler).toHaveBeenCalledWith('initial search'); + expect(component.searchBox.nativeElement.value).toEqual('initial search'); + }))); }); describe('onSearch', () => { diff --git a/aio/src/app/search/search-box/search-box.component.ts b/aio/src/app/search/search-box/search-box.component.ts index 7033d0b2d3..10b90866a2 100644 --- a/aio/src/app/search/search-box/search-box.component.ts +++ b/aio/src/app/search/search-box/search-box.component.ts @@ -43,7 +43,7 @@ export class SearchBoxComponent implements AfterViewInit { ngAfterViewInit() { const query = this.locationService.search()['search']; if (query) { - this.query = query; + this.query = this.decodeQuery(query); this.doSearch(); } } @@ -60,6 +60,11 @@ export class SearchBoxComponent implements AfterViewInit { this.searchBox.nativeElement.focus(); } + private decodeQuery(query: string): string { + // `decodeURIComponent` does not handle `+` for spaces, replace via RexEx. + return query.replace(/\+/g, ' '); + } + private get query() { return this.searchBox.nativeElement.value; } private set query(value: string) { this.searchBox.nativeElement.value = value; } }