From 2c54fb07725fb73f1bc7189e6ed1a52e053ecaa3 Mon Sep 17 00:00:00 2001 From: udayk Date: Thu, 17 Feb 2022 13:27:32 +0530 Subject: [PATCH] fix(Typeahead) Typeahead highlight displays tag when empty spaces added at the end of query and does not escape html tags --- src/typeahead/typeahead-container.component.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/typeahead/typeahead-container.component.ts b/src/typeahead/typeahead-container.component.ts index 0bcf48e596..085b72c378 100644 --- a/src/typeahead/typeahead-container.component.ts +++ b/src/typeahead/typeahead-container.component.ts @@ -264,13 +264,18 @@ export class TypeaheadContainerComponent implements OnDestroy { ? latinize(itemStr) : itemStr).toLowerCase(); let startIdx: number; - let tokenLen: number; + let tokenLen = 0; // Replaces the capture string with the same string inside of a "strong" tag if (typeof query === 'object') { const queryLen: number = query.length; + let indexOfTrimmedMatch:number; for (let i = 0; i < queryLen; i += 1) { // query[i] is already latinized and lower case - startIdx = itemStrHelper.indexOf(query[i]); + // When user type empty string as query, it always taking first index(0) instead of space after the query word. + // as result it adding the html markup, so to solve this issue we are trimming the match and find the index. + // If it is valid index adding token lenth to it + indexOfTrimmedMatch = itemStrHelper.trim().indexOf(query[i]); + startIdx = query[i].trim() ? itemStrHelper.indexOf(query[i]) : indexOfTrimmedMatch >= 0 ? indexOfTrimmedMatch + tokenLen : -1; tokenLen = query[i].length; if (startIdx >= 0 && tokenLen > 0) { itemStr =