Skip to content

Commit 97eb7af

Browse files
committed
Merge pull request twitter#1182 from jharding/964-escape-html-chars
Escape html chars from display value when using default template
2 parents 250a5dc + 9a74298 commit 97eb7af

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

doc/jquery_typeahead.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ Datasets can be configured using the following options.
169169

170170
* `suggestion` – Used to render a single suggestion. If set, this has to be a
171171
precompiled template. The associated suggestion object will serve as the
172-
context. Defaults to the value of `display` wrapped in a `p` tag i.e.
173-
`<p>{{value}}</p>`.
172+
context. Defaults to the value of `display` wrapped in a `div` tag i.e.
173+
`<div>{{value}}</div>`.
174174

175175
### Custom Events
176176

src/typeahead/dataset.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ var Dataset = (function() {
318318
};
319319

320320
function suggestionTemplate(context) {
321-
return '<div><p>' + displayFn(context) + '</p></div>';
321+
return $('<div>').text(displayFn(context));
322322
}
323323
}
324324

test/typeahead/dataset_spec.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ describe('Dataset', function() {
44
mockSuggestions = [
55
{ value: 'one', raw: { value: 'one' } },
66
{ value: 'two', raw: { value: 'two' } },
7-
{ value: 'three', raw: { value: 'three' } }
7+
{ value: 'html', raw: { value: '<b>html</b>' } }
88
];
99

1010
mockSuggestionsDisplayFn = [
@@ -53,7 +53,14 @@ describe('Dataset', function() {
5353

5454
expect(this.dataset.$el).toContainText('one');
5555
expect(this.dataset.$el).toContainText('two');
56-
expect(this.dataset.$el).toContainText('three');
56+
expect(this.dataset.$el).toContainText('html');
57+
});
58+
59+
it('should escape html chars from display value when using default template', function() {
60+
this.source.andCallFake(syncMockSuggestions);
61+
this.dataset.update('woah');
62+
63+
expect(this.dataset.$el).toContainText('<b>html</b>');
5764
});
5865

5966
it('should respect limit option', function() {
@@ -340,7 +347,7 @@ describe('Dataset', function() {
340347
runs(function() {
341348
expect(this.dataset.$el).toContainText('one');
342349
expect(this.dataset.$el).toContainText('two');
343-
expect(this.dataset.$el).toContainText('three');
350+
expect(this.dataset.$el).toContainText('html');
344351
expect(this.dataset.$el).not.toContainText('four');
345352
expect(this.dataset.$el).not.toContainText('five');
346353
});

0 commit comments

Comments
 (0)