Skip to content

Truly independent web browser

LibWeb: Replace the selection in setRangeText() with one argument

Andreas Kling05bc704ce7ba
diff --git a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp b/Libraries/LibWeb/HTML/FormAssociatedElement.cpp
index fd8c84c9385..722119b956e 100644
--- a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp
+++ b/Libraries/LibWeb/HTML/FormAssociatedElement.cpp
@@ -1045,12 +1045,15 @@ WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::set_selection_direct
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setrangetext
WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::set_range_text_binding(Utf16View replacement)
{
- return set_range_text(replacement, m_selection_start, m_selection_end);
+ return set_range_text(replacement, selection_start(), selection_end());
}
+// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setrangetext
WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::set_range_text(Utf16String const& replacement)
{
- return set_range_text(replacement.utf16_view(), 0, replacement.length_in_code_units());
+ // 3. If the method has only one argument, then let start and end have the values of the selectionStart attribute
+ // and the selectionEnd attribute respectively.
+ return set_range_text(replacement.utf16_view(), selection_start(), selection_end());
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setrangetext
@@ -1091,10 +1094,10 @@ WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::set_range_text(Utf16
end = relevant_value_length;
// 7. Let selection start be the current value of the selectionStart attribute.
- auto selection_start = m_selection_start;
+ auto selection_start = this->selection_start();
// 8. Let selection end be the current value of the selectionEnd attribute.
- auto selection_end = m_selection_end;
+ auto selection_end = this->selection_end();
// 9. If start is less than end, delete the sequence of code units within the element's relevant value starting with
// the code unit at the startth position and ending with the code unit at the (end-1)th position.
diff --git a/Tests/LibWeb/Text/expected/HTML/setRangeText-backward-user-selection.txt b/Tests/LibWeb/Text/expected/HTML/setRangeText-backward-user-selection.txt
new file mode 100644
index 00000000000..35acb977d1b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/setRangeText-backward-user-selection.txt
@@ -0,0 +1,4 @@
+input before: 2-4 backward
+input after: value=abXef 2-3
+textarea before: 2-4 backward
+textarea after: value=abXef 2-3
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/textfieldselection/textfieldselection-setRangeText.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/textfieldselection/textfieldselection-setRangeText.txt
index 685feaf1326..94040781d3d 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/textfieldselection/textfieldselection-setRangeText.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/textfieldselection/textfieldselection-setRangeText.txt
@@ -2,8 +2,7 @@ Harness status: OK
Found 88 tests
-87 Pass
-1 Fail
+88 Pass
Pass text setRangeText with only one argument replaces the value between selectionStart and selectionEnd, otherwise replaces the value between 2nd and 3rd arguments
Pass text selectionMode missing
Pass text selectionMode 'select'
@@ -70,7 +69,7 @@ Pass display_none setRangeText without argument throws a type error
Pass display_none setRangeText fires a select event
Pass display_none setRangeText fires a select event when fully selected
Pass display_none setRangeText fires a select event after select()
-Fail textarea setRangeText with only one argument replaces the value between selectionStart and selectionEnd, otherwise replaces the value between 2nd and 3rd arguments
+Pass textarea setRangeText with only one argument replaces the value between selectionStart and selectionEnd, otherwise replaces the value between 2nd and 3rd arguments
Pass textarea selectionMode missing
Pass textarea selectionMode 'select'
Pass textarea selectionMode 'start'
diff --git a/Tests/LibWeb/Text/input/HTML/setRangeText-backward-user-selection.html b/Tests/LibWeb/Text/input/HTML/setRangeText-backward-user-selection.html
new file mode 100644
index 00000000000..92b64bcc474
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/setRangeText-backward-user-selection.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<input id="input" value="abcdef">
+<textarea id="textarea">abcdef</textarea>
+<script>
+ const modShift = 1 << 2;
+
+ test(() => {
+ for (const id of ["input", "textarea"]) {
+ const element = document.getElementById(id);
+ element.focus();
+ element.setSelectionRange(4, 4);
+ internals.sendKey(element, "Left", modShift);
+ internals.sendKey(element, "Left", modShift);
+ println(`${id} before: ${element.selectionStart}-${element.selectionEnd} ${element.selectionDirection}`);
+ try {
+ element.setRangeText("X");
+ println(`${id} after: value=${element.value} ${element.selectionStart}-${element.selectionEnd}`);
+ } catch (e) {
+ println(`${id} threw ${e.name}`);
+ }
+ }
+ });
+</script>