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 -->
This commit is contained in:
bors-servo 2018-01-26 13:58:01 -06:00 committed by GitHub
commit c2dfece49f
19 changed files with 530 additions and 1269 deletions

View file

@ -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();

View file

@ -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();

View file

@ -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);
}

View file

@ -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"
};

View file

@ -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);

View file

@ -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);
};