Skip to content

Commit 26120fe

Browse files
Fix markup diplay when term match with html entity (#7162)
* fix(select2) fix markup diplay when term match with html entity * refactor code * Escape term and text after matching checks Co-authored-by: Cédric Anne <[email protected]>
1 parent 1ed2845 commit 26120fe

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

js/common.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -901,22 +901,22 @@ function markMatch (text, term) {
901901

902902
// If there is no match, move on
903903
if (match < 0) {
904-
_result.append(text);
904+
_result.append(escapeMarkupText(text));
905905
return _result.html();
906906
}
907907

908908
// Put in whatever text is before the match
909-
_result.html(text.substring(0, match));
909+
_result.html(escapeMarkupText(text.substring(0, match)));
910910

911911
// Mark the match
912912
var _match = $('<span class=\'select2-rendered__match\'></span>');
913-
_match.html(text.substring(match, match + term.length));
913+
_match.html(escapeMarkupText(text.substring(match, match + term.length)));
914914

915915
// Append the matching text
916916
_result.append(_match);
917917

918918
// Put in whatever is after the match
919-
_result.append(text.substring(match + term.length));
919+
_result.append(escapeMarkupText(text.substring(match + term.length)));
920920

921921
return _result.html();
922922
}
@@ -936,18 +936,13 @@ var templateResult = function(result) {
936936
}
937937

938938
var text = result.text;
939-
if (text.indexOf('>') !== -1 || text.indexOf('<') !== -1) {
940-
// escape text, if it contains chevrons (can already be escaped prior to this point :/)
941-
text = jQuery.fn.select2.defaults.defaults.escapeMarkup(text);
942-
};
943-
944939
if (!result.id) {
945940
// If result has no id, then it is used as an optgroup and is not used for matches
946-
_elt.html(text);
941+
_elt.html(escapeMarkupText(text));
947942
return _elt;
948943
}
949944

950-
var _term = jQuery.fn.select2.defaults.defaults.escapeMarkup(query.term || '');
945+
var _term = query.term || '';
951946
var markup = markMatch(text, _term);
952947

953948
if (result.level) {
@@ -1007,3 +1002,18 @@ var getTextWithoutDiacriticalMarks = function (text) {
10071002
// They are removed to keep only chars without their diacritical mark.
10081003
return text.replace(/[\u0300-\u036f]/g, '');
10091004
}
1005+
1006+
/**
1007+
* Escape markup in text to prevent XSS.
1008+
*
1009+
* @param {string} text
1010+
*
1011+
* @return {string}
1012+
*/
1013+
var escapeMarkupText = function (text) {
1014+
if (text.indexOf('>') !== -1 || text.indexOf('<') !== -1) {
1015+
// escape text, if it contains chevrons (can already be escaped prior to this point :/)
1016+
text = jQuery.fn.select2.defaults.defaults.escapeMarkup(text);
1017+
};
1018+
return text;
1019+
}

0 commit comments

Comments
 (0)