Skip to content

Commit 592e7af

Browse files
authored
Merge pull request #5782 from ggorlen/improve-option-name-comparison
Use textContent to avoid HTML special characters in options
2 parents e03e4d3 + 028a502 commit 592e7af

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/dom/dom.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ p5.prototype.createSelect = function() {
705705
}
706706
//see if there is already an option with this name
707707
for (let i = 0; i < this.elt.length; i += 1) {
708-
if (this.elt[i].innerHTML === name) {
708+
if (this.elt[i].textContent === name) {
709709
index = i;
710710
break;
711711
}
@@ -722,7 +722,7 @@ p5.prototype.createSelect = function() {
722722
} else {
723723
//if it doesn't exist create it
724724
const opt = document.createElement('option');
725-
opt.innerHTML = name;
725+
opt.textContent = name;
726726
opt.value = value === undefined ? name : value;
727727
this.elt.appendChild(opt);
728728
this._pInst._elements.push(opt);

test/unit/dom/dom.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,15 @@ suite('DOM', function() {
807807
}
808808
});
809809

810+
test('should update select value when HTML special characters are in the name', function() {
811+
testElement = myp5.createSelect(true);
812+
testElement.option('&', 'foo');
813+
assert.equal(testElement.elt.options.length, 1);
814+
assert.equal(testElement.elt.options[0].value, 'foo');
815+
testElement.option('&', 'bar');
816+
assert.equal(testElement.elt.options[0].value, 'bar');
817+
});
818+
810819
test('calling selected(value) should updated selectedIndex', function() {
811820
testElement = myp5.createSelect(true);
812821
options = ['Sunday', 'Monday', 'Tuesday', 'Friday'];

0 commit comments

Comments
 (0)