Skip to content

Commit

Permalink
Fix the end index in setRangeText() for input and textarea
Browse files Browse the repository at this point in the history
Also fixes the timing of the select event firing to be from a queued task, instead of synchronous.

Closes #2601.
  • Loading branch information
pmstss committed Apr 16, 2023
1 parent 12a24a9 commit f76d3a2
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/jsdom/living/nodes/HTMLInputElement-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ class HTMLInputElementImpl extends HTMLElementImpl {
}

_dispatchSelectEvent() {
fireAnEvent("select", this, undefined, { bubbles: true, cancelable: true });
setTimeout(() => fireAnEvent("select", this, undefined, { bubbles: true, cancelable: false }), 0);
}

_getValueLength() {
Expand Down Expand Up @@ -694,7 +694,7 @@ class HTMLInputElementImpl extends HTMLElementImpl {

this.value = val.slice(0, start) + repl + val.slice(end);

const newEnd = start + this.value.length;
const newEnd = start + repl.length;

if (selectionMode === "select") {
this.setSelectionRange(start, newEnd);
Expand Down
4 changes: 2 additions & 2 deletions lib/jsdom/living/nodes/HTMLTextAreaElement-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class HTMLTextAreaElementImpl extends HTMLElementImpl {
}

_dispatchSelectEvent() {
fireAnEvent("select", this, undefined, { bubbles: true, cancelable: true });
setTimeout(() => fireAnEvent("select", this, undefined, { bubbles: true, cancelable: false }), 0);
}

_getValueLength() {
Expand Down Expand Up @@ -156,7 +156,7 @@ class HTMLTextAreaElementImpl extends HTMLElementImpl {

this.value = val.slice(0, start) + repl + val.slice(end);

const newEnd = start + this.value.length;
const newEnd = start + repl.length;

if (selectionMode === "select") {
this.setSelectionRange(start, newEnd);
Expand Down
2 changes: 0 additions & 2 deletions test/web-platform-tests/to-run.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -902,8 +902,6 @@ DIR: html/semantics/forms/textfieldselection

select-event.html: [fail, not yet implemented]
selection-start-end-extra.html: [fail, not yet implemented. Shortening value by turning input type into url - fix would cause the-input-element type-change-state.html to fail.]
textfieldselection-setRangeText.html: [fail-slow, not yet implemented]
textfieldselection-setSelectionRange.html: [fail, not yet implemented]

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

assert_equals(element.value, "bazbaz");
assert_equals(element.selectionStart, 0);
assert_equals(element.selectionEnd, 6);
assert_equals(element.selectionEnd, 3);

}, "textarea setRangeText(text, start, end, 'select') should create a new selection using start and end as bounds");

Expand All @@ -60,8 +60,8 @@
element.setRangeText("baz", 0, 6, "end");

assert_equals(element.value, "bazbaz");
assert_equals(element.selectionStart, 6);
assert_equals(element.selectionEnd, 6);
assert_equals(element.selectionStart, 3);
assert_equals(element.selectionEnd, 3);

}, "textarea setRangeText(text, start, end, 'end') should create a new selection, collapsed to end");

Expand Down

0 comments on commit f76d3a2

Please sign in to comment.