aboutsummaryrefslogtreecommitdiffstats
path: root/components/script
diff options
context:
space:
mode:
authorbors-servo <lbergstrom+bors@mozilla.com>2018-01-26 13:58:01 -0600
committerGitHub <noreply@github.com>2018-01-26 13:58:01 -0600
commitc2dfece49f1d59f51a3207cd3d88c282ee1adf70 (patch)
tree824bc776762e450e937595676dba99040875822b /components/script
parentce17959f7c5f817bc5739c6693c93cafb1855f4f (diff)
parenta8b64aca2a9c5e6e3756145afc0dedb606947ef8 (diff)
downloadservo-c2dfece49f1d59f51a3207cd3d88c282ee1adf70.tar.gz
servo-c2dfece49f1d59f51a3207cd3d88c282ee1adf70.zip
Auto merge of #19544 - jonleighton:issue-19171-5, r=nox
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 -->
Diffstat (limited to 'components/script')
-rwxr-xr-xcomponents/script/dom/htmlinputelement.rs72
-rwxr-xr-xcomponents/script/dom/htmltextareaelement.rs51
-rw-r--r--components/script/dom/textcontrol.rs162
-rw-r--r--components/script/dom/webidls/HTMLFormElement.webidl8
-rw-r--r--components/script/dom/webidls/HTMLInputElement.webidl10
-rw-r--r--components/script/dom/webidls/HTMLTextAreaElement.webidl10
-rw-r--r--components/script/textinput.rs282
7 files changed, 435 insertions, 160 deletions
diff --git a/components/script/dom/htmlinputelement.rs b/components/script/dom/htmlinputelement.rs
index ed153529889..e70afde60c2 100755
--- a/components/script/dom/htmlinputelement.rs
+++ b/components/script/dom/htmlinputelement.rs
@@ -8,6 +8,7 @@ use dom::attr::Attr;
use dom::bindings::cell::DomRefCell;
use dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use dom::bindings::codegen::Bindings::FileListBinding::FileListMethods;
+use dom::bindings::codegen::Bindings::HTMLFormElementBinding::SelectionMode;
use dom::bindings::codegen::Bindings::HTMLInputElementBinding;
use dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods;
use dom::bindings::codegen::Bindings::KeyboardEventBinding::KeyboardEventMethods;
@@ -52,7 +53,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::{Direction, SelectionDirection, TextInput};
use textinput::KeyReaction::{DispatchInput, Nothing, RedrawSelection, TriggerDefaultAction};
use textinput::Lines::Single;
@@ -188,7 +189,6 @@ pub struct HTMLInputElement {
input_type: Cell<InputType>,
checked_changed: Cell<bool>,
placeholder: DomRefCell<DOMString>,
- value_changed: Cell<bool>,
size: Cell<u32>,
maxlength: Cell<i32>,
minlength: Cell<i32>,
@@ -244,7 +244,6 @@ impl HTMLInputElement {
input_type: Cell::new(Default::default()),
placeholder: DomRefCell::new(DOMString::new()),
checked_changed: Cell::new(false),
- value_changed: Cell::new(false),
maxlength: Cell::new(DEFAULT_MAX_LENGTH),
minlength: Cell::new(DEFAULT_MIN_LENGTH),
size: Cell::new(DEFAULT_INPUT_SIZE),
@@ -374,7 +373,7 @@ impl LayoutHTMLInputElementHelpers for LayoutDom<HTMLInputElement> {
match (*self.unsafe_get()).input_type() {
InputType::Password => {
let text = get_raw_textinput_value(self);
- let sel = textinput.get_absolute_selection_range();
+ let sel = textinput.sorted_selection_offsets_range();
// Translate indices from the raw value to indices in the replacement value.
let char_start = text[.. sel.start].chars().count();
@@ -383,7 +382,7 @@ impl LayoutHTMLInputElementHelpers for LayoutDom<HTMLInputElement> {
let bytes_per_char = PASSWORD_REPLACEMENT_CHAR.len_utf8();
Some(char_start * bytes_per_char .. char_end * bytes_per_char)
}
- input_type if input_type.is_textual() => Some(textinput.get_absolute_selection_range()),
+ input_type if input_type.is_textual() => Some(textinput.sorted_selection_offsets_range()),
_ => None
}
}
@@ -417,6 +416,35 @@ impl TextControl for HTMLInputElement {
_ => false
}
}
+
+ // https://html.spec.whatwg.org/multipage/#concept-input-apply
+ //
+ // Defines input types to which the select() IDL method applies. These are a superset of the
+ // types for which selection_api_applies() returns true.
+ //
+ // Types omitted which could theoretically be included if they were
+ // rendered as a text control: file
+ fn has_selectable_text(&self) -> bool {
+ match self.input_type() {
+ InputType::Text | InputType::Search | InputType::Url
+ | InputType::Tel | InputType::Password | InputType::Email
+ | InputType::Date | InputType::Month | InputType::Week
+ | InputType::Time | InputType::DatetimeLocal | InputType::Number
+ | InputType::Color => {
+ true
+ }
+
+ InputType::Button | InputType::Checkbox | InputType::File
+ | InputType::Hidden | InputType::Image | InputType::Radio
+ | InputType::Range | InputType::Reset | InputType::Submit => {
+ false
+ }
+ }
+ }
+
+ fn set_dirty_value_flag(&self, value: bool) {
+ self.value_dirty.set(value)
+ }
}
impl HTMLInputElementMethods for HTMLInputElement {
@@ -538,8 +566,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(Direction::Forward);
}
}
ValueMode::Default |
@@ -557,7 +584,6 @@ impl HTMLInputElementMethods for HTMLInputElement {
}
}
- self.value_changed.set(true);
self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);
Ok(())
}
@@ -687,6 +713,11 @@ impl HTMLInputElementMethods for HTMLInputElement {
}
}
+ // https://html.spec.whatwg.org/multipage/#dom-textarea/input-select
+ fn Select(&self) {
+ self.dom_select(); // defined in TextControl trait
+ }
+
// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionstart
fn GetSelectionStart(&self) -> Option<u32> {
self.get_dom_selection_start()
@@ -722,6 +753,19 @@ impl HTMLInputElementMethods for HTMLInputElement {
self.set_dom_selection_range(start, end, direction)
}
+ // https://html.spec.whatwg.org/multipage/#dom-textarea/input-setrangetext
+ fn SetRangeText(&self, replacement: DOMString) -> ErrorResult {
+ // defined in TextControl trait
+ self.set_dom_range_text(replacement, None, None, Default::default())
+ }
+
+ // https://html.spec.whatwg.org/multipage/#dom-textarea/input-setrangetext
+ fn SetRangeText_(&self, replacement: DOMString, start: u32, end: u32,
+ selection_mode: SelectionMode) -> ErrorResult {
+ // defined in TextControl trait
+ self.set_dom_range_text(replacement, Some(start), Some(end), selection_mode)
+ }
+
// Select the files based on filepaths passed in,
// enabled by dom.htmlinputelement.select_files.enabled,
// used for test purpose.
@@ -902,7 +946,6 @@ impl HTMLInputElement {
self.SetValue(self.DefaultValue())
.expect("Failed to reset input value to default.");
self.value_dirty.set(false);
- self.value_changed.set(false);
self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);
}
@@ -1116,6 +1159,8 @@ impl VirtualMethods for HTMLInputElement {
// https://html.spec.whatwg.org/multipage/#input-type-change
let (old_value_mode, old_idl_value) = (self.value_mode(), self.Value());
+ let previously_selectable = self.selection_api_applies();
+
self.input_type.set(new_type);
if new_type.is_textual() {
@@ -1167,6 +1212,11 @@ impl VirtualMethods for HTMLInputElement {
// Step 6
self.sanitize_value();
+
+ // Steps 7-9
+ if !previously_selectable && self.selection_api_applies() {
+ self.textinput.borrow_mut().clear_selection_to_limit(Direction::Backward);
+ }
},
AttributeMutation::Removed => {
if self.input_type() == InputType::Radio {
@@ -1184,7 +1234,7 @@ impl VirtualMethods for HTMLInputElement {
self.update_placeholder_shown_state();
},
- &local_name!("value") if !self.value_changed.get() => {
+ &local_name!("value") if !self.value_dirty.get() => {
let value = mutation.new_value(attr).map(|value| (**value).to_owned());
self.textinput.borrow_mut().set_content(
value.map_or(DOMString::new(), DOMString::from));
@@ -1327,7 +1377,7 @@ impl VirtualMethods for HTMLInputElement {
keyevent.MetaKey());
},
DispatchInput => {
- self.value_changed.set(true);
+ self.value_dirty.set(true);
self.update_placeholder_shown_state();
self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);
event.mark_as_handled();
diff --git a/components/script/dom/htmltextareaelement.rs b/components/script/dom/htmltextareaelement.rs
index af69e227d60..bdb0020c616 100755
--- a/components/script/dom/htmltextareaelement.rs
+++ b/components/script/dom/htmltextareaelement.rs
@@ -5,6 +5,7 @@
use dom::attr::Attr;
use dom::bindings::cell::DomRefCell;
use dom::bindings::codegen::Bindings::EventBinding::EventMethods;
+use dom::bindings::codegen::Bindings::HTMLFormElementBinding::SelectionMode;
use dom::bindings::codegen::Bindings::HTMLTextAreaElementBinding;
use dom::bindings::codegen::Bindings::HTMLTextAreaElementBinding::HTMLTextAreaElementMethods;
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
@@ -35,7 +36,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::{Direction, KeyReaction, Lines, SelectionDirection, TextInput};
#[dom_struct]
pub struct HTMLTextAreaElement {
@@ -44,7 +45,7 @@ pub struct HTMLTextAreaElement {
textinput: DomRefCell<TextInput<ScriptToConstellationChan>>,
placeholder: DomRefCell<DOMString>,
// https://html.spec.whatwg.org/multipage/#concept-textarea-dirty
- value_changed: Cell<bool>,
+ value_dirty: Cell<bool>,
form_owner: MutNullableDom<HTMLFormElement>,
}
@@ -81,7 +82,7 @@ impl LayoutHTMLTextAreaElementHelpers for LayoutDom<HTMLTextAreaElement> {
return None;
}
let textinput = (*self.unsafe_get()).textinput.borrow_for_layout();
- Some(textinput.get_absolute_selection_range())
+ Some(textinput.sorted_selection_offsets_range())
}
#[allow(unsafe_code)]
@@ -122,7 +123,7 @@ impl HTMLTextAreaElement {
placeholder: DomRefCell::new(DOMString::new()),
textinput: DomRefCell::new(TextInput::new(
Lines::Multiple, DOMString::new(), chan, None, None, SelectionDirection::None)),
- value_changed: Cell::new(false),
+ value_dirty: Cell::new(false),
form_owner: Default::default(),
}
}
@@ -152,6 +153,14 @@ impl TextControl for HTMLTextAreaElement {
fn selection_api_applies(&self) -> bool {
true
}
+
+ fn has_selectable_text(&self) -> bool {
+ true
+ }
+
+ fn set_dirty_value_flag(&self, value: bool) {
+ self.value_dirty.set(value)
+ }
}
impl HTMLTextAreaElementMethods for HTMLTextAreaElement {
@@ -227,7 +236,7 @@ impl HTMLTextAreaElementMethods for HTMLTextAreaElement {
// if the element's dirty value flag is false, then the element's
// raw value must be set to the value of the element's textContent IDL attribute
- if !self.value_changed.get() {
+ if !self.value_dirty.get() {
self.reset();
}
}
@@ -243,19 +252,19 @@ impl HTMLTextAreaElementMethods for HTMLTextAreaElement {
// Step 1
let old_value = textinput.get_content();
- let old_selection = textinput.selection_begin;
+ let old_selection = textinput.selection_origin;
// Step 2
textinput.set_content(value);
// Step 3
- self.value_changed.set(true);
+ self.value_dirty.set(true);
if old_value != textinput.get_content() {
// Step 4
- textinput.adjust_horizontal_to_limit(Direction::Forward, Selection::NotSelected);
+ textinput.clear_selection_to_limit(Direction::Forward);
} else {
- textinput.selection_begin = old_selection;
+ textinput.selection_origin = old_selection;
}
self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);
@@ -266,6 +275,11 @@ impl HTMLTextAreaElementMethods for HTMLTextAreaElement {
self.upcast::<HTMLElement>().labels()
}
+ // https://html.spec.whatwg.org/multipage/#dom-textarea/input-select
+ fn Select(&self) {
+ self.dom_select(); // defined in TextControl trait
+ }
+
// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionstart
fn GetSelectionStart(&self) -> Option<u32> {
self.get_dom_selection_start()
@@ -300,6 +314,19 @@ impl HTMLTextAreaElementMethods for HTMLTextAreaElement {
fn SetSelectionRange(&self, start: u32, end: u32, direction: Option<DOMString>) -> ErrorResult {
self.set_dom_selection_range(start, end, direction)
}
+
+ // https://html.spec.whatwg.org/multipage/#dom-textarea/input-setrangetext
+ fn SetRangeText(&self, replacement: DOMString) -> ErrorResult {
+ // defined in TextControl trait
+ self.set_dom_range_text(replacement, None, None, Default::default())
+ }
+
+ // https://html.spec.whatwg.org/multipage/#dom-textarea/input-setrangetext
+ fn SetRangeText_(&self, replacement: DOMString, start: u32, end: u32,
+ selection_mode: SelectionMode) -> ErrorResult {
+ // defined in TextControl trait
+ self.set_dom_range_text(replacement, Some(start), Some(end), selection_mode)
+ }
}
@@ -307,7 +334,7 @@ impl HTMLTextAreaElement {
pub fn reset(&self) {
// https://html.spec.whatwg.org/multipage/#the-textarea-element:concept-form-reset-control
self.SetValue(self.DefaultValue());
- self.value_changed.set(false);
+ self.value_dirty.set(false);
}
}
@@ -400,7 +427,7 @@ impl VirtualMethods for HTMLTextAreaElement {
if let Some(ref s) = self.super_type() {
s.children_changed(mutation);
}
- if !self.value_changed.get() {
+ if !self.value_dirty.get() {
self.reset();
}
}
@@ -423,7 +450,7 @@ impl VirtualMethods for HTMLTextAreaElement {
match action {
KeyReaction::TriggerDefaultAction => (),
KeyReaction::DispatchInput => {
- self.value_changed.set(true);
+ self.value_dirty.set(true);
self.update_placeholder_shown_state();
self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);
event.mark_as_handled();
diff --git a/components/script/dom/textcontrol.rs b/components/script/dom/textcontrol.rs
index 9143c2bda23..342b081621d 100644
--- a/components/script/dom/textcontrol.rs
+++ b/components/script/dom/textcontrol.rs
@@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::cell::DomRefCell;
+use dom::bindings::codegen::Bindings::HTMLFormElementBinding::SelectionMode;
use dom::bindings::conversions::DerivedFrom;
use dom::bindings::error::{Error, ErrorResult};
use dom::bindings::str::DOMString;
@@ -10,11 +11,24 @@ use dom::event::{EventBubbles, EventCancelable};
use dom::eventtarget::EventTarget;
use dom::node::{Node, NodeDamage, window_from_node};
use script_traits::ScriptToConstellationChan;
-use textinput::{SelectionDirection, TextInput};
+use textinput::{SelectionDirection, SelectionState, TextInput};
pub trait TextControl: DerivedFrom<EventTarget> + DerivedFrom<Node> {
fn textinput(&self) -> &DomRefCell<TextInput<ScriptToConstellationChan>>;
fn selection_api_applies(&self) -> bool;
+ fn has_selectable_text(&self) -> bool;
+ fn set_dirty_value_flag(&self, value: bool);
+
+ // https://html.spec.whatwg.org/multipage/#dom-textarea/input-select
+ fn dom_select(&self) {
+ // Step 1
+ if !self.has_selectable_text() {
+ return;
+ }
+
+ // Step 2
+ self.set_selection_range(Some(0), Some(u32::max_value()), None, None);
+ }
// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionstart
fn get_dom_selection_start(&self) -> Option<u32> {
@@ -45,7 +59,7 @@ pub trait TextControl: DerivedFrom<EventTarget> + DerivedFrom<Node> {
}
// Step 4
- self.set_selection_range(start, Some(end), Some(self.selection_direction()));
+ self.set_selection_range(start, Some(end), Some(self.selection_direction()), None);
Ok(())
}
@@ -68,7 +82,7 @@ pub trait TextControl: DerivedFrom<EventTarget> + DerivedFrom<Node> {
}
// Step 2
- self.set_selection_range(Some(self.selection_start()), end, Some(self.selection_direction()));
+ self.set_selection_range(Some(self.selection_start()), end, Some(self.selection_direction()), None);
Ok(())
}
@@ -93,7 +107,8 @@ pub trait TextControl: DerivedFrom<EventTarget> + DerivedFrom<Node> {
self.set_selection_range(
Some(self.selection_start()),
Some(self.selection_end()),
- direction.map(|d| SelectionDirection::from(d))
+ direction.map(|d| SelectionDirection::from(d)),
+ None
);
Ok(())
}
@@ -106,16 +121,125 @@ pub trait TextControl: DerivedFrom<EventTarget> + DerivedFrom<Node> {
}
// Step 2
- self.set_selection_range(Some(start), Some(end), direction.map(|d| SelectionDirection::from(d)));
+ self.set_selection_range(Some(start), Some(end), direction.map(|d| SelectionDirection::from(d)), None);
+ Ok(())
+ }
+
+ // https://html.spec.whatwg.org/multipage/#dom-textarea/input-setrangetext
+ fn set_dom_range_text(&self, replacement: DOMString, start: Option<u32>, end: Option<u32>,
+ selection_mode: SelectionMode) -> ErrorResult {
+ // Step 1
+ if !self.selection_api_applies() {
+ return Err(Error::InvalidState);
+ }
+
+ // Step 2
+ self.set_dirty_value_flag(true);
+
+ // Step 3
+ let mut start = start.unwrap_or_else(|| self.selection_start());
+ let mut end = end.unwrap_or_else(|| self.selection_end());
+
+ // Step 4
+ if start > end {
+ return Err(Error::IndexSize);
+ }
+
+ // Save the original selection state to later pass to set_selection_range, because we will
+ // change the selection state in order to replace the text in the range.
+ let original_selection_state = self.textinput().borrow().selection_state();
+
+ let content_length = self.textinput().borrow().len() as u32;
+
+ // Step 5
+ if start > content_length {
+ start = content_length;
+ }
+
+ // Step 6
+ if end > content_length {
+ end = content_length;
+ }
+
+ // Step 7
+ let mut selection_start = self.selection_start();
+
+ // Step 8
+ let mut selection_end = self.selection_end();
+
+ // Step 11
+ // Must come before the textinput.replace_selection() call, as replacement gets moved in
+ // that call.
+ let new_length = replacement.len() as u32;
+
+ {
+ let mut textinput = self.textinput().borrow_mut();
+
+ // Steps 9-10
+ textinput.set_selection_range(start, end, SelectionDirection::None);
+ textinput.replace_selection(replacement);
+ }
+
+ // Step 12
+ let new_end = start + new_length;
+
+ // Step 13
+ match selection_mode {
+ SelectionMode::Select => {
+ selection_start = start;
+ selection_end = new_end;
+ },
+
+ SelectionMode::Start => {
+ selection_start = start;
+ selection_end = start;
+ },
+
+ SelectionMode::End => {
+ selection_start = new_end;
+ selection_end = new_end;
+ },
+
+ SelectionMode::Preserve => {
+ // Sub-step 1
+ let old_length = end - start;
+
+ // Sub-step 2
+ let delta = (new_length as isize) - (old_length as isize);
+
+ // Sub-step 3
+ if selection_start > end {
+ selection_start = ((selection_start as isize) + delta) as u32;
+ } else if selection_start > start {
+ selection_start = start;
+ }
+
+ // Sub-step 4
+ if selection_end > end {
+ selection_end = ((selection_end as isize) + delta) as u32;
+ } else if selection_end > start {
+ selection_end = new_end;
+ }
+ },
+ }
+
+ // Step 14
+ self.set_selection_range(
+ Some(selection_start),
+ Some(selection_end),
+ None,
+ Some(original_selection_state)
+ );
+
Ok(())
}
fn selection_start(&self) -> u32 {
- self.textinput().borrow().get_selection_start()
+ self.textinput().borrow().selection_start_offset() as u32
}
fn selection_end(&self) -> u32 {
- self.textinput().borrow().get_absolute_insertion_point() as u32
+ self.textinput().borrow().selection_end_offset() as u32
}
fn selection_direction(&self) -> SelectionDirection {
@@ -123,7 +247,11 @@ pub trait TextControl: DerivedFrom<EventTarget> + DerivedFrom<Node> {
}
// https://html.spec.whatwg.org/multipage/#set-the-selection-range
- fn set_selection_range(&self, start: Option<u32>, end: Option<u32>, direction: Option<SelectionDirection>) {
+ fn set_selection_range(&self, start: Option<u32>, end: Option<u32>, direction: Option<SelectionDirection>,
+ original_selection_state: Option<SelectionState>) {
+ let mut textinput = self.textinput().borrow_mut();
+ let original_selection_state = original_selection_state.unwrap_or_else(|| textinput.selection_state());
+
// Step 1
let start = start.unwrap_or(0);
@@ -131,16 +259,18 @@ pub trait TextControl: DerivedFrom<EventTarget> + DerivedFrom<Node> {
let end = end.unwrap_or(0);
// Steps 3-5
- self.textinput().borrow_mut().set_selection_range(start, end, direction.unwrap_or(SelectionDirection::None));
+ textinput.set_selection_range(start, end, direction.unwrap_or(SelectionDirection::None));
// Step 6
- let window = window_from_node(self);
- let _ = window.user_interaction_task_source().queue_event(
- &self.upcast::<EventTarget>(),
- atom!("select"),
- EventBubbles::Bubbles,
- EventCancelable::NotCancelable,
- &window);
+ if textinput.selection_state() != original_selection_state {
+ let window = window_from_node(self);
+ window.user_interaction_task_source().queue_event(
+ &self.upcast::<EventTarget>(),
+ atom!("select"),
+ EventBubbles::Bubbles,
+ EventCancelable::NotCancelable,
+ &window);
+ }
self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);
}
diff --git a/components/script/dom/webidls/HTMLFormElement.webidl b/components/script/dom/webidls/HTMLFormElement.webidl
index b4a2504fd60..1514e38b6f5 100644
--- a/components/script/dom/webidls/HTMLFormElement.webidl
+++ b/components/script/dom/webidls/HTMLFormElement.webidl
@@ -35,3 +35,11 @@ interface HTMLFormElement : HTMLElement {
//boolean checkValidity();
//boolean reportValidity();
};
+
+// https://html.spec.whatwg.org/multipage/#selectionmode
+enum SelectionMode {
+ "preserve", // default
+ "select",
+ "start",
+ "end"
+};
diff --git a/components/script/dom/webidls/HTMLInputElement.webidl b/components/script/dom/webidls/HTMLInputElement.webidl
index 93a5a7f108b..e76ca054a6f 100644
--- a/components/script/dom/webidls/HTMLInputElement.webidl
+++ b/components/script/dom/webidls/HTMLInputElement.webidl
@@ -89,16 +89,18 @@ interface HTMLInputElement : HTMLElement {
readonly attribute NodeList labels;
- //void select();
+ void select();
[SetterThrows]
attribute unsigned long? selectionStart;
[SetterThrows]
attribute unsigned long? selectionEnd;
[SetterThrows]
attribute DOMString? selectionDirection;
- //void setRangeText(DOMString replacement);
- //void setRangeText(DOMString replacement, unsigned long start, unsigned long end,
- // optional SelectionMode selectionMode = "preserve");
+ [Throws]
+ void setRangeText(DOMString replacement);
+ [Throws]
+ void setRangeText(DOMString replacement, unsigned long start, unsigned long end,
+ optional SelectionMode selectionMode = "preserve");
[Throws]
void setSelectionRange(unsigned long start, unsigned long end, optional DOMString direction);
diff --git a/components/script/dom/webidls/HTMLTextAreaElement.webidl b/components/script/dom/webidls/HTMLTextAreaElement.webidl
index f0e8a0be118..3ff20242bfe 100644
--- a/components/script/dom/webidls/HTMLTextAreaElement.webidl
+++ b/components/script/dom/webidls/HTMLTextAreaElement.webidl
@@ -50,16 +50,18 @@ interface HTMLTextAreaElement : HTMLElement {
readonly attribute NodeList labels;
- // void select();
+ void select();
[SetterThrows]
attribute unsigned long? selectionStart;
[SetterThrows]
attribute unsigned long? selectionEnd;
[SetterThrows]
attribute DOMString? selectionDirection;
- // void setRangeText(DOMString replacement);
- // void setRangeText(DOMString replacement, unsigned long start, unsigned long end,
- // optional SelectionMode selectionMode = "preserve");
+ [Throws]
+ void setRangeText(DOMString replacement);
+ [Throws]
+ void setRangeText(DOMString replacement, unsigned long start, unsigned long end,
+ optional SelectionMode selectionMode = "preserve");
[Throws]
void setSelectionRange(unsigned long start, unsigned long end, optional DOMString direction);
};
diff --git a/components/script/textinput.rs b/components/script/textinput.rs
index 0fe0024a116..937ab38ed5d 100644
--- a/components/script/textinput.rs
+++ b/components/script/textinput.rs
@@ -48,7 +48,7 @@ impl From<SelectionDirection> for DOMString {
}
}
-#[derive(Clone, Copy, JSTraceable, MallocSizeOf, PartialEq)]
+#[derive(Clone, Copy, Debug, JSTraceable, MallocSizeOf, PartialEq, PartialOrd)]
pub struct TextPoint {
/// 0-based line number
pub line: usize,
@@ -56,6 +56,13 @@ pub struct TextPoint {
pub index: usize,
}
+#[derive(Clone, Copy, PartialEq)]
+pub struct SelectionState {
+ start: TextPoint,
+ end: TextPoint,
+ direction: SelectionDirection,
+}
+
/// Encapsulated state for handling keyboard input in a single or multiline text input control.
#[derive(JSTraceable, MallocSizeOf)]
pub struct TextInput<T: ClipboardProvider> {
@@ -63,8 +70,9 @@ pub struct TextInput<T: ClipboardProvider> {
lines: Vec<DOMString>,
/// Current cursor input point
pub edit_point: TextPoint,
- /// Beginning of selection range with edit_point as end that can span multiple lines.
- pub selection_begin: Option<TextPoint>,
+ /// The current selection goes from the selection_origin until the edit_point. Note that the
+ /// selection_origin may be after the edit_point, in the case of a backward selection.
+ pub selection_origin: Option<TextPoint>,
/// Is this a multiline input?
multiline: bool,
#[ignore_malloc_size_of = "Can't easily measure this generic type"]
@@ -156,7 +164,7 @@ impl<T: ClipboardProvider> TextInput<T> {
let mut i = TextInput {
lines: vec!(),
edit_point: Default::default(),
- selection_begin: None,
+ selection_origin: None,
multiline: lines == Lines::Multiple,
clipboard_provider: clipboard_provider,
max_length: max_length,
@@ -169,7 +177,7 @@ impl<T: ClipboardProvider> TextInput<T> {
/// Remove a character at the current editing point
pub fn delete_char(&mut self, dir: Direction) {
- if self.selection_begin.is_none() || self.selection_begin == Some(self.edit_point) {
+ if self.selection_origin.is_none() || self.selection_origin == Some(self.edit_point) {
self.adjust_horizontal_by_one(dir, Selection::Selected);
}
self.replace_selection(DOMString::new());
@@ -182,46 +190,93 @@ impl<T: ClipboardProvider> TextInput<T> {
/// Insert a string at the current editing point
pub fn insert_string<S: Into<String>>(&mut self, s: S) {
- if self.selection_begin.is_none() {
- self.selection_begin = Some(self.edit_point);
+ if self.selection_origin.is_none() {
+ self.selection_origin = Some(self.edit_point);
}
self.replace_selection(DOMString::from(s.into()));
}
- pub fn get_sorted_selection(&self) -> Option<(TextPoint, TextPoint)> {
- self.selection_begin.map(|begin| {
- let end = self.edit_point;
+ /// The selection origin, or the edit point if there is no selection. Note that the selection
+ /// origin may be after the edit point, in the case of a backward selection.
+ pub fn selection_origin_or_edit_point(&self) -> TextPoint {
+ self.selection_origin.unwrap_or(self.edit_point)
+ }
- if begin.line < end.line || (begin.line == end.line && begin.index < end.index) {
- (begin, end)
- } else {
- (end, begin)
- }
- })
+ /// The start of the selection (or the edit point, if there is no selection). Always less than
+ /// or equal to selection_end(), regardless of the selection direction.
+ pub fn selection_start(&self) -> TextPoint {
+ match self.selection_direction {
+ SelectionDirection::None | SelectionDirection::Forward => self.selection_origin_or_edit_point(),
+ SelectionDirection::Backward => self.edit_point,
+ }
}
- // Check that the selection is valid.
- fn assert_ok_selection(&self) {
- if let Some(begin) = self.selection_begin {
- debug_assert!(begin.line < self.lines.len());
- debug_assert!(begin.index <= self.lines[begin.line].len());
+ /// The UTF-8 byte offset of the selection_start()
+ pub fn selection_start_offset(&self) -> usize {
+ self.text_point_to_offset(&self.selection_start())
+ }
+
+ /// The end of the selection (or the edit point, if there is no selection). Always greater
+ /// than or equal to selection_start(), regardless of the selection direction.
+ pub fn selection_end(&self) -> TextPoint {
+ match self.selection_direction {
+ SelectionDirection::None | SelectionDirection::Forward => self.edit_point,
+ SelectionDirection::Backward => self.selection_origin_or_edit_point(),
}
- debug_assert!(self.edit_point.line < self.lines.len());
- debug_assert!(self.edit_point.index <= self.lines[self.edit_point.line].len());
+ }
+
+ /// The UTF-8 byte offset of the selection_end()
+ pub fn selection_end_offset(&self) -> usize {
+ self.text_point_to_offset(&self.selection_end())
+ }
+
+ /// Whether or not there is an active selection (the selection may be zero-length)
+ #[inline]
+ pub fn has_selection(&self) -> bool {
+ self.selection_origin.is_some()
+ }
+
+ /// Returns a tuple of (start, end) giving the bounds of the current selection. start is always
+ /// less than or equal to end.
+ pub fn sorted_selection_bounds(&self) -> (TextPoint, TextPoint) {
+ (self.selection_start(), self.selection_end())
}
/// Return the selection range as UTF-8 byte offsets from the start of the content.
///
- /// If there is no selection, returns an empty range at the insertion point.
- pub fn get_absolute_selection_range(&self) -> Range<usize> {
- match self.get_sorted_selection() {
- Some((begin, end)) => self.get_absolute_point_for_text_point(&begin) ..
- self.get_absolute_point_for_text_point(&end),
- None => {
- let insertion_point = self.get_absolute_insertion_point();
- insertion_point .. insertion_point
+ /// If there is no selection, returns an empty range at the edit point.
+ pub fn sorted_selection_offsets_range(&self) -> Range<usize> {
+ self.selection_start_offset() .. self.selection_end_offset()
+ }
+
+ /// The state of the current selection. Can be used to compare whether selection state has changed.
+ pub fn selection_state(&self) -> SelectionState {
+ SelectionState {
+ start: self.selection_start(),
+ end: self.selection_end(),
+ direction: self.selection_direction,
+ }
+ }
+
+ // Check that the selection is valid.
+ fn assert_ok_selection(&self) {
+ if let Some(begin) = self.selection_origin {
+ debug_assert!(begin.line < self.lines.len());
+ debug_assert!(begin.index <= self.lines[begin.line].len());
+
+ match self.selection_direction {
+ SelectionDirection::None | SelectionDirection::Forward => {
+ debug_assert!(begin <= self.edit_point)
+ },
+
+ SelectionDirection::Backward => {
+ debug_assert!(self.edit_point <= begin)
+ },
}
}
+
+ debug_assert!(self.edit_point.line < self.lines.len());
+ debug_assert!(self.edit_point.index <= self.lines[self.edit_point.line].len());
}
pub fn get_selection_text(&self) -> Option<String> {
@@ -242,78 +297,83 @@ impl<T: ClipboardProvider> TextInput<T> {
///
/// The accumulator `acc` can be mutated by the callback, and will be returned at the end.
fn fold_selection_slices<B, F: FnMut(&mut B, &str)>(&self, mut acc: B, mut f: F) -> B {
- match self.get_sorted_selection() {
- Some((begin, end)) if begin.line == end.line => {
- f(&mut acc, &self.lines[begin.line][begin.index..end.index])
- }
- Some((begin, end)) => {
- f(&mut acc, &self.lines[begin.line][begin.index..]);
- for line in &self.lines[begin.line + 1 .. end.line] {
+ if self.has_selection() {
+ let (start, end) = self.sorted_selection_bounds();
+
+ if start.line == end.line {
+ f(&mut acc, &self.lines[start.line][start.index..end.index])
+ } else {
+ f(&mut acc, &self.lines[start.line][start.index..]);
+ for line in &self.lines[start.line + 1 .. end.line] {
f(&mut acc, "\n");
f(&mut acc, line);
}
f(&mut acc, "\n");
f(&mut acc, &self.lines[end.line][..end.index])
}
- None => {}
}
+
acc
}
pub fn replace_selection(&mut self, insert: DOMString) {
- if let Some((begin, end)) = self.get_sorted_selection() {
- let allowed_to_insert_count = if let Some(max_length) = self.max_length {
- let len_after_selection_replaced = self.utf16_len() - self.selection_utf16_len();
- if len_after_selection_replaced >= max_length {
- // If, after deleting the selection, the len is still greater than the max
- // length, then don't delete/insert anything
- return
- }
+ if !self.has_selection() {
+ return
+ }
- max_length - len_after_selection_replaced
- } else {
- usize::MAX
- };
+ let (start, end) = self.sorted_selection_bounds();
- let last_char_index = len_of_first_n_code_units(&*insert, allowed_to_insert_count);
- let chars_to_insert = &insert[..last_char_index];
+ let allowed_to_insert_count = if let Some(max_length) = self.max_length {
+ let len_after_selection_replaced = self.utf16_len() - self.selection_utf16_len();
+ if len_after_selection_replaced >= max_length {
+ // If, after deleting the selection, the len is still greater than the max
+ // length, then don't delete/insert anything
+ return
+ }
- self.clear_selection();
+ max_length - len_after_selection_replaced
+ } else {
+ usize::MAX
+ };
- let new_lines = {
- let prefix = &self.lines[begin.line][..begin.index];
- let suffix = &self.lines[end.line][end.index..];
- let lines_prefix = &self.lines[..begin.line];
- let lines_suffix = &self.lines[end.line + 1..];
+ let last_char_index = len_of_first_n_code_units(&*insert, allowed_to_insert_count);
+ let chars_to_insert = &insert[..last_char_index];
- let mut insert_lines = if self.multiline {
- chars_to_insert.split('\n').map(|s| DOMString::from(s)).collect()
- } else {
- vec!(DOMString::from(chars_to_insert))
- };
+ self.clear_selection();
+
+ let new_lines = {
+ let prefix = &self.lines[start.line][..start.index];
+ let suffix = &self.lines[end.line][end.index..];
+ let lines_prefix = &self.lines[..start.line];
+ let lines_suffix = &self.lines[end.line + 1..];
- // FIXME(ajeffrey): effecient append for DOMStrings
- let mut new_line = prefix.to_owned();
+ let mut insert_lines = if self.multiline {
+ chars_to_insert.split('\n').map(|s| DOMString::from(s)).collect()
+ } else {
+ vec!(DOMString::from(chars_to_insert))
+ };
- new_line.push_str(&insert_lines[0]);
- insert_lines[0] = DOMString::from(new_line);
+ // FIXME(ajeffrey): effecient append for DOMStrings
+ let mut new_line = prefix.to_owned();
- let last_insert_lines_index = insert_lines.len() - 1;
- self.edit_point.index = insert_lines[last_insert_lines_index].len();
- self.edit_point.line = begin.line + last_insert_lines_index;
+ new_line.push_str(&insert_lines[0]);
+ insert_lines[0] = DOMString::from(new_line);
- // FIXME(ajeffrey): effecient append for DOMStrings
- insert_lines[last_insert_lines_index].push_str(suffix);
+ let last_insert_lines_index = insert_lines.len() - 1;
+ self.edit_point.index = insert_lines[last_insert_lines_index].len();
+ self.edit_point.line = start.line + last_insert_lines_index;
- let mut new_lines = vec!();
- new_lines.extend_from_slice(lines_prefix);
- new_lines.extend_from_slice(&insert_lines);
- new_lines.extend_from_slice(lines_suffix);
- new_lines
- };
+ // FIXME(ajeffrey): effecient append for DOMStrings
+ insert_lines[last_insert_lines_index].push_str(suffix);
- self.lines = new_lines;
- }
+ let mut new_lines = vec!();
+ new_lines.extend_from_slice(lines_prefix);
+ new_lines.extend_from_slice(&insert_lines);
+ new_lines.extend_from_slice(lines_suffix);
+ new_lines
+ };
+
+ self.lines = new_lines;
self.assert_ok_selection();
}
@@ -330,8 +390,8 @@ impl<T: ClipboardProvider> TextInput<T> {
}
if select == Selection::Selected {
- if self.selection_begin.is_none() {
- self.selection_begin = Some(self.edit_point);
+ if self.selection_origin.is_none() {
+ self.selection_origin = Some(self.edit_point);
}
} else {
self.clear_selection();
@@ -398,14 +458,19 @@ impl<T: ClipboardProvider> TextInput<T> {
fn adjust_selection_for_horizontal_change(&mut self, adjust: Direction, select: Selection)
-> bool {
if select == Selection::Selected {
- if self.selection_begin.is_none() {
- self.selection_begin = Some(self.edit_point);
+ if self.selection_origin.is_none() {
+ self.selection_origin = Some(self.edit_point);
}
+
+ self.selection_direction = match adjust {
+ Direction::Backward => SelectionDirection::Backward,
+ Direction::Forward => SelectionDirection::Forward,
+ };
} else {
- if let Some((begin, end)) = self.get_sorted_selection() {
+ if self.has_selection() {
self.edit_point = match adjust {
- Direction::Backward => begin,
- Direction::Forward => end,
+ Direction::Backward => self.selection_start(),
+ Direction::Forward => self.selection_end(),
};
self.clear_selection();
return true
@@ -451,7 +516,7 @@ impl<T: ClipboardProvider> TextInput<T> {
/// Select all text in the input control.
pub fn select_all(&mut self) {
- self.selection_begin = Some(TextPoint {
+ self.selection_origin = Some(TextPoint {
line: 0,
index: 0,
});
@@ -463,7 +528,14 @@ impl<T: ClipboardProvider> TextInput<T> {
/// Remove the current selection.
pub fn clear_selection(&mut self) {
- self.selection_begin = None;
+ 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, direction: Direction) {
+ self.clear_selection();
+ self.adjust_horizontal_to_limit(direction, Selection::NotSelected);
}
pub fn adjust_horizontal_by_word(&mut self, direction: Direction, select: Selection) {
@@ -780,17 +852,12 @@ impl<T: ClipboardProvider> TextInput<T> {
};
self.edit_point.line = min(self.edit_point.line, self.lines.len() - 1);
self.edit_point.index = min(self.edit_point.index, self.current_line_length());
- self.selection_begin = None;
+ self.selection_origin = None;
self.assert_ok_selection();
}
- /// Get the insertion point as a byte offset from the start of the content.
- pub fn get_absolute_insertion_point(&self) -> usize {
- self.get_absolute_point_for_text_point(&self.edit_point)
- }
-
/// Convert a TextPoint into a byte offset from the start of the content.
- pub fn get_absolute_point_for_text_point(&self, text_point: &TextPoint) -> usize {
+ fn text_point_to_offset(&self, text_point: &TextPoint) -> usize {
self.lines.iter().enumerate().fold(0, |acc, (i, val)| {
if i < text_point.line {
acc + val.len() + 1 // +1 for the \n
@@ -801,7 +868,7 @@ impl<T: ClipboardProvider> TextInput<T> {
}
/// Convert a byte offset from the start of the content into a TextPoint.
- pub fn get_text_point_for_absolute_point(&self, abs_point: usize) -> TextPoint {
+ fn offset_to_text_point(&self, abs_point: usize) -> TextPoint {
let mut index = abs_point;
let mut line = 0;
@@ -842,28 +909,17 @@ impl<T: ClipboardProvider> TextInput<T> {
match direction {
SelectionDirection::None |
SelectionDirection::Forward => {
- self.selection_begin = Some(self.get_text_point_for_absolute_point(start));
- self.edit_point = self.get_text_point_for_absolute_point(end);
+ self.selection_origin = Some(self.offset_to_text_point(start));
+ self.edit_point = self.offset_to_text_point(end);
},
SelectionDirection::Backward => {
- self.selection_begin = Some(self.get_text_point_for_absolute_point(end));
- self.edit_point = self.get_text_point_for_absolute_point(start);
+ self.selection_origin = Some(self.offset_to_text_point(end));
+ self.edit_point = self.offset_to_text_point(start);
}
}
self.assert_ok_selection();
}
- pub fn get_selection_start(&self) -> u32 {
- let selection_start = match self.selection_begin {
- Some(selection_begin_point) => {
- self.get_absolute_point_for_text_point(&selection_begin_point)
- },
- None => self.get_absolute_insertion_point()
- };
-
- selection_start as u32
- }
-
pub fn set_edit_point_index(&mut self, index: usize) {
let byte_size = self.lines[self.edit_point.line]
.graphemes(true)