Gravatar for fcote@coveo.com

Question by fcote, May 19, 2015 3:39 PM

White space in Search as you Type and handleQueryStateChanged

We are using CoveoJsSearch.WithDependencies.js library and it is trimming spaces from the end of the search input. This is causing issues for users trying to search using more than one keyword. For instance, if a user wishes to search "Convection Oven" and pauses after typing "Convection ", Coveo will run the search and overwrite the value of the input tag with a trimmed string "Convection". Then when the user continues typing, they end up concatenating their keywords with no spaces and the search term becomes "ConvectionOven".

Is this a known issue and is their a fix for this? I have traced CoveoJsSearch.WithDependencies.js and found that the replace happens in the QueryBox.prototype._handleQueryStateChanged method (line 21245 of our file version). The full method is:

        QueryBox.prototype._handleQueryStateChanged = function (e, data) {
            Coveo.Assert.exists(data);
            var q = data.value;
            Coveo.$(this.element).val(q);
            this._addClearElement();
        };

Our work around is to test the element value and the data.value and if the current element value with extra whitespace matches data.value, then we do not replace the element value.

        QueryBox.prototype._handleQueryStateChanged = function (e, data) {
            Coveo.Assert.exists(data);
            var q = data.value;
            var val = Coveo.$(this.element).val();
            if (!val.match(q + '[\\s]*')) {
                Coveo.$(this.element).val(q);
            }
            this._addClearElement();
        };

Please let me know if there is a better fix for this.

1 Reply
Gravatar for olamothe@coveo.com

Answer by olamothe, May 19, 2015 4:19 PM

OOTB, I was not able to reproduce the behavior you just described
Do you have any custom code that could interact with the q attribute in the state ? Be it on buildingQuery or doneBuildingQuery ?
Could any custom code inadvertently be trimming the string ?

Ask a question