Skip to content

Commit

Permalink
Fix <select>'s value getter when selectedIndex is -1
Browse files Browse the repository at this point in the history
In this case, it should return the empty string, instead of the value of the first option.
  • Loading branch information
tgohn authored and domenic committed Aug 4, 2019
1 parent 2bd84ee commit 699ed6b
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lib/jsdom/living/nodes/HTMLSelectElement-impl.js
Expand Up @@ -144,7 +144,13 @@ class HTMLSelectElementImpl extends HTMLElementImpl {

set selectedIndex(index) {
for (let i = 0; i < this.options.length; i++) {
this.options.item(i).selected = i === index;
this.options.item(i)._selectedness = false;
}

const selectedOption = this.options.item(index);
if (selectedOption) {
selectedOption._selectedness = true;
selectedOption._dirtyness = true;
}
}

Expand All @@ -153,14 +159,13 @@ class HTMLSelectElementImpl extends HTMLElementImpl {
}

get value() {
let i = this.selectedIndex;
if (this.options.length && (i === -1)) {
i = 0;
}
if (i === -1) {
return "";
for (const option of this.options) {
if (option._selectedness) {
return option.value;
}
}
return this.options.item(i).value;

return "";
}

set value(val) {
Expand Down

0 comments on commit 699ed6b

Please sign in to comment.