Skip to content

Commit

Permalink
Fix clearing the selection when value is changed
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
jonleighton committed Jan 26, 2018
1 parent 02883a6 commit 648bfbe
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 25 deletions.
5 changes: 2 additions & 3 deletions components/script/dom/htmlinputelement.rs
Expand Up @@ -52,7 +52,7 @@ use std::ops::Range;
use style::attr::AttrValue;
use style::element_state::ElementState;
use style::str::split_commas;
use textinput::{Direction, Selection, SelectionDirection, TextInput};
use textinput::{SelectionDirection, TextInput};
use textinput::KeyReaction::{DispatchInput, Nothing, RedrawSelection, TriggerDefaultAction};
use textinput::Lines::Single;

Expand Down Expand Up @@ -563,8 +563,7 @@ impl HTMLInputElementMethods for HTMLInputElement {
self.sanitize_value();
// Step 5.
if *self.textinput.borrow().single_line_content() != old_value {
self.textinput.borrow_mut()
.adjust_horizontal_to_limit(Direction::Forward, Selection::NotSelected);
self.textinput.borrow_mut().clear_selection_to_limit();
}
}
ValueMode::Default |
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/htmltextareaelement.rs
Expand Up @@ -35,7 +35,7 @@ use std::default::Default;
use std::ops::Range;
use style::attr::AttrValue;
use style::element_state::ElementState;
use textinput::{Direction, KeyReaction, Lines, Selection, SelectionDirection, TextInput};
use textinput::{KeyReaction, Lines, SelectionDirection, TextInput};

#[dom_struct]
pub struct HTMLTextAreaElement {
Expand Down Expand Up @@ -257,7 +257,7 @@ impl HTMLTextAreaElementMethods for HTMLTextAreaElement {

if old_value != textinput.get_content() {
// Step 4
textinput.adjust_horizontal_to_limit(Direction::Forward, Selection::NotSelected);
textinput.clear_selection_to_limit();
} else {
textinput.selection_origin = old_selection;
}
Expand Down
7 changes: 7 additions & 0 deletions components/script/textinput.rs
Expand Up @@ -513,6 +513,13 @@ impl<T: ClipboardProvider> TextInput<T> {
/// Remove the current selection.
pub fn clear_selection(&mut self) {
self.selection_origin = None;
self.selection_direction = SelectionDirection::None;
}

/// Remove the current selection and set the edit point to the end of the content.
pub fn clear_selection_to_limit(&mut self) {
self.clear_selection();
self.adjust_horizontal_to_limit(Direction::Forward, Selection::NotSelected);
}

pub fn adjust_horizontal_by_word(&mut self, direction: Direction, select: Selection) {
Expand Down

This file was deleted.

0 comments on commit 648bfbe

Please sign in to comment.