@@ -33,8 +33,18 @@ var DEFAULT_SETTINGS = {
33
33
theme : null ,
34
34
zindex : 999 ,
35
35
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
+ } ,
38
48
39
49
// Tokenization settings
40
50
tokenLimit : null ,
@@ -102,6 +112,27 @@ var KEY = {
102
112
COMMA : 188
103
113
} ;
104
114
115
+ var HTML_ESCAPES = {
116
+ '&' : '&' ,
117
+ '<' : '<' ,
118
+ '>' : '>' ,
119
+ '"' : '"' ,
120
+ "'" : ''' ,
121
+ '/' : '/'
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
+
105
136
// Additional public (exposed) methods
106
137
var methods = {
107
138
init : function ( url_or_data_or_function , options ) {
@@ -456,6 +487,10 @@ $.TokenList = function (input, url_or_data, settings) {
456
487
// Private functions
457
488
//
458
489
490
+ function escapeHTML ( text ) {
491
+ return settings . enableHTML ? text : _escapeHTML ( text ) ;
492
+ }
493
+
459
494
// Toggles the widget between enabled and disabled state, or according
460
495
// to the [disable] parameter.
461
496
function toggleDisabled ( disable ) {
@@ -485,8 +520,7 @@ $.TokenList = function (input, url_or_data, settings) {
485
520
if ( input_val === ( input_val = input_box . val ( ) ) ) { return ; }
486
521
487
522
// Enter new content into resizer and resize input accordingly
488
- var escaped = input_val . replace ( / & / g, '&' ) . replace ( / \s / g, ' ' ) . replace ( / < / g, '<' ) . replace ( / > / g, '>' ) ;
489
- input_resizer . html ( escaped ) ;
523
+ input_resizer . html ( _escapeHTML ( input_val ) ) ;
490
524
input_box . width ( input_resizer . width ( ) + 30 ) ;
491
525
}
492
526
@@ -720,14 +754,14 @@ $.TokenList = function (input, url_or_data, settings) {
720
754
721
755
function show_dropdown_searching ( ) {
722
756
if ( settings . searchingText ) {
723
- dropdown . html ( "<p>" + settings . searchingText + "</p>" ) ;
757
+ dropdown . html ( "<p>" + escapeHTML ( settings . searchingText ) + "</p>" ) ;
724
758
show_dropdown ( ) ;
725
759
}
726
760
}
727
761
728
762
function show_dropdown_hint ( ) {
729
763
if ( settings . hintText ) {
730
- dropdown . html ( "<p>" + settings . hintText + "</p>" ) ;
764
+ dropdown . html ( "<p>" + escapeHTML ( settings . hintText ) + "</p>" ) ;
731
765
show_dropdown ( ) ;
732
766
}
733
767
}
@@ -739,7 +773,14 @@ $.TokenList = function (input, url_or_data, settings) {
739
773
740
774
// Highlight the query part of the search term
741
775
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
+ ) ;
743
784
}
744
785
745
786
function find_value_and_highlight_term ( template , value , term ) {
@@ -795,7 +836,7 @@ $.TokenList = function (input, url_or_data, settings) {
795
836
}
796
837
} else {
797
838
if ( settings . noResultsText ) {
798
- dropdown . html ( "<p>" + settings . noResultsText + "</p>" ) ;
839
+ dropdown . html ( "<p>" + escapeHTML ( settings . noResultsText ) + "</p>" ) ;
799
840
show_dropdown ( ) ;
800
841
}
801
842
}
0 commit comments