Skip to content

Commit 0da15aa

Browse files
committed
Fixed option text encoding
This fixes an issue when using a `<select>` where the elements were created with XHTML-encoded characters to prevent any injection, as they would be double-encoded and display incorrectly. When using a `<select>`, we can assume that the data has already been encoded because any XSS will have already run before we get to it. Because of this, we can just use `.text()` instead of `.html()` to avoid any issues. This also includes a test to ensure that this does not become an issue in the future. This closes select2#3115.
1 parent b917754 commit 0da15aa

8 files changed

Lines changed: 20 additions & 7 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2563,7 +2563,7 @@ define('select2/data/select',[
25632563
if ($option.is('option')) {
25642564
data = {
25652565
id: $option.val(),
2566-
text: $option.html(),
2566+
text: $option.text(),
25672567
disabled: $option.prop('disabled'),
25682568
selected: $option.prop('selected'),
25692569
title: $option.prop('title')

dist/js/select2.amd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2563,7 +2563,7 @@ define('select2/data/select',[
25632563
if ($option.is('option')) {
25642564
data = {
25652565
id: $option.val(),
2566-
text: $option.html(),
2566+
text: $option.text(),
25672567
disabled: $option.prop('disabled'),
25682568
selected: $option.prop('selected'),
25692569
title: $option.prop('title')

dist/js/select2.full.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3002,7 +3002,7 @@ define('select2/data/select',[
30023002
if ($option.is('option')) {
30033003
data = {
30043004
id: $option.val(),
3005-
text: $option.html(),
3005+
text: $option.text(),
30063006
disabled: $option.prop('disabled'),
30073007
selected: $option.prop('selected'),
30083008
title: $option.prop('title')

dist/js/select2.full.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3002,7 +3002,7 @@ define('select2/data/select',[
30023002
if ($option.is('option')) {
30033003
data = {
30043004
id: $option.val(),
3005-
text: $option.html(),
3005+
text: $option.text(),
30063006
disabled: $option.prop('disabled'),
30073007
selected: $option.prop('selected'),
30083008
title: $option.prop('title')

dist/js/select2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/select2/data/select.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ define([
205205
if ($option.is('option')) {
206206
data = {
207207
id: $option.val(),
208-
text: $option.html(),
208+
text: $option.text(),
209209
disabled: $option.prop('disabled'),
210210
selected: $option.prop('selected'),
211211
title: $option.prop('title')

tests/data/select-tests.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,16 @@ test('multiple options with the same value are returned', function (assert) {
439439
);
440440
});
441441
});
442+
443+
test('data objects use the text of the option', function (assert) {
444+
var $select = $('#qunit-fixture .duplicates');
445+
446+
var data = new SelectData($select, options);
447+
448+
var $option = $('<option>&amp;</option>');
449+
450+
var item = data.item($option);
451+
452+
assert.equal(item.id, '&');
453+
assert.equal(item.text, '&');
454+
});

0 commit comments

Comments
 (0)