Skip to content

Commit b1ff0d9

Browse files
committed
Fix <mark> elements being replaced bug by matching words at one time.
1 parent dec975f commit b1ff0d9

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

jquery.highlighttextarea.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -81,44 +81,44 @@
8181
}
8282

8383
// 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
8585
// HTML (e.g. "&amp;" or <foo>).
8686
text = htmlDecode(text);
87-
87+
8888
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);
9199
var re = that.spacer + '(' + wordsRe + ')' + that.spacer;
92100
var regex = new RegExp(re, that.regParam);
93101

94102
var wordMatches = text.match(regex);
95103
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-
'&': "&amp;",
109-
'"': "&quot;",
110-
'<': "&lt;",
111-
'>': "&gt;"
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+
'&': "&amp;",
113+
'"': "&quot;",
114+
'<': "&lt;",
115+
'>': "&gt;"
116+
}[c];
117+
});
118+
119+
return '<mark style="background-color:'+ color +';">' + encodedMatch + '</mark>';
120+
}
121+
);
122122
}
123123
});
124124

@@ -592,7 +592,7 @@
592592
return out;
593593
};
594594

595-
595+
596596
/*
597597
* Formats a list of ranges into a hash of arrays (Color => Ranges list)
598598
* @param ranges {mixed}

0 commit comments

Comments
 (0)