@@ -22,7 +22,7 @@ define([], function () {
22
22
data-queryId ="${ item . __autocomplete_queryID } ">
23
23
< div class ="thumb "> < img src ="${ item . thumbnail_url || '' } " alt ="${ item . name || '' } "/> </ div >
24
24
< div class ="info ">
25
- ${ components . Highlight ( { hit : item , attribute : ' name' } ) }
25
+ ${ this . safeHighlight ( components , item , " name" ) }
26
26
< div class ="algoliasearch-autocomplete-category ">
27
27
${ this . getColorHtml ( item , components , html ) }
28
28
${ this . getCategoriesHtml ( item , components , html ) }
@@ -43,16 +43,20 @@ define([], function () {
43
43
// Helper methods //
44
44
////////////////////
45
45
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
+ : "" ;
50
52
} ,
51
53
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 ) ;
54
56
55
- return html `< span > in ${ components . Highlight ( { hit : item , attribute : "categories_without_path" , } ) } </ span > ` ;
57
+ return highlight
58
+ ? html `< span > in ${ highlight } </ span > `
59
+ : "" ;
56
60
} ,
57
61
58
62
getOriginalPriceHtml : ( item , html , priceGroup ) => {
@@ -98,6 +102,30 @@ define([], function () {
98
102
return html `${ algoliaConfig . translations . seeIn } < span > < a href ="${ resultDetails . allDepartmentsUrl } "> ${ algoliaConfig . translations . allDepartments } </ a > </ span > (${ resultDetails . nbHits } )
99
103
${ this . getFooterSearchCategoryLinks ( html , resultDetails ) }
100
104
` ;
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
+ }
101
129
}
102
130
103
131
} ;
0 commit comments