diff options
author | Alberto Corona <ac@albertocorona.com> | 2016-03-15 09:03:05 -0500 |
---|---|---|
committer | Adrian Utrilla <adrianutrilla@gmail.com> | 2016-04-17 17:27:26 +0200 |
commit | 5e863f2eb8c261ed05c46310b8f8a125b8ed9f04 (patch) | |
tree | 9da74c7c6e9fd27720668bd96cdbccffa94c5e9d /components/script/dom | |
parent | db4481b4509db4be83e0f77a66884feccd1878e0 (diff) | |
download | servo-5e863f2eb8c261ed05c46310b8f8a125b8ed9f04.tar.gz servo-5e863f2eb8c261ed05c46310b8f8a125b8ed9f04.zip |
Implement HTMLTextArea.setSelectionRange
Diffstat (limited to 'components/script/dom')
-rw-r--r-- | components/script/dom/htmlinputelement.rs | 86 | ||||
-rw-r--r-- | components/script/dom/htmltextareaelement.rs | 47 | ||||
-rw-r--r-- | components/script/dom/webidls/HTMLTextAreaElement.webidl | 8 |
3 files changed, 65 insertions, 76 deletions
diff --git a/components/script/dom/htmlinputelement.rs b/components/script/dom/htmlinputelement.rs index 9c969ceec98..ab753053b8f 100644 --- a/components/script/dom/htmlinputelement.rs +++ b/components/script/dom/htmlinputelement.rs @@ -42,7 +42,7 @@ use string_cache::Atom; use style::element_state::*; use textinput::KeyReaction::{DispatchInput, Nothing, RedrawSelection, TriggerDefaultAction}; use textinput::Lines::Single; -use textinput::TextInput; +use textinput::{TextInput, SelectionDirection}; use util::str::{DOMString, search_index}; const DEFAULT_SUBMIT_VALUE: &'static str = "Submit"; @@ -71,14 +71,6 @@ enum ValueMode { Filename, } -#[derive(JSTraceable, PartialEq, Copy, Clone)] -#[derive(HeapSizeOf)] -enum SelectionDirection { - Forward, - Backward, - None -} - #[dom_struct] pub struct HTMLInputElement { htmlelement: HTMLElement, @@ -94,8 +86,6 @@ pub struct HTMLInputElement { // https://html.spec.whatwg.org/multipage/#concept-input-value-dirty-flag value_dirty: Cell<bool>, - selection_direction: Cell<SelectionDirection>, - // TODO: selected files for file input } @@ -142,10 +132,9 @@ impl HTMLInputElement { value_changed: Cell::new(false), maxlength: Cell::new(DEFAULT_MAX_LENGTH), size: Cell::new(DEFAULT_INPUT_SIZE), - textinput: DOMRefCell::new(TextInput::new(Single, DOMString::new(), chan, None)), + textinput: DOMRefCell::new(TextInput::new(Single, DOMString::new(), chan, None, SelectionDirection::None)), activation_state: DOMRefCell::new(InputActivationState::new()), value_dirty: Cell::new(false), - selection_direction: Cell::new(SelectionDirection::None) } } @@ -177,33 +166,6 @@ impl HTMLInputElement { InputType::InputFile => ValueMode::Filename, } } - - // this method exists so that the functions SetSelectionStart() and SetSelectionEnd() - // don't needlessly allocate strings - fn set_selection_range(&self, start: u32, end: u32, direction: &SelectionDirection) { - let mut text_input = self.textinput.borrow_mut(); - - let mut start = start as usize; - let mut end = end as usize; - - let text_end = text_input.get_content().len(); - if start > text_end { - start = text_end; - } - if end > text_end { - end = text_end; - } - - if start >= end { - start = end; - } - - text_input.selection_begin = Some(text_input.get_text_point_for_absolute_point(start)); - text_input.edit_point = text_input.get_text_point_for_absolute_point(end); - self.selection_direction.set(*direction); - self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage); - } - } pub trait LayoutHTMLInputElementHelpers { @@ -547,60 +509,44 @@ impl HTMLInputElementMethods for HTMLInputElement { // https://html.spec.whatwg.org/multipage/#dom-input-selectionstart fn SelectionStart(&self) -> u32 { - let text_input = self.textinput.borrow(); - let selection_start = match text_input.selection_begin { - Some(selection_begin_point) => { - text_input.get_absolute_point_for_text_point(&selection_begin_point) - }, - None => text_input.get_absolute_insertion_point() - }; - - selection_start as u32 + self.textinput.borrow().get_selection_start() } // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionstart fn SetSelectionStart(&self, start: u32) { - self.set_selection_range(start, self.SelectionEnd(), &self.selection_direction.get()); + let selection_end = self.SelectionEnd(); + self.textinput.borrow_mut().set_selection_range(start, selection_end); + self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage); } // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionend fn SelectionEnd(&self) -> u32 { - let text_input = self.textinput.borrow(); - text_input.get_absolute_insertion_point() as u32 + self.textinput.borrow().get_absolute_insertion_point() as u32 } // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionend fn SetSelectionEnd(&self, end: u32) { - self.set_selection_range(self.SelectionStart(), end, &self.selection_direction.get()); + let selection_start = self.SelectionStart(); + self.textinput.borrow_mut().set_selection_range(selection_start, end); + self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage); } // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectiondirection fn SelectionDirection(&self) -> DOMString { - match self.selection_direction.get() { - SelectionDirection::Forward => DOMString::from("forward"), - SelectionDirection::Backward => DOMString::from("backward"), - SelectionDirection::None => DOMString::from("none"), - } + DOMString::from(self.textinput.borrow().selection_direction) } // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectiondirection fn SetSelectionDirection(&self, direction: DOMString) { - self.SetSelectionRange(self.SelectionStart(), self.SelectionEnd(), Some(direction)); + self.textinput.borrow_mut().selection_direction = SelectionDirection::from(direction); } // https://html.spec.whatwg.org/multipage/#dom-textarea/input-setselectionrange fn SetSelectionRange(&self, start: u32, end: u32, direction: Option<DOMString>) { - let selection_direction = match direction { - Some(selection_direction) => { - match &*selection_direction { - "forward" => SelectionDirection::Forward, - "backward" => SelectionDirection::Backward, - _ => SelectionDirection::None, - } - }, - None => SelectionDirection::None, - }; - self.set_selection_range(start, end, &selection_direction); + let direction = direction.map_or(SelectionDirection::None, |d| SelectionDirection::from(d)); + self.textinput.borrow_mut().selection_direction = direction; + self.textinput.borrow_mut().set_selection_range(start, end); + self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage); } } diff --git a/components/script/dom/htmltextareaelement.rs b/components/script/dom/htmltextareaelement.rs index a9f8f170889..d3eb7827031 100644 --- a/components/script/dom/htmltextareaelement.rs +++ b/components/script/dom/htmltextareaelement.rs @@ -31,7 +31,7 @@ use script_traits::ScriptMsg as ConstellationMsg; use std::cell::Cell; use string_cache::Atom; use style::element_state::*; -use textinput::{KeyReaction, Lines, TextInput}; +use textinput::{KeyReaction, Lines, TextInput, SelectionDirection}; use util::str::DOMString; #[dom_struct] @@ -106,7 +106,8 @@ impl HTMLTextAreaElement { htmlelement: HTMLElement::new_inherited_with_state(IN_ENABLED_STATE, localName, prefix, document), - textinput: DOMRefCell::new(TextInput::new(Lines::Multiple, DOMString::new(), chan, None)), + textinput: DOMRefCell::new(TextInput::new( + Lines::Multiple, DOMString::new(), chan, None, SelectionDirection::None)), value_changed: Cell::new(false), } } @@ -216,6 +217,48 @@ impl HTMLTextAreaElementMethods for HTMLTextAreaElement { fn Labels(&self) -> Root<NodeList> { self.upcast::<HTMLElement>().labels() } + + // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectiondirection + fn SetSelectionDirection(&self, direction: DOMString) { + self.textinput.borrow_mut().selection_direction = SelectionDirection::from(direction); + } + + // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectiondirection + fn SelectionDirection(&self) -> DOMString { + DOMString::from(self.textinput.borrow().selection_direction) + } + + // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionend + fn SetSelectionEnd(&self, end: u32) { + let selection_start = self.SelectionStart(); + self.textinput.borrow_mut().set_selection_range(selection_start, end); + self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage); + } + + // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionend + fn SelectionEnd(&self) -> u32 { + self.textinput.borrow().get_absolute_insertion_point() as u32 + } + + // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionstart + fn SetSelectionStart(&self, start: u32) { + let selection_end = self.SelectionEnd(); + self.textinput.borrow_mut().set_selection_range(start, selection_end); + self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage); + } + + // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionstart + fn SelectionStart(&self) -> u32 { + self.textinput.borrow().get_selection_start() + } + + // https://html.spec.whatwg.org/multipage/#dom-textarea/input-setselectionrange + fn SetSelectionRange(&self, start: u32, end: u32, direction: Option<DOMString>) { + let direction = direction.map_or(SelectionDirection::None, |d| SelectionDirection::from(d)); + self.textinput.borrow_mut().selection_direction = direction; + self.textinput.borrow_mut().set_selection_range(start, end); + self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage); + } } diff --git a/components/script/dom/webidls/HTMLTextAreaElement.webidl b/components/script/dom/webidls/HTMLTextAreaElement.webidl index 797a537d0cc..e5e2cabdb78 100644 --- a/components/script/dom/webidls/HTMLTextAreaElement.webidl +++ b/components/script/dom/webidls/HTMLTextAreaElement.webidl @@ -38,11 +38,11 @@ interface HTMLTextAreaElement : HTMLElement { readonly attribute NodeList labels; //void select(); - // attribute unsigned long selectionStart; - // attribute unsigned long selectionEnd; - // attribute DOMString selectionDirection; + attribute unsigned long selectionStart; + attribute unsigned long selectionEnd; + attribute DOMString selectionDirection; //void setRangeText(DOMString replacement); //void setRangeText(DOMString replacement, unsigned long start, unsigned long end, // optional SelectionMode selectionMode = "preserve"); - //void setSelectionRange(unsigned long start, unsigned long end, optional DOMString direction); + void setSelectionRange(unsigned long start, unsigned long end, optional DOMString direction); }; |