fix(docs-infra): Handle search criteria from Chrome search providers (#30345)

fixes #30242

PR Close #30345
This commit is contained in:
Mike Brocchi 2019-05-08 21:13:50 -04:00 committed by Alex Rickabaugh
parent 3aff79c251
commit d70b1ff177
2 changed files with 16 additions and 1 deletions

View File

@ -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', () => {

View File

@ -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; }
}