Skip to content
This repository was archived by the owner on Sep 10, 2023. It is now read-only.

Commit 317bc55

Browse files
committed
Merge pull request #366 from kourge/escape_html
Escape HTML
2 parents 6481b00 + 10e1a63 commit 317bc55

File tree

1 file changed

+49
-8
lines changed

1 file changed

+49
-8
lines changed

src/jquery.tokeninput.js

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,18 @@ var DEFAULT_SETTINGS = {
3333
theme: null,
3434
zindex: 999,
3535
resultsLimit: null,
36-
resultsFormatter: function(item){ return "<li>" + item[this.propertyToSearch]+ "</li>" },
37-
tokenFormatter: function(item) { return "<li><p>" + item[this.propertyToSearch] + "</p></li>" },
36+
37+
enableHTML: false,
38+
39+
resultsFormatter: function(item) {
40+
var string = item[this.propertyToSearch];
41+
return "<li>" + (this.enableHTML ? string : _escapeHTML(string)) + "</li>";
42+
},
43+
44+
tokenFormatter: function(item) {
45+
var string = item[this.propertyToSearch];
46+
return "<li><p>" + (this.enableHTML ? string : _escapeHTML(string)) + "</p></li>";
47+
},
3848

3949
// Tokenization settings
4050
tokenLimit: null,
@@ -102,6 +112,27 @@ var KEY = {
102112
COMMA: 188
103113
};
104114

115+
var HTML_ESCAPES = {
116+
'&': '&amp;',
117+
'<': '&lt;',
118+
'>': '&gt;',
119+
'"': '&quot;',
120+
"'": '&#x27;',
121+
'/': '&#x2F;'
122+
};
123+
124+
var HTML_ESCAPE_CHARS = /[&<>"'\/]/g;
125+
126+
function coerceToString(val) {
127+
return String((val === null || val === undefined) ? '' : val);
128+
}
129+
130+
function _escapeHTML(text) {
131+
return coerceToString(text).replace(HTML_ESCAPE_CHARS, function(match) {
132+
return HTML_ESCAPES[match];
133+
});
134+
}
135+
105136
// Additional public (exposed) methods
106137
var methods = {
107138
init: function(url_or_data_or_function, options) {
@@ -456,6 +487,10 @@ $.TokenList = function (input, url_or_data, settings) {
456487
// Private functions
457488
//
458489

490+
function escapeHTML(text) {
491+
return settings.enableHTML ? text : _escapeHTML(text);
492+
}
493+
459494
// Toggles the widget between enabled and disabled state, or according
460495
// to the [disable] parameter.
461496
function toggleDisabled(disable) {
@@ -485,8 +520,7 @@ $.TokenList = function (input, url_or_data, settings) {
485520
if(input_val === (input_val = input_box.val())) {return;}
486521

487522
// Enter new content into resizer and resize input accordingly
488-
var escaped = input_val.replace(/&/g, '&amp;').replace(/\s/g,' ').replace(/</g, '&lt;').replace(/>/g, '&gt;');
489-
input_resizer.html(escaped);
523+
input_resizer.html(_escapeHTML(input_val));
490524
input_box.width(input_resizer.width() + 30);
491525
}
492526

@@ -720,14 +754,14 @@ $.TokenList = function (input, url_or_data, settings) {
720754

721755
function show_dropdown_searching () {
722756
if(settings.searchingText) {
723-
dropdown.html("<p>"+settings.searchingText+"</p>");
757+
dropdown.html("<p>" + escapeHTML(settings.searchingText) + "</p>");
724758
show_dropdown();
725759
}
726760
}
727761

728762
function show_dropdown_hint () {
729763
if(settings.hintText) {
730-
dropdown.html("<p>"+settings.hintText+"</p>");
764+
dropdown.html("<p>" + escapeHTML(settings.hintText) + "</p>");
731765
show_dropdown();
732766
}
733767
}
@@ -739,7 +773,14 @@ $.TokenList = function (input, url_or_data, settings) {
739773

740774
// Highlight the query part of the search term
741775
function highlight_term(value, term) {
742-
return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + regexp_escape(term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<b>$1</b>");
776+
return value.replace(
777+
new RegExp(
778+
"(?![^&;]+;)(?!<[^<>]*)(" + regexp_escape(term) + ")(?![^<>]*>)(?![^&;]+;)",
779+
"gi"
780+
), function(match, p1) {
781+
return "<b>" + escapeHTML(p1) + "</b>";
782+
}
783+
);
743784
}
744785

745786
function find_value_and_highlight_term(template, value, term) {
@@ -795,7 +836,7 @@ $.TokenList = function (input, url_or_data, settings) {
795836
}
796837
} else {
797838
if(settings.noResultsText) {
798-
dropdown.html("<p>"+settings.noResultsText+"</p>");
839+
dropdown.html("<p>" + escapeHTML(settings.noResultsText) + "</p>");
799840
show_dropdown();
800841
}
801842
}

0 commit comments

Comments
 (0)