Skip to content

Commit 9add9c4

Browse files
committed
MAGE-889: Add highlight safeguards to category / pages template
1 parent 9437e2e commit 9add9c4

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ define([], function () {
104104
`;
105105
},
106106

107+
// TODO: Refactor to external lib
107108
safeHighlight: function(components, hit, attribute, strict = true) {
108109
const highlightResult = hit._highlightResult[attribute];
109110

0 commit comments

Comments
 (0)