Skip to content

Commit 9437e2e

Browse files
committed
MAGE-889: Add more robust handling of Autocomplete product highlighting
1 parent c80d0c5 commit 9437e2e

File tree

1 file changed

+35
-8
lines changed
  • view/frontend/web/internals/template/autocomplete

1 file changed

+35
-8
lines changed

view/frontend/web/internals/template/autocomplete/products.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ define([], function () {
2222
data-queryId="${item.__autocomplete_queryID}">
2323
<div class="thumb"><img src="${item.thumbnail_url || ''}" alt="${item.name || ''}"/></div>
2424
<div class="info">
25-
${components.Highlight({hit: item, attribute: 'name'})}
25+
${this.safeHighlight(components, item, "name")}
2626
<div class="algoliasearch-autocomplete-category">
2727
${this.getColorHtml(item, components, html)}
2828
${this.getCategoriesHtml(item, components, html)}
@@ -43,16 +43,20 @@ define([], function () {
4343
// Helper methods //
4444
////////////////////
4545

46-
getColorHtml: (item, components, html) => {
47-
if (item._highlightResult.color == undefined || item._highlightResult.color.value == "") return "";
48-
49-
return html`<span class="color">color: ${components.Highlight({ hit: item, attribute: "color" })}</span>`;
46+
getColorHtml: function(item, components, html) {
47+
const highlight = this.safeHighlight(components, item, "color");
48+
49+
return highlight
50+
? html`<span class="color">color: ${highlight}</span>`
51+
: "";
5052
},
5153

52-
getCategoriesHtml: (item, components, html) => {
53-
if (item.categories_without_path == undefined || item.categories_without_path.length == 0) return "";
54+
getCategoriesHtml: function(item, components, html) {
55+
const highlight = this.safeHighlight(components, item, "categories_without_path", false);
5456

55-
return html`<span>in ${components.Highlight({ hit: item, attribute: "categories_without_path",})}</span>`;
57+
return highlight
58+
? html`<span>in ${highlight}</span>`
59+
: "";
5660
},
5761

5862
getOriginalPriceHtml: (item, html, priceGroup) => {
@@ -98,6 +102,29 @@ define([], function () {
98102
return html`${algoliaConfig.translations.seeIn} <span><a href="${resultDetails.allDepartmentsUrl}">${algoliaConfig.translations.allDepartments}</a></span> (${resultDetails.nbHits})
99103
${this.getFooterSearchCategoryLinks(html, resultDetails)}
100104
`;
105+
},
106+
107+
safeHighlight: function(components, hit, attribute, strict = true) {
108+
const highlightResult = hit._highlightResult[attribute];
109+
110+
if (!highlightResult) return '';
111+
112+
if (strict
113+
&&
114+
(
115+
(Array.isArray(highlightResult)) && !highlightResult.find(hit => hit.matchLevel !== 'none')
116+
||
117+
highlightResult.value === ''
118+
)
119+
) {
120+
return '';
121+
}
122+
123+
try {
124+
return components.Highlight({ hit, attribute });
125+
} catch (e) {
126+
return '';
127+
}
101128
}
102129

103130
};

0 commit comments

Comments
 (0)