зеркало из https://github.com/mozilla/gecko-dev.git
servo: Merge #19544 - Text selection API conformance (from jonleighton:issue-19171-5); r=nox
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 Source-Repo: https://github.com/servo/servo Source-Revision: c2dfece49f1d59f51a3207cd3d88c282ee1adf70 --HG-- extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear extra : subtree_revision : 6321a59ea353cb0f8196c63a1e2644ee8171a584
This commit is contained in:
Родитель
74425c36d7
Коммит
dbd4f2f3f1
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
|
||||
if begin.line < end.line || (begin.line == end.line && begin.index < end.index) {
|
||||
(begin, end)
|
||||
} else {
|
||||
(end, begin)
|
||||
}
|
||||
})
|
||||
/// 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)
|
||||
}
|
||||
|
||||
// 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 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,
|
||||
}
|
||||
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_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(),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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
|
||||
}
|
||||
|
||||
max_length - len_after_selection_replaced
|
||||
} else {
|
||||
usize::MAX
|
||||
};
|
||||
|
||||
let last_char_index = len_of_first_n_code_units(&*insert, allowed_to_insert_count);
|
||||
let chars_to_insert = &insert[..last_char_index];
|
||||
|
||||
self.clear_selection();
|
||||
|
||||
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 mut insert_lines = if self.multiline {
|
||||
chars_to_insert.split('\n').map(|s| DOMString::from(s)).collect()
|
||||
} else {
|
||||
vec!(DOMString::from(chars_to_insert))
|
||||
};
|
||||
|
||||
// FIXME(ajeffrey): effecient append for DOMStrings
|
||||
let mut new_line = prefix.to_owned();
|
||||
|
||||
new_line.push_str(&insert_lines[0]);
|
||||
insert_lines[0] = DOMString::from(new_line);
|
||||
|
||||
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;
|
||||
|
||||
// FIXME(ajeffrey): effecient append for DOMStrings
|
||||
insert_lines[last_insert_lines_index].push_str(suffix);
|
||||
|
||||
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;
|
||||
if !self.has_selection() {
|
||||
return
|
||||
}
|
||||
|
||||
let (start, end) = self.sorted_selection_bounds();
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
max_length - len_after_selection_replaced
|
||||
} else {
|
||||
usize::MAX
|
||||
};
|
||||
|
||||
let last_char_index = len_of_first_n_code_units(&*insert, allowed_to_insert_count);
|
||||
let chars_to_insert = &insert[..last_char_index];
|
||||
|
||||
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..];
|
||||
|
||||
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))
|
||||
};
|
||||
|
||||
// FIXME(ajeffrey): effecient append for DOMStrings
|
||||
let mut new_line = prefix.to_owned();
|
||||
|
||||
new_line.push_str(&insert_lines[0]);
|
||||
insert_lines[0] = DOMString::from(new_line);
|
||||
|
||||
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;
|
||||
|
||||
// FIXME(ajeffrey): effecient append for DOMStrings
|
||||
insert_lines[last_insert_lines_index].push_str(suffix);
|
||||
|
||||
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)
|
||||
|
|
|
@ -222,7 +222,7 @@ fn test_textinput_delete_char() {
|
|||
let mut textinput = text_input(Lines::Single, "abcdefg");
|
||||
textinput.adjust_horizontal(2, Selection::NotSelected);
|
||||
// Set an empty selection range.
|
||||
textinput.selection_begin = Some(textinput.edit_point);
|
||||
textinput.selection_origin = Some(textinput.edit_point);
|
||||
textinput.delete_char(Direction::Backward);
|
||||
assert_eq!(textinput.get_content(), "acdefg");
|
||||
}
|
||||
|
@ -252,15 +252,15 @@ fn test_textinput_get_sorted_selection() {
|
|||
let mut textinput = text_input(Lines::Single, "abcdefg");
|
||||
textinput.adjust_horizontal(2, Selection::NotSelected);
|
||||
textinput.adjust_horizontal(2, Selection::Selected);
|
||||
let (begin, end) = textinput.get_sorted_selection().unwrap();
|
||||
assert_eq!(begin.index, 2);
|
||||
let (start, end) = textinput.sorted_selection_bounds();
|
||||
assert_eq!(start.index, 2);
|
||||
assert_eq!(end.index, 4);
|
||||
|
||||
textinput.clear_selection();
|
||||
|
||||
textinput.adjust_horizontal(-2, Selection::Selected);
|
||||
let (begin, end) = textinput.get_sorted_selection().unwrap();
|
||||
assert_eq!(begin.index, 2);
|
||||
let (start, end) = textinput.sorted_selection_bounds();
|
||||
assert_eq!(start.index, 2);
|
||||
assert_eq!(end.index, 4);
|
||||
}
|
||||
|
||||
|
@ -588,18 +588,18 @@ fn test_textinput_set_selection_with_direction() {
|
|||
assert_eq!(textinput.edit_point.index, 6);
|
||||
assert_eq!(textinput.selection_direction, SelectionDirection::Forward);
|
||||
|
||||
assert!(textinput.selection_begin.is_some());
|
||||
assert_eq!(textinput.selection_begin.unwrap().line, 0);
|
||||
assert_eq!(textinput.selection_begin.unwrap().index, 2);
|
||||
assert!(textinput.selection_origin.is_some());
|
||||
assert_eq!(textinput.selection_origin.unwrap().line, 0);
|
||||
assert_eq!(textinput.selection_origin.unwrap().index, 2);
|
||||
|
||||
textinput.set_selection_range(2, 6, SelectionDirection::Backward);
|
||||
assert_eq!(textinput.edit_point.line, 0);
|
||||
assert_eq!(textinput.edit_point.index, 2);
|
||||
assert_eq!(textinput.selection_direction, SelectionDirection::Backward);
|
||||
|
||||
assert!(textinput.selection_begin.is_some());
|
||||
assert_eq!(textinput.selection_begin.unwrap().line, 0);
|
||||
assert_eq!(textinput.selection_begin.unwrap().index, 6);
|
||||
assert!(textinput.selection_origin.is_some());
|
||||
assert_eq!(textinput.selection_origin.unwrap().line, 0);
|
||||
assert_eq!(textinput.selection_origin.unwrap().index, 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -611,3 +611,22 @@ fn test_textinput_unicode_handling() {
|
|||
textinput.set_edit_point_index(4);
|
||||
assert_eq!(textinput.edit_point.index, 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_selection_bounds() {
|
||||
let mut textinput = text_input(Lines::Single, "abcdef");
|
||||
|
||||
textinput.set_selection_range(2, 5, SelectionDirection::Forward);
|
||||
assert_eq!(TextPoint { line: 0, index: 2 }, textinput.selection_origin_or_edit_point());
|
||||
assert_eq!(TextPoint { line: 0, index: 2 }, textinput.selection_start());
|
||||
assert_eq!(TextPoint { line: 0, index: 5 }, textinput.selection_end());
|
||||
assert_eq!(2, textinput.selection_start_offset());
|
||||
assert_eq!(5, textinput.selection_end_offset());
|
||||
|
||||
textinput.set_selection_range(3, 6, SelectionDirection::Backward);
|
||||
assert_eq!(TextPoint { line: 0, index: 6 }, textinput.selection_origin_or_edit_point());
|
||||
assert_eq!(TextPoint { line: 0, index: 3 }, textinput.selection_start());
|
||||
assert_eq!(TextPoint { line: 0, index: 6 }, textinput.selection_end());
|
||||
assert_eq!(3, textinput.selection_start_offset());
|
||||
assert_eq!(6, textinput.selection_end_offset());
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче