Repository navigation
Text selection API conformance - #19544
Conversation
|
Heads up! This PR modifies the following files:
|
2275fb3 to
945abf9
Compare
KiChjang
left a comment
There was a problem hiding this comment.
Not a full review, just commenting on things that jumped out at me when I skimmed through the changes.
| true | ||
| } | ||
|
|
||
| _ => false |
There was a problem hiding this comment.
Can we use exhaustive matching here? It is useful when a new enum variant is added to input type, and we need to look for places where the InputType enum is referenced.
|
|
||
| // https://html.spec.whatwg.org/multipage/#dom-textarea/input-select | ||
| fn Select(&self) { | ||
| self.dom_select(); |
There was a problem hiding this comment.
I'm quite surprised by this change when I initially looked at it, since nowhere in this file is dom_select being defined.
That goes back to my comments about making the TextControl trait -- I think that TextInput can instead hold all the text selection API methods, without going through the trait indirection.
There was a problem hiding this comment.
Thanks for starting to review this.
I can see where you are coming from, but personally I think that to try to put this code in TextInput would muddy the concerns of TextInput. At the moment it doesn't have any DOM node-related code - it is basically a wrapper around a string, providing some extra state about the selection and so on. This makes it easy to unit test and keeps its implementation focused. To put the selection API code inside TextInput would require passing in DOM nodes somehow.
As I say, I agree that there is some indirection here, I guess I just don't think it's that big a deal (easy to find the dom_select method using grep, ctags, etc...) It's also just how traits work - when you implement e.g. the Iterator trait, you get a whole load of extra methods which aren't defined in the same file, and that's the point of it.
There is potentially another option, though, which is to have a TextControl object rather than trait (maybe the name could be improved too). So HTMLInputElement and HTMLTextAreaElement would each do something like (in their constructor):
self.text_control = TextControl::new(&self.upcast::<Node>(), self.textinput)(Actually, we'd probably need to pass in more things than this... but you get the idea.)
And then this method would become:
fn Select(&self) {
self.text_control.dom_select();
}Either way, since I'm not changing the existing architecture in this PR, could we perhaps move this discussion elsewhere, to an issue or something?
There was a problem hiding this comment.
At the very least, we'll need to make the code look less magical. I don't agree that "the ease of grepping for dom_select" is a good reason to not surprise future maintainers of the code, since even if I do find the definition in text_control.rs, it's not exactly apparent that both HTMLInputElement and HTMLTextAreaElement implement the TextControl trait, until you search both htmlinputelement.rs and htmltextareaelement.rs for its implementation.
Encapsulating a TextInput inside of a TextControl struct sounds fine to me, however. In other words, instead of having HTMLInputElement or HTMLTextAreaElement both contain a TextInput field, have a TextControl as a member field, inside of which contains a TextInput.
There was a problem hiding this comment.
Respectfully, I'm feeling quite frustrated that you're asking me to change this as a precondition of getting my code merged.
I had thought that the architectural question we're talking about had already been decided in #19272 (where you chose to defer the review to @jdm). I've now submitted this quite substantial PR and the only real feedback I've received after waiting several days is to ask me to make an architectural change which is unrelated to the actual PR, other than that it relates to the same area of the code.
I can understand your perspective, and I place a lot of value on easy to follow code too. I don't think either of us is "right", we just have a difference of opinion about the trade-offs of each choice. I'm willing to revisit the code that you're concerned about, but I'd prefer to do so in a separate PR, since this PR doesn't change the existing architecture. (And I think we should also involve @jdm in any such change since he reviewed #19272.)
There was a problem hiding this comment.
I'm sorry if my intentions were not clear! I did not mean that you need to make architectural changes in my previous comment. My simple wish is to keep the implementation details of dom_select within htmlinputelement.rs and htmltextareaelement.rs. A workable solution here is to not have a default implementation on the trait, and have it implemented on both htmlinputelement.rs and htmltextareaelement.rs.
Also, the architectural design is not already decided (refer to @jdm's comment here). We have to keep in mind that the spec can change at any time, and reducing the burden for future maintainers when reading the code and making changes to it (keeping in mind that they may not be Rust experts!) would make their lives easier. However, for the purpose of this PR, I concede that this is out of scope.
I really appreciate the work that you've done so far, and I understand it could be frustrating when a code review requires you to make huge architectural changes. In the future, may I invite you to #servo at irc.mozilla.org to discuss implementation plans, before any huge changes are made?
There was a problem hiding this comment.
Another idea would be to just leave a comment here explaining that this is implemented by default on the TextControl trait.
There was a problem hiding this comment.
A workable solution here is to not have a default implementation on the trait, and have it implemented on both htmlinputelement.rs and htmltextareaelement.rs.
Not sure if I've misunderstood, but if you mean to duplicate the ~110 lines of code which implement dom_select() and set_dom_range_text() in TextControl, this seems like much more of a maintainability problem to me than a bit of indirection.
I'm fine with your other suggestion of adding a comment, though, so I've done that. I'm also willing to revisit the architecture of TextControl separately, as I already said.
In the future, may I invite you to #servo at irc.mozilla.org to discuss implementation plans, before any huge changes are made?
I've been in the IRC room a bit, but I don't really understand what you'd prefer I'd done differently here? Do you mean that you'd prefer I'd discussed the #19272 changes in IRC before submitting a PR?
945abf9 to
d94e582
Compare
|
@KiChjang I believe I've addressed everything you've currently commented on, would you mind having another look at this please? (Or let me know if I should ask somebody else?) |
| }, | ||
|
|
||
| SelectionMode::Preserve => { | ||
| // Step 1 |
There was a problem hiding this comment.
How would you recommend labelling this? "Substep 1"? "Step 13 -> preserve -> 1"?
There was a problem hiding this comment.
I believe there are two ways to go about this, we can do Substep 1 or Step 13.1.
There was a problem hiding this comment.
I'll go with the former; the latter seems ambiguous if other branches also had sub-steps.
| fn textinput(&self) -> &DomRefCell<TextInput<ScriptToConstellationChan>>; | ||
| fn selection_api_applies(&self) -> bool; | ||
| fn has_selectable_text(&self) -> bool; | ||
| fn dirty_value_flag(&self) -> &Cell<bool>; |
There was a problem hiding this comment.
Returning a &Cell here looks like an implementation detail, rather than a generalized API. I would expect this to return a bool, and have another method that sets the dirty value flag.
mbrubeck
left a comment
There was a problem hiding this comment.
r+ for the second commit. Looks great!
d94e582 to
b76ec35
Compare
|
Great job with the last 2 commits! I think that can be merged as is. @bors-servo r+ |
|
📌 Commit b76ec35 has been approved by |
Per the spec, selectionStart and selectionEnd should return the same values regardless of the selectionDirection. (That is, selectionStart is always less than or equal to selectionEnd; the direction then implies which of selectionStart or selectionEnd is the cursor position.) There was no explicit WPT test for this, so I added one. This bug was initially quite hard to wrap my head around, and I think part of the problem is the code in TextInput. Therefore, in the process of fixing it I have refactored the implementation of TextInput: * Rename selection_begin to selection_origin. This value doesn't necessarily correspond directly to the selectionStart DOM value - in the case of a backward selection, it corresponds to selectionEnd. I feel that "origin" doesn't imply a specific ordering as strongly as "begin" (or "start" for that matter) does. * In various other cases where "begin" is used as a synonym for "start", just use "start" for consistency. * Implement selection_start() and selection_end() methods (and their _offset() variants) which directly correspond to their DOM equivalents. * Rename other related methods to make them less wordy and more consistent / intention-revealing. * Add assertions to assert_ok_selection() to ensure that our assumptions about the ordering of selection_origin and edit_point are met. This then revealed a bug in adjust_selection_for_horizontal_change() where the value of selection_direction was not maintained correctly (causing a unit test failure when the new assertion failed).
The implementation of adjust_horizontal_to_limit() is written with UI in mind. As such, when there's a selection and we "adjust horizontal", the selection will be cleared and the cursor will and up at the start/end of the previous selection. This is what happens when you have a selection and you press an arrow key on your keyboard, but it isn't the behaviour we want when programmatically changing the value. Instead, we need to first clear the selection, and then move the cursor to the end. (We also need to reset the selection direction when clearing the selection.)
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.)
Spec: https://html.spec.whatwg.org/multipage/input.html#input-type-change In short, this resets the selection to the start of the field when the type has changed from one which doesn't support the selection API to one that does. I couldn't see an existing WPT test covering this.
b76ec35 to
a8b64ac
Compare
|
@nox should be good to go now. (FWIW I had to add |
|
@bors-servo r+ Thanks again! |
|
📌 Commit a8b64ac has been approved by |
Text selection API conformance This is my next batch of changes for issue #19171. All the tests in tests/wpt/metadata/html/semantics/forms/textfieldselection/ are now passing (and also some tests outside of there). I've made detailed notes about the changes in each commit message. r? @KiChjang <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19544) <!-- Reviewable:end -->
|
☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css1, mac-rel-css2, mac-rel-wpt1, mac-rel-wpt2, mac-rel-wpt3, mac-rel-wpt4, windows-msvc-dev |
The intention here is to make the flow more explicit. I.e. rather than calling `self.dom_select()` and relying on the programmer to know/realise that this method is provided by a trait, we call `self.selection().dom_select()` and the programmer can inspect the definition of `self.selection()` to follow the code. This came out of a discussion with KiChjang here: servo#19544 (comment) Note that I tried to make "selection" be a member field of HTML{Input,TextArea}Element but it opened up a whole can of worms with lifetimes, so it seemed simpler to not do that since it is not essential for this code to work.
The intention here is to make the flow more explicit. I.e. rather than calling `self.dom_select()` and relying on the programmer to know/realise that this method is provided by a trait, we call `self.selection().dom_select()` and the programmer can inspect the definition of `self.selection()` to follow the code. This came out of a discussion with KiChjang here: servo#19544 (comment) Note that I tried to make "selection" be a member field of HTML{Input,TextArea}Element but it opened up a whole can of worms with lifetimes, so it seemed simpler to not do that since it is not essential for this code to work.
Per the spec, selectionStart and selectionEnd should return the same values regardless of the selectionDirection. (That is, selectionStart is always less than or equal to selectionEnd; the direction then implies which of selectionStart or selectionEnd is the cursor position.) There was no explicit WPT test for this, so I added one. This bug was initially quite hard to wrap my head around, and I think part of the problem is the code in TextInput. Therefore, in the process of fixing it I have refactored the implementation of TextInput: * Rename selection_begin to selection_origin. This value doesn't necessarily correspond directly to the selectionStart DOM value - in the case of a backward selection, it corresponds to selectionEnd. I feel that "origin" doesn't imply a specific ordering as strongly as "begin" (or "start" for that matter) does. * In various other cases where "begin" is used as a synonym for "start", just use "start" for consistency. * Implement selection_start() and selection_end() methods (and their _offset() variants) which directly correspond to their DOM equivalents. * Rename other related methods to make them less wordy and more consistent / intention-revealing. * Add assertions to assert_ok_selection() to ensure that our assumptions about the ordering of selection_origin and edit_point are met. This then revealed a bug in adjust_selection_for_horizontal_change() where the value of selection_direction was not maintained correctly (causing a unit test failure when the new assertion failed). Upstreamed from servo/servo#19544 [ci skip]
Spec: https://html.spec.whatwg.org/multipage/input.html#input-type-change In short, this resets the selection to the start of the field when the type has changed from one which doesn't support the selection API to one that does. I couldn't see an existing WPT test covering this. Upstreamed from servo/servo#19544 [ci skip]
Refactor implementation of TextControl The intention here is to make the flow more explicit. I.e. rather than calling `self.dom_select()` and relying on the programmer to know/realise that this method is provided by a trait, we call `self.selection().dom_select()` and the programmer can inspect the definition of `self.selection()` to follow the code. This came out of a discussion with KiChjang here: #19544 (comment) Note that I tried to make "selection" be a member field of HTML{Input,TextArea}Element but it opened up a whole can of worms with lifetimes, so it seemed simpler to not do that since it is not essential for this code to work. r? @jdm and @KiChjang <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19909) <!-- Reviewable:end -->
…nleighton:refactor-textcontrol); r=jdm The intention here is to make the flow more explicit. I.e. rather than calling `self.dom_select()` and relying on the programmer to know/realise that this method is provided by a trait, we call `self.selection().dom_select()` and the programmer can inspect the definition of `self.selection()` to follow the code. This came out of a discussion with KiChjang here: servo/servo#19544 (comment) Note that I tried to make "selection" be a member field of HTML{Input,TextArea}Element but it opened up a whole can of worms with lifetimes, so it seemed simpler to not do that since it is not essential for this code to work. r? @jdm and @KiChjang Source-Repo: https://github.com/servo/servo Source-Revision: ff54a58694af43fc549baa6c64bf6ebc5c37385f --HG-- extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear extra : subtree_revision : 308bb6755c90c07004c00e5cefacfaa4fa1f6ade
…nleighton:refactor-textcontrol); r=jdm The intention here is to make the flow more explicit. I.e. rather than calling `self.dom_select()` and relying on the programmer to know/realise that this method is provided by a trait, we call `self.selection().dom_select()` and the programmer can inspect the definition of `self.selection()` to follow the code. This came out of a discussion with KiChjang here: servo/servo#19544 (comment) Note that I tried to make "selection" be a member field of HTML{Input,TextArea}Element but it opened up a whole can of worms with lifetimes, so it seemed simpler to not do that since it is not essential for this code to work. r? jdm and KiChjang Source-Repo: https://github.com/servo/servo Source-Revision: ff54a58694af43fc549baa6c64bf6ebc5c37385f UltraBlame original commit: bc60fea496d5e9f1eb059881b14e197dabc7bc59
…nleighton:refactor-textcontrol); r=jdm The intention here is to make the flow more explicit. I.e. rather than calling `self.dom_select()` and relying on the programmer to know/realise that this method is provided by a trait, we call `self.selection().dom_select()` and the programmer can inspect the definition of `self.selection()` to follow the code. This came out of a discussion with KiChjang here: servo/servo#19544 (comment) Note that I tried to make "selection" be a member field of HTML{Input,TextArea}Element but it opened up a whole can of worms with lifetimes, so it seemed simpler to not do that since it is not essential for this code to work. r? jdm and KiChjang Source-Repo: https://github.com/servo/servo Source-Revision: ff54a58694af43fc549baa6c64bf6ebc5c37385f UltraBlame original commit: bc60fea496d5e9f1eb059881b14e197dabc7bc59
…nleighton:refactor-textcontrol); r=jdm The intention here is to make the flow more explicit. I.e. rather than calling `self.dom_select()` and relying on the programmer to know/realise that this method is provided by a trait, we call `self.selection().dom_select()` and the programmer can inspect the definition of `self.selection()` to follow the code. This came out of a discussion with KiChjang here: servo/servo#19544 (comment) Note that I tried to make "selection" be a member field of HTML{Input,TextArea}Element but it opened up a whole can of worms with lifetimes, so it seemed simpler to not do that since it is not essential for this code to work. r? jdm and KiChjang Source-Repo: https://github.com/servo/servo Source-Revision: ff54a58694af43fc549baa6c64bf6ebc5c37385f UltraBlame original commit: bc60fea496d5e9f1eb059881b14e197dabc7bc59
This is my next batch of changes for issue #19171. All the tests in tests/wpt/metadata/html/semantics/forms/textfieldselection/ are now passing (and also some tests outside of there).
I've made detailed notes about the changes in each commit message.
r? @KiChjang
This change is