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({ ...@@ -245,15 +245,7 @@ Polymer({
* @private * @private
*/ */
onSearchChanged_: function(e) { onSearchChanged_: function(e) {
// Trim leading whitespace only, to prevent searching for empty string. This const query = e.detail;
// 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;
}
settings.navigateTo( settings.navigateTo(
settings.routes.BASIC, settings.routes.BASIC,
query.length > 0 ? query.length > 0 ?
......
...@@ -101,7 +101,7 @@ cr.define('cr_toolbar_search_field', function() { ...@@ -101,7 +101,7 @@ cr.define('cr_toolbar_search_field', function() {
// Expecting identical query to be ignored. // Expecting identical query to be ignored.
simulateSearch('query2'); simulateSearch('query2');
assertEquals(['query1', '', 'query2'].join(), searches.join()); assertDeepEquals(['query1', '', 'query2'], searches);
}); });
test('notifies on setValue', function() { test('notifies on setValue', function() {
...@@ -112,7 +112,7 @@ cr.define('cr_toolbar_search_field', function() { ...@@ -112,7 +112,7 @@ cr.define('cr_toolbar_search_field', function() {
// Expecting identical query to be ignored. // Expecting identical query to be ignored.
field.setValue('bar'); field.setValue('bar');
field.setValue('baz'); 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() { test('does not notify on setValue with noEvent=true', function() {
...@@ -120,23 +120,55 @@ cr.define('cr_toolbar_search_field', function() { ...@@ -120,23 +120,55 @@ cr.define('cr_toolbar_search_field', function() {
field.setValue('foo', true); field.setValue('foo', true);
field.setValue('bar'); field.setValue('bar');
field.setValue('baz', true); field.setValue('baz', true);
assertEquals(['bar'].join(), searches.join()); assertDeepEquals(['bar'], searches);
}); });
test('treat consecutive whitespace as single space', function() { test('treat consecutive whitespace as single space', function() {
field.click(); field.click();
const query = ' foo bar baz '; const query = 'foo bar baz';
simulateSearch(query); simulateSearch(query);
Polymer.dom.flush(); Polymer.dom.flush();
assertEquals(query, field.getValue()); assertEquals(query, field.getValue());
// Expecting effectively the same query to be ignored. // Expecting effectively the same query to be ignored.
const effectivelySameQuery = ' foo bar baz '; const effectivelySameQuery = 'foo bar baz';
simulateSearch(effectivelySameQuery); simulateSearch(effectivelySameQuery);
Polymer.dom.flush(); Polymer.dom.flush();
assertEquals(effectivelySameQuery, field.getValue()); 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 // Tests that calling setValue() from within a 'search-changed' callback
...@@ -153,7 +185,7 @@ cr.define('cr_toolbar_search_field', function() { ...@@ -153,7 +185,7 @@ cr.define('cr_toolbar_search_field', function() {
field.click(); field.click();
field.setValue('bar'); field.setValue('bar');
assertEquals(1, counter); assertEquals(1, counter);
assertEquals(['bar'].join(), searches.join()); assertDeepEquals(['bar'], searches);
}); });
test('blur does not close field when a search is active', function() { test('blur does not close field when a search is active', function() {
......
...@@ -106,7 +106,10 @@ const CrSearchFieldBehavior = { ...@@ -106,7 +106,10 @@ const CrSearchFieldBehavior = {
* @private * @private
*/ */
onValueChanged_: function(newValue, noEvent) { 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_) { if (effectiveValue == this.lastValue_) {
return; 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