Skip to content

Commit 45f4d1f

Browse files
authored
Merge pull request #1505 from algolia/bugfix/MAGE-889
Bugfix/mage 889 - Autocomplete highlight robustness
2 parents 4c430b9 + 9add9c4 commit 45f4d1f

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,24 @@ define([], function () {
1515
data-position="${item.position}"
1616
data-index="${item.__autocomplete_indexName}"
1717
data-queryId="${item.__autocomplete_queryID}">
18-
${components.Highlight({ hit: item, attribute: 'path' })} (${item.product_count})
18+
${this.safeHighlight(components, item, "path")} (${item.product_count})
1919
</a>`;
2020
},
2121

2222
getFooterHtml: function () {
2323
return "";
2424
},
25+
26+
safeHighlight: function(components, hit, attribute) {
27+
const highlightResult = hit._highlightResult[attribute];
28+
29+
if (!highlightResult) return '';
30+
31+
try {
32+
return components.Highlight({ hit, attribute });
33+
} catch (e) {
34+
return '';
35+
}
36+
}
2537
};
2638
});

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ define([], function () {
1616
data-index="${item.__autocomplete_indexName}"
1717
data-queryId="${item.__autocomplete_queryID}">
1818
<div class="info-without-thumb">
19-
${components.Highlight({hit: item, attribute: 'name'})}
19+
${this.safeHighlight(components, item, "name")}
2020
<div class="details">
21-
${components.Highlight({hit: item, attribute: 'content'})}
21+
${this.safeHighlight(components, item, "content")}
2222
</div>
2323
</div>
2424
<div class="algolia-clearfix"></div>
@@ -27,6 +27,19 @@ define([], function () {
2727

2828
getFooterHtml: function () {
2929
return "";
30+
},
31+
32+
safeHighlight: function(components, hit, attribute) {
33+
const highlightResult = hit._highlightResult[attribute];
34+
35+
if (!highlightResult) return '';
36+
37+
try {
38+
return components.Highlight({ hit, attribute });
39+
} catch (e) {
40+
return '';
41+
}
3042
}
43+
3144
};
3245
});

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

Lines changed: 36 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,30 @@ 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+
// TODO: Refactor to external lib
108+
safeHighlight: function(components, hit, attribute, strict = true) {
109+
const highlightResult = hit._highlightResult[attribute];
110+
111+
if (!highlightResult) return '';
112+
113+
if (strict
114+
&&
115+
(
116+
(Array.isArray(highlightResult)) && !highlightResult.find(hit => hit.matchLevel !== 'none')
117+
||
118+
highlightResult.value === ''
119+
)
120+
) {
121+
return '';
122+
}
123+
124+
try {
125+
return components.Highlight({ hit, attribute });
126+
} catch (e) {
127+
return '';
128+
}
101129
}
102130

103131
};

0 commit comments

Comments
 (0)