Commit 1094c7da authored by Esmael El-Moslimany's avatar Esmael El-Moslimany Committed by Commit Bot

WebUI: ignore leading whitespace in toolbar search field

Bug: 928729
Change-Id: Ia5269a1f26742cd0a9d7fdf0fbe12aeccaafa65b
Reviewed-on: https://chromium-review.googlesource.com/c/1455678Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Commit-Queue: Esmael El-Moslimany <aee@chromium.org>
Cr-Commit-Position: refs/heads/master@{#636340}
parent 5c35d3ae
......@@ -245,15 +245,7 @@ Polymer({
* @private
*/
onSearchChanged_: function(e) {
// Trim leading whitespace only, to prevent searching for empty string. This
// still allows the user to search for 'foo bar', while taking a long pause
// after typing 'foo '.
const query = e.detail.replace(/^\s+/, '');
// Prevent duplicate history entries.
if (query == this.lastSearchQuery_) {
return;
}
const query = e.detail;
settings.navigateTo(
settings.routes.BASIC,
query.length > 0 ?
......
......@@ -101,7 +101,7 @@ cr.define('cr_toolbar_search_field', function() {
// Expecting identical query to be ignored.
simulateSearch('query2');
assertEquals(['query1', '', 'query2'].join(), searches.join());
assertDeepEquals(['query1', '', 'query2'], searches);
});
test('notifies on setValue', function() {
......@@ -112,7 +112,7 @@ cr.define('cr_toolbar_search_field', function() {
// Expecting identical query to be ignored.
field.setValue('bar');
field.setValue('baz');
assertEquals(['foo', '', 'bar', 'baz'].join(), searches.join());
assertDeepEquals(['foo', '', 'bar', 'baz'], searches);
});
test('does not notify on setValue with noEvent=true', function() {
......@@ -120,23 +120,55 @@ cr.define('cr_toolbar_search_field', function() {
field.setValue('foo', true);
field.setValue('bar');
field.setValue('baz', true);
assertEquals(['bar'].join(), searches.join());
assertDeepEquals(['bar'], searches);
});
test('treat consecutive whitespace as single space', function() {
field.click();
const query = ' foo bar baz ';
const query = 'foo bar baz';
simulateSearch(query);
Polymer.dom.flush();
assertEquals(query, field.getValue());
// Expecting effectively the same query to be ignored.
const effectivelySameQuery = ' foo bar baz ';
const effectivelySameQuery = 'foo bar baz';
simulateSearch(effectivelySameQuery);
Polymer.dom.flush();
assertEquals(effectivelySameQuery, field.getValue());
assertEquals(' foo bar baz ', searches.join());
assertDeepEquals(['foo bar baz'], searches);
});
test('ignore leading whitespace', () => {
field.click();
const query = ' foo';
simulateSearch(query);
Polymer.dom.flush();
assertEquals(query, field.getValue());
// Expecting effectively the same query to be ignored.
const effectivelySameQuery = ' foo';
simulateSearch(effectivelySameQuery);
Polymer.dom.flush();
assertEquals(effectivelySameQuery, field.getValue());
assertDeepEquals(['foo'], searches);
});
test('when there is trailing whitespace, replace with one space', () => {
field.click();
const query = 'foo ';
simulateSearch(query);
Polymer.dom.flush();
assertEquals(query, field.getValue());
// Expecting effectively the same query to be ignored.
const effectivelySameQuery = 'foo ';
simulateSearch(effectivelySameQuery);
Polymer.dom.flush();
assertEquals(effectivelySameQuery, field.getValue());
assertDeepEquals(['foo '], searches);
});
// Tests that calling setValue() from within a 'search-changed' callback
......@@ -153,7 +185,7 @@ cr.define('cr_toolbar_search_field', function() {
field.click();
field.setValue('bar');
assertEquals(1, counter);
assertEquals(['bar'].join(), searches.join());
assertDeepEquals(['bar'], searches);
});
test('blur does not close field when a search is active', function() {
......
......@@ -106,7 +106,10 @@ const CrSearchFieldBehavior = {
* @private
*/
onValueChanged_: function(newValue, noEvent) {
const effectiveValue = newValue.replace(/\s+/g, ' ');
// Trim leading whitespace and replace consecutive whitespace with single
// space. This will prevent empty string searches and searches for
// effectively the same query.
const effectiveValue = newValue.replace(/\s+/g, ' ').replace(/^\s/, '');
if (effectiveValue == this.lastValue_) {
return;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment