Navigation Menu

Skip to content

Commit

Permalink
Revert "Correct default Selectionstart and SelectionEnd"
Browse files Browse the repository at this point in the history
This reverts commit b2c1f89.
  • Loading branch information
paavininanda committed Feb 22, 2018
1 parent 267f9db commit b517410
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 131 deletions.
2 changes: 1 addition & 1 deletion components/script/dom/htmlformelement.rs
Expand Up @@ -651,7 +651,7 @@ impl HTMLFormElement {
child.downcast::<HTMLSelectElement>().unwrap().reset();
}
NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLTextAreaElement)) => {
child.downcast::<HTMLTextAreaElement>().unwrap().reset(true);
child.downcast::<HTMLTextAreaElement>().unwrap().reset();
}
NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLOutputElement)) => {
// Unimplemented
Expand Down
79 changes: 36 additions & 43 deletions components/script/dom/htmlinputelement.rs
Expand Up @@ -551,7 +551,36 @@ impl HTMLInputElementMethods for HTMLInputElement {

// https://html.spec.whatwg.org/multipage/#dom-input-value
fn SetValue(&self, value: DOMString) -> ErrorResult {
self.update_text_contents(value, true)
match self.value_mode() {
ValueMode::Value => {
// Steps 1-2.
let old_value = mem::replace(self.textinput.borrow_mut().single_line_content_mut(), value);
// Step 3.
self.value_dirty.set(true);
// Step 4.
self.sanitize_value();
// Step 5.
if *self.textinput.borrow().single_line_content() != old_value {
self.textinput.borrow_mut().clear_selection_to_limit(Direction::Forward);
}
}
ValueMode::Default |
ValueMode::DefaultOn => {
self.upcast::<Element>().set_string_attribute(&local_name!("value"), value);
}
ValueMode::Filename => {
if value.is_empty() {
let window = window_from_node(self);
let fl = FileList::new(&window, vec![]);
self.filelist.set(Some(&fl));
} else {
return Err(Error::InvalidState);
}
}
}

self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);
Ok(())
}

// https://html.spec.whatwg.org/multipage/#dom-input-defaultvalue
Expand Down Expand Up @@ -906,8 +935,7 @@ impl HTMLInputElement {
InputType::Image => (),
_ => ()
}

self.update_text_contents(self.DefaultValue(), true)
self.SetValue(self.DefaultValue())
.expect("Failed to reset input value to default.");
self.value_dirty.set(false);
self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);
Expand Down Expand Up @@ -1056,44 +1084,6 @@ impl HTMLInputElement {
fn selection(&self) -> TextControlSelection<Self> {
TextControlSelection::new(&self, &self.textinput)
}

fn update_text_contents(&self, mut value: DOMString, update_text_cursor: bool) -> ErrorResult {
match self.value_mode() {
ValueMode::Value => {
// Step 3.
self.value_dirty.set(true);

// Step 4.
self.sanitize_value(&mut value);

let mut textinput = self.textinput.borrow_mut();

if *textinput.single_line_content() != value {
// Steps 1-2
textinput.set_content(value, update_text_cursor);

// Step 5.
textinput.clear_selection_to_limit(Direction::Forward, update_text_cursor);
}
}
ValueMode::Default |
ValueMode::DefaultOn => {
self.upcast::<Element>().set_string_attribute(&local_name!("value"), value);
}
ValueMode::Filename => {
if value.is_empty() {
let window = window_from_node(self);
let fl = FileList::new(&window, vec![]);
self.filelist.set(Some(&fl));
} else {
return Err(Error::InvalidState);
}
}
}

self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);
Ok(())
}
}

impl VirtualMethods for HTMLInputElement {
Expand All @@ -1103,6 +1093,7 @@ impl VirtualMethods for HTMLInputElement {

fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation) {
self.super_type().unwrap().attribute_mutated(attr, mutation);

match attr.local_name() {
&local_name!("disabled") => {
let disabled_state = match mutation {
Expand Down Expand Up @@ -1207,7 +1198,7 @@ impl VirtualMethods for HTMLInputElement {

// Steps 7-9
if !previously_selectable && self.selection_api_applies() {
textinput.clear_selection_to_limit(Direction::Backward, true);
self.textinput.borrow_mut().clear_selection_to_limit(Direction::Backward);
}
},
AttributeMutation::Removed => {
Expand All @@ -1231,7 +1222,9 @@ impl VirtualMethods for HTMLInputElement {
let mut value = value.map_or(DOMString::new(), DOMString::from);

self.sanitize_value(&mut value);
self.textinput.borrow_mut().set_content(value, true);

self.textinput.borrow_mut().set_content(
value.map_or(DOMString::new(), DOMString::from));
self.update_placeholder_shown_state();
},
&local_name!("name") if self.input_type() == InputType::Radio => {
Expand Down
52 changes: 25 additions & 27 deletions components/script/dom/htmltextareaelement.rs
Expand Up @@ -233,7 +233,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_dirty.get() {
self.reset(false);
self.reset();
}
}

Expand All @@ -244,7 +244,26 @@ impl HTMLTextAreaElementMethods for HTMLTextAreaElement {

// https://html.spec.whatwg.org/multipage/#dom-textarea-value
fn SetValue(&self, value: DOMString) {
self.update_text_contents(value, true);
let mut textinput = self.textinput.borrow_mut();

// Step 1
let old_value = textinput.get_content();
let old_selection = textinput.selection_origin;

// Step 2
textinput.set_content(value);

// Step 3
self.value_dirty.set(true);

if old_value != textinput.get_content() {
// Step 4
textinput.clear_selection_to_limit(Direction::Forward);
} else {
textinput.selection_origin = old_selection;
}

self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);
}

// https://html.spec.whatwg.org/multipage/#dom-lfe-labels
Expand Down Expand Up @@ -306,37 +325,16 @@ impl HTMLTextAreaElementMethods for HTMLTextAreaElement {


impl HTMLTextAreaElement {
pub fn reset(&self, update_text_cursor: bool) {
pub fn reset(&self) {
// https://html.spec.whatwg.org/multipage/#the-textarea-element:concept-form-reset-control
self.update_text_contents(self.DefaultValue(), update_text_cursor);
self.SetValue(self.DefaultValue());
self.value_dirty.set(false);
}

#[allow(unrooted_must_root)]
fn selection(&self) -> TextControlSelection<Self> {
TextControlSelection::new(&self, &self.textinput)
}

// Helper function to check if text_cursor is to be updated or not
fn update_text_contents(&self, value: DOMString, update_text_cursor: bool) {
let mut textinput = self.textinput.borrow_mut();

// Step 1
let old_value = textinput.get_content();

// Step 2
textinput.set_content(value, update_text_cursor);

// Step 3
self.value_dirty.set(true);

if old_value != textinput.get_content() {
// Step 4
textinput.clear_selection_to_limit(Direction::Forward, update_text_cursor);
}

self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);
}
}


Expand Down Expand Up @@ -429,7 +427,7 @@ impl VirtualMethods for HTMLTextAreaElement {
s.children_changed(mutation);
}
if !self.value_dirty.get() {
self.reset(false);
self.reset();
}
}

Expand Down Expand Up @@ -480,7 +478,7 @@ impl VirtualMethods for HTMLTextAreaElement {
self.super_type().unwrap().pop();

// https://html.spec.whatwg.org/multipage/#the-textarea-element:stack-of-open-elements
self.reset(false);
self.reset();
}
}

Expand Down
38 changes: 18 additions & 20 deletions components/script/textinput.rs
Expand Up @@ -188,7 +188,7 @@ impl<T: ClipboardProvider> TextInput<T> {
min_length: min_length,
selection_direction: selection_direction,
};
i.set_content(initial, false);
i.set_content(initial);
i
}

Expand Down Expand Up @@ -448,7 +448,9 @@ impl<T: ClipboardProvider> TextInput<T> {
return;
}


let col = self.lines[self.edit_point.line][..self.edit_point.index].chars().count();

self.edit_point.line = target_line as usize;
self.edit_point.index = len_of_first_n_chars(&self.lines[self.edit_point.line], col);
self.assert_ok_selection();
Expand Down Expand Up @@ -568,9 +570,9 @@ impl<T: ClipboardProvider> TextInput<T> {
}

/// 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, update_text_cursor: bool) {
pub fn clear_selection_to_limit(&mut self, direction: Direction) {
self.clear_selection();
self.adjust_horizontal_to_limit(direction, Selection::NotSelected, update_text_cursor);
self.adjust_horizontal_to_limit(direction, Selection::NotSelected);
}

pub fn adjust_horizontal_by_word(&mut self, direction: Direction, select: Selection) {
Expand Down Expand Up @@ -662,20 +664,18 @@ impl<T: ClipboardProvider> TextInput<T> {
self.perform_horizontal_adjustment(shift, select);
}

pub fn adjust_horizontal_to_limit(&mut self, direction: Direction, select: Selection, update_text_cursor: bool) {
pub fn adjust_horizontal_to_limit(&mut self, direction: Direction, select: Selection) {
if self.adjust_selection_for_horizontal_change(direction, select) {
return
}
if update_text_cursor {
match direction {
Direction::Backward => {
self.edit_point.line = 0;
self.edit_point.index = 0;
},
Direction::Forward => {
self.edit_point.line = &self.lines.len() - 1;
self.edit_point.index = (&self.lines[&self.lines.len() - 1]).len();
}
match direction {
Direction::Backward => {
self.edit_point.line = 0;
self.edit_point.index = 0;
},
Direction::Forward => {
self.edit_point.line = &self.lines.len() - 1;
self.edit_point.index = (&self.lines[&self.lines.len() - 1]).len();
}
}
}
Expand Down Expand Up @@ -765,12 +765,12 @@ impl<T: ClipboardProvider> TextInput<T> {
},
#[cfg(target_os = "macos")]
(None, Key::Up) if mods.contains(KeyModifiers::SUPER) => {
self.adjust_horizontal_to_limit(Direction::Backward, maybe_select, true);
self.adjust_horizontal_to_limit(Direction::Backward, maybe_select);
KeyReaction::RedrawSelection
},
#[cfg(target_os = "macos")]
(None, Key::Down) if mods.contains(KeyModifiers::SUPER) => {
self.adjust_horizontal_to_limit(Direction::Forward, maybe_select, true);
self.adjust_horizontal_to_limit(Direction::Forward, maybe_select);
KeyReaction::RedrawSelection
},
(None, Key::Left) if mods.contains(KeyModifiers::ALT) => {
Expand Down Expand Up @@ -871,7 +871,7 @@ impl<T: ClipboardProvider> TextInput<T> {

/// Set the current contents of the text input. If this is control supports multiple lines,
/// any \n encountered will be stripped and force a new logical line.
pub fn set_content(&mut self, content: DOMString, update_text_cursor: bool) {
pub fn set_content(&mut self, content: DOMString) {
self.lines = if self.multiline {
// https://html.spec.whatwg.org/multipage/#textarea-line-break-normalisation-transformation
content.replace("\r\n", "\n")
Expand All @@ -882,9 +882,7 @@ impl<T: ClipboardProvider> TextInput<T> {
vec!(content)
};

if update_text_cursor {
self.edit_point = self.edit_point.constrain_to(&self.lines);
}
self.edit_point = self.edit_point.constrain_to(&self.lines);

if let Some(origin) = self.selection_origin {
self.selection_origin = Some(origin.constrain_to(&self.lines));
Expand Down
8 changes: 2 additions & 6 deletions tests/unit/script/textinput.rs
Expand Up @@ -27,7 +27,7 @@ fn test_set_content_ignores_max_length() {
Lines::Single, DOMString::from(""), DummyClipboardContext::new(""), Some(1), None, SelectionDirection::None
);

textinput.set_content(DOMString::from("mozilla rocks"), true);
textinput.set_content(DOMString::from("mozilla rocks"));
assert_eq!(textinput.get_content(), DOMString::from("mozilla rocks"));
}

Expand Down Expand Up @@ -489,7 +489,7 @@ fn test_textinput_set_content() {
let mut textinput = text_input(Lines::Multiple, "abc\nde\nf");
assert_eq!(textinput.get_content(), "abc\nde\nf");

textinput.set_content(DOMString::from("abc\nf"), true);
textinput.set_content(DOMString::from("abc\nf"));
assert_eq!(textinput.get_content(), "abc\nf");

assert_eq!(textinput.edit_point().line, 0);
Expand Down Expand Up @@ -634,10 +634,6 @@ fn test_textinput_unicode_handling() {
fn test_selection_bounds() {
let mut textinput = text_input(Lines::Single, "abcdef");

assert_eq!(TextPoint { line: 0, index: 0 }, textinput.selection_origin_or_edit_point());
assert_eq!(TextPoint { line: 0, index: 0 }, textinput.selection_start());
assert_eq!(TextPoint { line: 0, index: 0 }, textinput.selection_end());

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());
Expand Down
6 changes: 0 additions & 6 deletions tests/wpt/metadata/MANIFEST.json
Expand Up @@ -330871,12 +330871,6 @@
{}
]
],
"html/semantics/forms/textfieldselection/defaultSelection.html": [
[
"/html/semantics/forms/textfieldselection/defaultSelection.html",
{}
]
],
"html/semantics/forms/textfieldselection/select-event.html": [
[
"/html/semantics/forms/textfieldselection/select-event.html",
Expand Down
@@ -0,0 +1,4 @@
[setSelectionRange.html]
[setSelectionRange on line boundaries]
expected: FAIL

This file was deleted.

0 comments on commit b517410

Please sign in to comment.