Implement setRangeText API

Spec: https://html.spec.whatwg.org/multipage/#dom-textarea/input-setrangetext

In order to do this, we need to define the SelectionMode enum in WebIDL:
https://html.spec.whatwg.org/multipage/#selectionmode

Since the enum is used by HTMLTextAreaElement and HTMLInputElement, it
doesn't seem to make sense to define it in the WebIDL file for one or
other of those.

However, we also can't create a stand-alone SelectionMode.webidl file,
because the current binding-generation code won't generate a "pub mod
SelectionMode;" line in mod.rs unless SelectionMode.webidl contains
either an interface or a namespace. (This logic happens in
components/script/dom/bindings/codegen/Configuration.py:35, in the
Configuration.__init__ method.)

I thought about changing the binding-generation code, but that seems
difficult. So I settled for placing the enum inside
HTMLFormElement.webidl, as that seems like a "neutral" location. We
could equally settle for putting it under HTMLTextAreaElement or
HTMLInputElement, it probably doesn't really matter.

The setRangeText algorithm set the "dirty value flag" on the
input/textarea. I made some clean-ups related to this:

1. HTMLTextAreaElement called its dirty value flag "value_changed"; I
   changed this to "value_dirty" to be consistent with the spec.

2. HTMLInputElement had a "value_changed" field and also a "value_dirty"
   field, which were each used in slightly different places (and
   sometimes in both places). I consolidated these into a single
   "value_dirty" field, which was necessary in order to make some of the
   tests pass.

TextControl::set_dom_range_text replaces part of the existing textinput
content with the replacement string (steps 9-10 of the algorithm). My
implementation changes the textinput's selection and then replaces the
selection. A downside of this approach is that we lose the original
selection state from before the call to setRangeText. Therefore, we have
to save the state into the original_selection_state variable so that we
can later pass it into TextControl::set_selection_range. This allows
TextControl::set_selection_range to correctly decide whether or not to
fire the select event.

An alternative approach would be to implement a method on TextInput
which allows a subtring of the content to be mutated, without touching
the current selection state. However, any such method would potentially
put the TextInput into an inconsistent state where the edit_point and/or
selection_origin is a TextPoint which doesn't exist in the content. It
would be up to the caller to subsequently make sure that the TextInput
gets put back into a valid state (which would actually happen, when
TextControl::set_selection_range is called).

I think TextInput's public API should not make it possible to put it
into an invalid state, as that would be a potential source of bugs.
That's why I didn't take this approach. (TextInput's public API does
currently make it possible to create an invalid state, but I'd like to
submit a follow-up patch to lock this down.)
This commit is contained in:
Jon Leighton 2017-12-11 15:57:18 +01:00
parent e34f7c58c9
commit ce7bae8834
13 changed files with 198 additions and 740 deletions

View file

@ -1,38 +0,0 @@
[select-event.html]
type: testharness
[textarea: setRangeText()]
expected: FAIL
[textarea: setRangeText() a second time (must not fire select)]
expected: FAIL
[input type text: setRangeText()]
expected: FAIL
[input type text: setRangeText() a second time (must not fire select)]
expected: FAIL
[input type search: setRangeText()]
expected: FAIL
[input type search: setRangeText() a second time (must not fire select)]
expected: FAIL
[input type tel: setRangeText()]
expected: FAIL
[input type tel: setRangeText() a second time (must not fire select)]
expected: FAIL
[input type url: setRangeText()]
expected: FAIL
[input type url: setRangeText() a second time (must not fire select)]
expected: FAIL
[input type password: setRangeText()]
expected: FAIL
[input type password: setRangeText() a second time (must not fire select)]
expected: FAIL

View file

@ -1,74 +0,0 @@
[selection-not-application.html]
type: testharness
[setRangeText on an input[type=hidden\] throws InvalidStateError]
expected: FAIL
[setRangeText on an input[type=email\] throws InvalidStateError]
expected: FAIL
[setRangeText on an input[type=datetime-local\] throws InvalidStateError]
expected: FAIL
[setRangeText on an input[type=date\] throws InvalidStateError]
expected: FAIL
[setRangeText on an input[type=month\] throws InvalidStateError]
expected: FAIL
[setRangeText on an input[type=week\] throws InvalidStateError]
expected: FAIL
[setRangeText on an input[type=time\] throws InvalidStateError]
expected: FAIL
[setRangeText on an input[type=number\] throws InvalidStateError]
expected: FAIL
[setRangeText on an input[type=range\] throws InvalidStateError]
expected: FAIL
[setRangeText on an input[type=color\] throws InvalidStateError]
expected: FAIL
[setRangeText on an input[type=checkbox\] throws InvalidStateError]
expected: FAIL
[setRangeText on an input[type=radio\] throws InvalidStateError]
expected: FAIL
[setRangeText on an input[type=file\] throws InvalidStateError]
expected: FAIL
[setRangeText on an input[type=submit\] throws InvalidStateError]
expected: FAIL
[setRangeText on an input[type=image\] throws InvalidStateError]
expected: FAIL
[setRangeText on an input[type=reset\] throws InvalidStateError]
expected: FAIL
[setRangeText on an input[type=button\] throws InvalidStateError]
expected: FAIL
[setRangeText on an input[type=text\] doesn't throw an exception]
expected: FAIL
[setRangeText on an input[type=search\] doesn't throw an exception]
expected: FAIL
[setRangeText on an input[type=tel\] doesn't throw an exception]
expected: FAIL
[setRangeText on an input[type=url\] doesn't throw an exception]
expected: FAIL
[setRangeText on an input[type=password\] doesn't throw an exception]
expected: FAIL
[setRangeText on an input[type=null\] doesn't throw an exception]
expected: FAIL
[setRangeText on an input[type=aninvalidtype\] doesn't throw an exception]
expected: FAIL

View file

@ -1,35 +0,0 @@
[selection-value-interactions.html]
type: testharness
[value dirty flag behavior after setRangeText on textarea not in body]
expected: FAIL
[value dirty flag behavior after setRangeText on input not in body]
expected: FAIL
[value dirty flag behavior after setRangeText on textarea in body]
expected: FAIL
[value dirty flag behavior after setRangeText on input in body]
expected: FAIL
[value dirty flag behavior after setRangeText on textarea in body with parsed default value]
expected: FAIL
[value dirty flag behavior after setRangeText on input in body with parsed default value]
expected: FAIL
[value dirty flag behavior after setRangeText on focused textarea]
expected: FAIL
[value dirty flag behavior after setRangeText on focused input]
expected: FAIL
[value dirty flag behavior after setRangeText on focused then blurred textarea]
expected: FAIL
[value dirty flag behavior after setRangeText on focused then blurred input]
expected: FAIL
[selection is always collapsed to the end after setting values on textarea]
expected: FAIL

View file

@ -1,194 +0,0 @@
[textfieldselection-setRangeText.html]
type: testharness
[text setRangeText fires a select event]
expected: FAIL
[text setRangeText with only one argument replaces the value between selectionStart and selectionEnd, otherwise replaces the value between 2nd and 3rd arguments]
expected: FAIL
[text selectionMode missing]
expected: FAIL
[text selectionMode 'select']
expected: FAIL
[text selectionMode 'start']
expected: FAIL
[text selectionMode 'end']
expected: FAIL
[text selectionMode 'preserve']
expected: FAIL
[text setRangeText with 3rd argument greater than 2nd argument throws an IndexSizeError exception]
expected: FAIL
[search setRangeText with only one argument replaces the value between selectionStart and selectionEnd, otherwise replaces the value between 2nd and 3rd arguments]
expected: FAIL
[search selectionMode missing]
expected: FAIL
[search selectionMode 'select']
expected: FAIL
[search selectionMode 'start']
expected: FAIL
[search selectionMode 'end']
expected: FAIL
[search selectionMode 'preserve']
expected: FAIL
[search setRangeText with 3rd argument greater than 2nd argument throws an IndexSizeError exception]
expected: FAIL
[search setRangeText fires a select event]
expected: FAIL
[tel setRangeText with only one argument replaces the value between selectionStart and selectionEnd, otherwise replaces the value between 2nd and 3rd arguments]
expected: FAIL
[tel selectionMode missing]
expected: FAIL
[tel selectionMode 'select']
expected: FAIL
[tel selectionMode 'start']
expected: FAIL
[tel selectionMode 'end']
expected: FAIL
[tel selectionMode 'preserve']
expected: FAIL
[tel setRangeText with 3rd argument greater than 2nd argument throws an IndexSizeError exception]
expected: FAIL
[tel setRangeText fires a select event]
expected: FAIL
[url setRangeText with only one argument replaces the value between selectionStart and selectionEnd, otherwise replaces the value between 2nd and 3rd arguments]
expected: FAIL
[url selectionMode missing]
expected: FAIL
[url selectionMode 'select']
expected: FAIL
[url selectionMode 'start']
expected: FAIL
[url selectionMode 'end']
expected: FAIL
[url selectionMode 'preserve']
expected: FAIL
[url setRangeText with 3rd argument greater than 2nd argument throws an IndexSizeError exception]
expected: FAIL
[url setRangeText fires a select event]
expected: FAIL
[password setRangeText with only one argument replaces the value between selectionStart and selectionEnd, otherwise replaces the value between 2nd and 3rd arguments]
expected: FAIL
[password selectionMode missing]
expected: FAIL
[password selectionMode 'select']
expected: FAIL
[password selectionMode 'start']
expected: FAIL
[password selectionMode 'end']
expected: FAIL
[password selectionMode 'preserve']
expected: FAIL
[password setRangeText with 3rd argument greater than 2nd argument throws an IndexSizeError exception]
expected: FAIL
[password setRangeText fires a select event]
expected: FAIL
[display_none setRangeText with only one argument replaces the value between selectionStart and selectionEnd, otherwise replaces the value between 2nd and 3rd arguments]
expected: FAIL
[display_none selectionMode missing]
expected: FAIL
[display_none selectionMode 'select']
expected: FAIL
[display_none selectionMode 'start']
expected: FAIL
[display_none selectionMode 'end']
expected: FAIL
[display_none selectionMode 'preserve']
expected: FAIL
[display_none setRangeText with 3rd argument greater than 2nd argument throws an IndexSizeError exception]
expected: FAIL
[display_none setRangeText fires a select event]
expected: FAIL
[textarea setRangeText with only one argument replaces the value between selectionStart and selectionEnd, otherwise replaces the value between 2nd and 3rd arguments]
expected: FAIL
[textarea selectionMode missing]
expected: FAIL
[textarea selectionMode 'select']
expected: FAIL
[textarea selectionMode 'start']
expected: FAIL
[textarea selectionMode 'end']
expected: FAIL
[textarea selectionMode 'preserve']
expected: FAIL
[textarea setRangeText with 3rd argument greater than 2nd argument throws an IndexSizeError exception]
expected: FAIL
[textarea setRangeText fires a select event]
expected: FAIL
[input_not_in_doc setRangeText with only one argument replaces the value between selectionStart and selectionEnd, otherwise replaces the value between 2nd and 3rd arguments]
expected: FAIL
[input_not_in_doc selectionMode missing]
expected: FAIL
[input_not_in_doc selectionMode 'select']
expected: FAIL
[input_not_in_doc selectionMode 'start']
expected: FAIL
[input_not_in_doc selectionMode 'end']
expected: FAIL
[input_not_in_doc selectionMode 'preserve']
expected: FAIL
[input_not_in_doc setRangeText with 3rd argument greater than 2nd argument throws an IndexSizeError exception]
expected: FAIL
[input_not_in_doc setRangeText fires a select event]
expected: FAIL