Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle cases where selection API doesn't apply #19461

Merged
merged 1 commit into from
Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions components/script/dom/htmlinputelement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,18 @@ impl TextControl for HTMLInputElement {
fn textinput(&self) -> &DomRefCell<TextInput<ScriptToConstellationChan>> {
&self.textinput
}

// https://html.spec.whatwg.org/multipage/#concept-input-apply
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 +691,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading