Auto merge of #19358 - jonleighton:issue-19171-3, r=KiChjang

Move selection to end when textarea value is assigned

Issue #19171

<!-- 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/19358)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-11-25 16:10:41 -06:00 committed by GitHub
commit 3f0ccd0fef
8 changed files with 59 additions and 72 deletions

View file

@ -34,7 +34,7 @@ use std::default::Default;
use std::ops::Range;
use style::attr::AttrValue;
use style::element_state::ElementState;
use textinput::{KeyReaction, Lines, SelectionDirection, TextInput};
use textinput::{Direction, KeyReaction, Lines, Selection, SelectionDirection, TextInput};
#[dom_struct]
pub struct HTMLTextAreaElement {
@ -232,10 +232,25 @@ impl HTMLTextAreaElementMethods for HTMLTextAreaElement {
// https://html.spec.whatwg.org/multipage/#dom-textarea-value
fn SetValue(&self, value: DOMString) {
// TODO move the cursor to the end of the field
self.textinput.borrow_mut().set_content(value);
let mut textinput = self.textinput.borrow_mut();
// Step 1
let old_value = textinput.get_content();
let old_selection = textinput.selection_begin;
// Step 2
textinput.set_content(value);
// Step 3
self.value_changed.set(true);
if old_value != textinput.get_content() {
// Step 4
textinput.adjust_horizontal_to_limit(Direction::Forward, Selection::NotSelected);
} else {
textinput.selection_begin = old_selection;
}
self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);
}

View file

@ -21,7 +21,16 @@ pub trait TextControl: DerivedFrom<EventTarget> + DerivedFrom<Node> {
// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionstart
fn set_dom_selection_start(&self, start: u32) {
self.set_selection_range(start, self.dom_selection_end(), self.selection_direction());
// Step 2
let mut end = self.dom_selection_end();
// Step 3
if end < start {
end = start;
}
// Step 4
self.set_selection_range(start, end, self.selection_direction());
}
// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionend

View file

@ -770,7 +770,11 @@ impl<T: ClipboardProvider> TextInput<T> {
/// any \n encountered will be stripped and force a new logical line.
pub fn set_content(&mut self, content: DOMString) {
self.lines = if self.multiline {
content.split('\n').map(DOMString::from).collect()
// https://html.spec.whatwg.org/multipage/#textarea-line-break-normalisation-transformation
content.replace("\r\n", "\n")
.split(|c| c == '\n' || c == '\r')
.map(DOMString::from)
.collect()
} else {
vec!(content)
};