|
81 | 81 | }
|
82 | 82 |
|
83 | 83 | // Encode text before inserting into <div> so that the textarea and
|
84 |
| - // overlay don't get out if sync when the textarea contains something |
| 84 | + // overlay don't get out if sync when the textarea contains something |
85 | 85 | // HTML (e.g. "&" or <foo>).
|
86 | 86 | text = htmlDecode(text);
|
87 |
| - |
| 87 | + |
88 | 88 | var matches = [];
|
89 |
| - $.each(this.settings.words, function(color, words) { |
90 |
| - var wordsRe = htmlDecode(words.join("|")); |
| 89 | + |
| 90 | + var wordColorMap = {}; |
| 91 | + $.each(this.settings.words, function (color, words) { |
| 92 | + $.each(words, function (index, word) { |
| 93 | + wordColorMap[word] = color; |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + $.each(wordColorMap, function (word, color) { |
| 98 | + var wordsRe = htmlDecode(word); |
91 | 99 | var re = that.spacer + '(' + wordsRe + ')' + that.spacer;
|
92 | 100 | var regex = new RegExp(re, that.regParam);
|
93 | 101 |
|
94 | 102 | var wordMatches = text.match(regex);
|
95 | 103 | if (wordMatches) {
|
96 |
| - var evaluated = []; |
97 |
| - $.each(words, function(index, match) { |
98 |
| - match = htmlDecode(match); |
99 |
| - |
100 |
| - matches.push(match); |
101 |
| - if (evaluated.indexOf(match) === -1) { |
102 |
| - text = text.replace( |
103 |
| - new RegExp(that.spacer + match + that.spacer, that.regParam), |
104 |
| - function(innerMatch, start, contents) { |
105 |
| - var encodedMatch = innerMatch |
106 |
| - .replace(/[&"<>]/g, function (c) { |
107 |
| - return { |
108 |
| - '&': "&", |
109 |
| - '"': """, |
110 |
| - '<': "<", |
111 |
| - '>': ">" |
112 |
| - }[c]; |
113 |
| - }); |
114 |
| - |
115 |
| - return '<mark style="background-color:'+ color +';">' + encodedMatch + '</mark>'; |
116 |
| - } |
117 |
| - ); |
118 |
| - |
119 |
| - evaluated.push(match); |
120 |
| - } |
121 |
| - }); |
| 104 | + word = htmlDecode(word); |
| 105 | + matches.push(word); |
| 106 | + text = text.replace( |
| 107 | + new RegExp(that.spacer + word + that.spacer, that.regParam), |
| 108 | + function(innerMatch, start, contents) { |
| 109 | + var encodedMatch = innerMatch |
| 110 | + .replace(/[&"<>]/g, function (c) { |
| 111 | + return { |
| 112 | + '&': "&", |
| 113 | + '"': """, |
| 114 | + '<': "<", |
| 115 | + '>': ">" |
| 116 | + }[c]; |
| 117 | + }); |
| 118 | + |
| 119 | + return '<mark style="background-color:'+ color +';">' + encodedMatch + '</mark>'; |
| 120 | + } |
| 121 | + ); |
122 | 122 | }
|
123 | 123 | });
|
124 | 124 |
|
|
592 | 592 | return out;
|
593 | 593 | };
|
594 | 594 |
|
595 |
| - |
| 595 | + |
596 | 596 | /*
|
597 | 597 | * Formats a list of ranges into a hash of arrays (Color => Ranges list)
|
598 | 598 | * @param ranges {mixed}
|
|
0 commit comments