Skip to content

Commit

Permalink
Handle cases where selection API doesn't apply
Browse files Browse the repository at this point in the history
The selection API only applies to certain <input> types:

https://html.spec.whatwg.org/multipage/#do-not-apply

This commit ensures that we handle that correctly.

Some notes:

1. TextControl::set_dom_selection_direction now calls
   set_selection_range(), which means that setting selectionDirection will
   now fire a selection event, as it should per the spec.

2. There is a test for the firing of the select event in
   tests/wpt/web-platform-tests/html/semantics/forms/textfieldselection/select-event.html,
   however the test did not run due to this syntax error:

   (pid:26017) "ERROR:script::dom::bindings::error: Error at http://web-platform.test:8000/html/semantics/forms/textfieldselection/select-event.html:50:11 missing = in const declaration"

   This happens due to the us of the "for (const foo of ...)" construct.
   Per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
   this should actually work, so it's somewhat unsatisfying to have to
   change the test.

3. I removed tests/wpt/web-platform-tests/html/semantics/forms/textfieldselection/selection-not-application-textarea.html
   because it doesn't seem to add any extra value - the selection API
   always applies to textarea elements, and the API is tested elsewhere.

4. If an <input>'s type is unset, it defaults to a text, and the
   selection API applies. Also, if an <input>'s type is set to an
   invalid value, it defaults to a text too. This second case doesn't
   currently work, and I'll need to do more restructuring of the code in
   a future commit. See discussion with nox in IRC:
   https://mozilla.logbot.info/servo/20171201#c13946454-c13946594
  • Loading branch information
jonleighton committed Dec 7, 2017
1 parent e646471 commit a3da184
Show file tree
Hide file tree
Showing 14 changed files with 371 additions and 158 deletions.
37 changes: 24 additions & 13 deletions components/script/dom/htmlinputelement.rs
Expand Up @@ -408,6 +408,17 @@ impl TextControl for HTMLInputElement {
fn textinput(&self) -> &DomRefCell<TextInput<ScriptToConstellationChan>> {
&self.textinput
}

fn selection_api_applies(&self) -> bool {
match self.input_type() {
InputType::Text | InputType::Search | InputType::Url
| InputType::Tel | InputType::Password => {
true
},

_ => false
}
}
}

impl HTMLInputElementMethods for HTMLInputElement {
Expand Down Expand Up @@ -679,38 +690,38 @@ impl HTMLInputElementMethods for HTMLInputElement {
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionstart
fn SelectionStart(&self) -> u32 {
self.dom_selection_start()
fn GetSelectionStart(&self) -> Option<u32> {
self.get_dom_selection_start()
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionstart
fn SetSelectionStart(&self, start: u32) {
self.set_dom_selection_start(start);
fn SetSelectionStart(&self, start: Option<u32>) -> ErrorResult {
self.set_dom_selection_start(start)
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionend
fn SelectionEnd(&self) -> u32 {
self.dom_selection_end()
fn GetSelectionEnd(&self) -> Option<u32> {
self.get_dom_selection_end()
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionend
fn SetSelectionEnd(&self, end: u32) {
fn SetSelectionEnd(&self, end: Option<u32>) -> ErrorResult {
self.set_dom_selection_end(end)
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectiondirection
fn SelectionDirection(&self) -> DOMString {
self.dom_selection_direction()
fn GetSelectionDirection(&self) -> Option<DOMString> {
self.get_dom_selection_direction()
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectiondirection
fn SetSelectionDirection(&self, direction: DOMString) {
self.set_dom_selection_direction(direction);
fn SetSelectionDirection(&self, direction: Option<DOMString>) -> ErrorResult {
self.set_dom_selection_direction(direction)
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-setselectionrange
fn SetSelectionRange(&self, start: u32, end: u32, direction: Option<DOMString>) {
self.set_dom_selection_range(start, end, direction);
fn SetSelectionRange(&self, start: u32, end: u32, direction: Option<DOMString>) -> ErrorResult {
self.set_dom_selection_range(start, end, direction)
}

// Select the files based on filepaths passed in,
Expand Down
33 changes: 19 additions & 14 deletions components/script/dom/htmltextareaelement.rs
Expand Up @@ -8,6 +8,7 @@ use dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use dom::bindings::codegen::Bindings::HTMLTextAreaElementBinding;
use dom::bindings::codegen::Bindings::HTMLTextAreaElementBinding::HTMLTextAreaElementMethods;
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use dom::bindings::error::ErrorResult;
use dom::bindings::inheritance::Castable;
use dom::bindings::root::{DomRoot, LayoutDom, MutNullableDom};
use dom::bindings::str::DOMString;
Expand Down Expand Up @@ -145,6 +146,10 @@ impl TextControl for HTMLTextAreaElement {
fn textinput(&self) -> &DomRefCell<TextInput<ScriptToConstellationChan>> {
&self.textinput
}

fn selection_api_applies(&self) -> bool {
true
}
}

impl HTMLTextAreaElementMethods for HTMLTextAreaElement {
Expand Down Expand Up @@ -260,38 +265,38 @@ impl HTMLTextAreaElementMethods for HTMLTextAreaElement {
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionstart
fn SelectionStart(&self) -> u32 {
self.dom_selection_start()
fn GetSelectionStart(&self) -> Option<u32> {
self.get_dom_selection_start()
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionstart
fn SetSelectionStart(&self, start: u32) {
self.set_dom_selection_start(start);
fn SetSelectionStart(&self, start: Option<u32>) -> ErrorResult {
self.set_dom_selection_start(start)
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionend
fn SelectionEnd(&self) -> u32 {
self.dom_selection_end()
fn GetSelectionEnd(&self) -> Option<u32> {
self.get_dom_selection_end()
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionend
fn SetSelectionEnd(&self, end: u32) {
self.set_dom_selection_end(end);
fn SetSelectionEnd(&self, end: Option<u32>) -> ErrorResult {
self.set_dom_selection_end(end)
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectiondirection
fn SelectionDirection(&self) -> DOMString {
self.dom_selection_direction()
fn GetSelectionDirection(&self) -> Option<DOMString> {
self.get_dom_selection_direction()
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectiondirection
fn SetSelectionDirection(&self, direction: DOMString) {
self.set_dom_selection_direction(direction);
fn SetSelectionDirection(&self, direction: Option<DOMString>) -> ErrorResult {
self.set_dom_selection_direction(direction)
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-setselectionrange
fn SetSelectionRange(&self, start: u32, end: u32, direction: Option<DOMString>) {
self.set_dom_selection_range(start, end, direction);
fn SetSelectionRange(&self, start: u32, end: u32, direction: Option<DOMString>) -> ErrorResult {
self.set_dom_selection_range(start, end, direction)
}
}

Expand Down
108 changes: 84 additions & 24 deletions components/script/dom/textcontrol.rs
Expand Up @@ -4,6 +4,7 @@

use dom::bindings::cell::DomRefCell;
use dom::bindings::conversions::DerivedFrom;
use dom::bindings::error::{Error, ErrorResult};
use dom::bindings::str::DOMString;
use dom::event::{EventBubbles, EventCancelable};
use dom::eventtarget::EventTarget;
Expand All @@ -13,65 +14,124 @@ use textinput::{SelectionDirection, TextInput};

pub trait TextControl: DerivedFrom<EventTarget> + DerivedFrom<Node> {
fn textinput(&self) -> &DomRefCell<TextInput<ScriptToConstellationChan>>;
fn selection_api_applies(&self) -> bool;

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionstart
fn dom_selection_start(&self) -> u32 {
self.textinput().borrow().get_selection_start()
fn get_dom_selection_start(&self) -> Option<u32> {
// Step 1
if !self.selection_api_applies() {
return None;
}

// Steps 2-3
Some(self.selection_start())
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionstart
fn set_dom_selection_start(&self, start: u32) {
fn set_dom_selection_start(&self, start: Option<u32>) -> ErrorResult {
// Step 1
if !self.selection_api_applies() {
return Err(Error::InvalidState);
}

// Step 2
let mut end = self.dom_selection_end();
let mut end = self.selection_end();

// Step 3
if end < start {
end = start;
if let Some(s) = start {
if end < s {
end = s;
}
}

// Step 4
self.set_selection_range(start, end, self.selection_direction());
self.set_selection_range(start, Some(end), Some(self.selection_direction()));
Ok(())
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionend
fn dom_selection_end(&self) -> u32 {
self.textinput().borrow().get_absolute_insertion_point() as u32
fn get_dom_selection_end(&self) -> Option<u32> {
// Step 1
if !self.selection_api_applies() {
return None;
}

// Steps 2-3
Some(self.selection_end())
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionend
fn set_dom_selection_end(&self, end: u32) {
self.set_selection_range(self.dom_selection_start(), end, self.selection_direction());
fn set_dom_selection_end(&self, end: Option<u32>) -> ErrorResult {
// Step 1
if !self.selection_api_applies() {
return Err(Error::InvalidState);
}

// Step 2
self.set_selection_range(Some(self.selection_start()), end, Some(self.selection_direction()));
Ok(())
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectiondirection
fn dom_selection_direction(&self) -> DOMString {
DOMString::from(self.selection_direction())
fn get_dom_selection_direction(&self) -> Option<DOMString> {
// Step 1
if !self.selection_api_applies() {
return None;
}

Some(DOMString::from(self.selection_direction()))
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectiondirection
fn set_dom_selection_direction(&self, direction: DOMString) {
self.textinput().borrow_mut().selection_direction = SelectionDirection::from(direction);
fn set_dom_selection_direction(&self, direction: Option<DOMString>) -> ErrorResult {
// Step 1
if !self.selection_api_applies() {
return Err(Error::InvalidState);
}

// Step 2
self.set_selection_range(
Some(self.selection_start()),
Some(self.selection_end()),
direction.map(|d| SelectionDirection::from(d))
);
Ok(())
}

// https://html.spec.whatwg.org/multipage/#dom-textarea/input-setselectionrange
fn set_dom_selection_range(&self, start: u32, end: u32, direction: Option<DOMString>) {
// Step 4
let direction = direction.map_or(SelectionDirection::None, |d| SelectionDirection::from(d));
fn set_dom_selection_range(&self, start: u32, end: u32, direction: Option<DOMString>) -> ErrorResult {
// Step 1
if !self.selection_api_applies() {
return Err(Error::InvalidState);
}

// Step 2
self.set_selection_range(Some(start), Some(end), direction.map(|d| SelectionDirection::from(d)));
Ok(())
}

self.set_selection_range(start, end, direction);
fn selection_start(&self) -> u32 {
self.textinput().borrow().get_selection_start()
}

fn selection_end(&self) -> u32 {
self.textinput().borrow().get_absolute_insertion_point() as u32
}

fn selection_direction(&self) -> SelectionDirection {
self.textinput().borrow().selection_direction
}

// https://html.spec.whatwg.org/multipage/#set-the-selection-range
fn set_selection_range(&self, start: u32, end: u32, direction: SelectionDirection) {
// Step 5
self.textinput().borrow_mut().selection_direction = direction;
fn set_selection_range(&self, start: Option<u32>, end: Option<u32>, direction: Option<SelectionDirection>) {
// Step 1
let start = start.unwrap_or(0);

// Step 3
self.textinput().borrow_mut().set_selection_range(start, end);
// Step 2
let end = end.unwrap_or(0);

// Steps 3-5
self.textinput().borrow_mut().set_selection_range(start, end, direction.unwrap_or(SelectionDirection::None));

// Step 6
let window = window_from_node(self);
Expand Down
12 changes: 8 additions & 4 deletions components/script/dom/webidls/HTMLInputElement.webidl
Expand Up @@ -90,13 +90,17 @@ interface HTMLInputElement : HTMLElement {
readonly attribute NodeList labels;

//void select();
attribute unsigned long selectionStart;
attribute unsigned long selectionEnd;
attribute DOMString selectionDirection;
[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");
void setSelectionRange(unsigned long start, unsigned long end, optional DOMString direction);
[Throws]
void setSelectionRange(unsigned long start, unsigned long end, optional DOMString direction);

// also has obsolete members

Expand Down
12 changes: 8 additions & 4 deletions components/script/dom/webidls/HTMLTextAreaElement.webidl
Expand Up @@ -51,11 +51,15 @@ interface HTMLTextAreaElement : HTMLElement {
readonly attribute NodeList labels;

// void select();
attribute unsigned long selectionStart;
attribute unsigned long selectionEnd;
attribute DOMString selectionDirection;
[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");
void setSelectionRange(unsigned long start, unsigned long end, optional DOMString direction);
[Throws]
void setSelectionRange(unsigned long start, unsigned long end, optional DOMString direction);
};
8 changes: 5 additions & 3 deletions components/script/textinput.rs
Expand Up @@ -21,7 +21,7 @@ pub enum Selection {
NotSelected
}

#[derive(Clone, Copy, JSTraceable, MallocSizeOf, PartialEq)]
#[derive(Clone, Copy, Debug, JSTraceable, MallocSizeOf, PartialEq)]
pub enum SelectionDirection {
Forward,
Backward,
Expand Down Expand Up @@ -825,7 +825,7 @@ impl<T: ClipboardProvider> TextInput<T> {
}
}

pub fn set_selection_range(&mut self, start: u32, end: u32) {
pub fn set_selection_range(&mut self, start: u32, end: u32, direction: SelectionDirection) {
let mut start = start as usize;
let mut end = end as usize;
let text_end = self.get_content().len();
Expand All @@ -837,7 +837,9 @@ impl<T: ClipboardProvider> TextInput<T> {
start = end;
}

match self.selection_direction {
self.selection_direction = direction;

match direction {
SelectionDirection::None |
SelectionDirection::Forward => {
self.selection_begin = Some(self.get_text_point_for_absolute_point(start));
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/script/textinput.rs
Expand Up @@ -583,19 +583,19 @@ fn test_textinput_cursor_position_correct_after_clearing_selection() {
#[test]
fn test_textinput_set_selection_with_direction() {
let mut textinput = text_input(Lines::Single, "abcdef");
textinput.selection_direction = SelectionDirection::Forward;
textinput.set_selection_range(2, 6);
textinput.set_selection_range(2, 6, SelectionDirection::Forward);
assert_eq!(textinput.edit_point.line, 0);
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);

textinput.selection_direction = SelectionDirection::Backward;
textinput.set_selection_range(2, 6);
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);
Expand Down

0 comments on commit a3da184

Please sign in to comment.