|
1 | 1 | define(['jquery', 'algoliaBundle'], function ($, algoliaBundle) {
|
| 2 | + // Character maps supplied for more performant Regex ops |
| 3 | + const SPECIAL_CHAR_ENCODE_MAP = { |
| 4 | + '&': '&', |
| 5 | + '<': '<', |
| 6 | + '>': '>', |
| 7 | + '"': '"', |
| 8 | + "'": ''' |
| 9 | + }; |
| 10 | + |
| 11 | + /// Reverse key / value pair |
| 12 | + const SPECIAL_CHAR_DECODE_MAP = Object.entries(SPECIAL_CHAR_ENCODE_MAP).reduce((acc, [key, value]) => { |
| 13 | + acc[value] = key; |
| 14 | + return acc; |
| 15 | + }, {}); |
2 | 16 |
|
3 |
| - window.algolia = { |
| 17 | + window.algolia = { |
4 | 18 | deprecatedHooks: [
|
5 | 19 | 'beforeAutocompleteProductSourceOptions',
|
6 | 20 | 'beforeAutocompleteSources'
|
@@ -64,23 +78,14 @@ define(['jquery', 'algoliaBundle'], function ($, algoliaBundle) {
|
64 | 78 |
|
65 | 79 | return data;
|
66 | 80 | },
|
67 |
| - htmlspecialcharsDecode: function(string) { |
68 |
| - var unescapedString = string, |
69 |
| - specialchars = [ |
70 |
| - [ '"', '"' ], |
71 |
| - [ '>', '>' ], |
72 |
| - [ '<', '<' ], |
73 |
| - [ '&', '&' ], |
74 |
| - [ "'", ''' ] |
75 |
| - ]; |
76 |
| - |
77 |
| - var len = specialchars.length; |
78 |
| - for (var i=0; i<len; i++) { |
79 |
| - unescapedString = unescapedString.replace(new RegExp(specialchars[i][1], 'g'), specialchars[i][0]); |
80 |
| - } |
81 |
| - |
82 |
| - return unescapedString; |
83 |
| - } |
| 81 | + htmlspecialcharsDecode: string => { |
| 82 | + const regex = new RegExp(Object.keys(SPECIAL_CHAR_DECODE_MAP).join('|'), 'g'); |
| 83 | + return string.replace(regex, m => SPECIAL_CHAR_DECODE_MAP[m]); |
| 84 | + }, |
| 85 | + htmlspecialcharsEncode: string => { |
| 86 | + const regex = new RegExp(`[${Object.keys(SPECIAL_CHAR_ENCODE_MAP).join('')}]`, 'g'); |
| 87 | + return string.replace(regex, (m) => SPECIAL_CHAR_ENCODE_MAP[m]); |
| 88 | + } |
84 | 89 | };
|
85 | 90 |
|
86 | 91 | window.isMobile = function () {
|
|
0 commit comments