From 87e7b1ee7a7e59900eddc169e6c1cb7484cd2b2d Mon Sep 17 00:00:00 2001 From: paavininanda Date: Wed, 21 Feb 2018 01:29:33 +0530 Subject: [PATCH] Correct default selectionStart and selectionEnd --- components/script/dom/htmlinputelement.rs | 30 ++++++++++--------- components/script/dom/htmltextareaelement.rs | 6 ++-- components/script/textinput.rs | 3 -- tests/unit/script/textinput.rs | 7 ++++- tests/wpt/metadata/MANIFEST.json | 6 ++++ .../setSelectionRange.html.ini | 4 --- .../textfieldselection/defaultSelection.html | 28 +++++++++++++++++ 7 files changed, 58 insertions(+), 26 deletions(-) delete mode 100644 tests/wpt/metadata/html/semantics/forms/textfieldselection/setSelectionRange.html.ini create mode 100644 tests/wpt/web-platform-tests/html/semantics/forms/textfieldselection/defaultSelection.html diff --git a/components/script/dom/htmlinputelement.rs b/components/script/dom/htmlinputelement.rs index 353117c39a0e..030d6278e0ef 100755 --- a/components/script/dom/htmlinputelement.rs +++ b/components/script/dom/htmlinputelement.rs @@ -550,18 +550,24 @@ impl HTMLInputElementMethods for HTMLInputElement { } // https://html.spec.whatwg.org/multipage/#dom-input-value - fn SetValue(&self, value: DOMString) -> ErrorResult { + fn SetValue(&self, mut value: DOMString) -> ErrorResult { 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(); + self.sanitize_value(&mut value); + + let mut textinput = self.textinput.borrow_mut(); + // Step 5. - if *self.textinput.borrow().single_line_content() != old_value { - self.textinput.borrow_mut().clear_selection_to_limit(Direction::Forward); + if *textinput.single_line_content() != value { + // Steps 1-2 + textinput.set_content(value); + + // Step 5. + textinput.clear_selection_to_limit(Direction::Forward); } } ValueMode::Default | @@ -935,8 +941,7 @@ impl HTMLInputElement { InputType::Image => (), _ => () } - self.SetValue(self.DefaultValue()) - .expect("Failed to reset input value to default."); + self.textinput.borrow_mut().set_content(self.DefaultValue()); self.value_dirty.set(false); self.upcast::().dirty(NodeDamage::OtherNodeDamage); } @@ -1093,7 +1098,6 @@ 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 { @@ -1194,11 +1198,11 @@ impl VirtualMethods for HTMLInputElement { let mut textinput = self.textinput.borrow_mut(); let mut value = textinput.single_line_content().clone(); self.sanitize_value(&mut value); - textinput.set_content(value, true); + textinput.set_content(value); // Steps 7-9 if !previously_selectable && self.selection_api_applies() { - self.textinput.borrow_mut().clear_selection_to_limit(Direction::Backward); + textinput.clear_selection_to_limit(Direction::Backward); } }, AttributeMutation::Removed => { @@ -1222,9 +1226,7 @@ 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.map_or(DOMString::new(), DOMString::from)); + self.textinput.borrow_mut().set_content(value); self.update_placeholder_shown_state(); }, &local_name!("name") if self.input_type() == InputType::Radio => { diff --git a/components/script/dom/htmltextareaelement.rs b/components/script/dom/htmltextareaelement.rs index 58e017de93ad..2798305da1d5 100755 --- a/components/script/dom/htmltextareaelement.rs +++ b/components/script/dom/htmltextareaelement.rs @@ -248,7 +248,6 @@ impl HTMLTextAreaElementMethods for HTMLTextAreaElement { // Step 1 let old_value = textinput.get_content(); - let old_selection = textinput.selection_origin; // Step 2 textinput.set_content(value); @@ -259,8 +258,6 @@ impl HTMLTextAreaElementMethods for HTMLTextAreaElement { if old_value != textinput.get_content() { // Step 4 textinput.clear_selection_to_limit(Direction::Forward); - } else { - textinput.selection_origin = old_selection; } self.upcast::().dirty(NodeDamage::OtherNodeDamage); @@ -327,7 +324,8 @@ impl HTMLTextAreaElementMethods for HTMLTextAreaElement { impl HTMLTextAreaElement { pub fn reset(&self) { // https://html.spec.whatwg.org/multipage/#the-textarea-element:concept-form-reset-control - self.SetValue(self.DefaultValue()); + let mut textinput = self.textinput.borrow_mut(); + textinput.set_content(self.DefaultValue()); self.value_dirty.set(false); } diff --git a/components/script/textinput.rs b/components/script/textinput.rs index 3f26873ca24a..1133c9cdd1c0 100644 --- a/components/script/textinput.rs +++ b/components/script/textinput.rs @@ -448,9 +448,7 @@ impl TextInput { 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(); @@ -887,7 +885,6 @@ impl TextInput { if let Some(origin) = self.selection_origin { self.selection_origin = Some(origin.constrain_to(&self.lines)); } - self.assert_ok_selection(); } diff --git a/tests/unit/script/textinput.rs b/tests/unit/script/textinput.rs index ec4441dfeba8..3880d641d851 100644 --- a/tests/unit/script/textinput.rs +++ b/tests/unit/script/textinput.rs @@ -494,10 +494,11 @@ fn test_textinput_set_content() { assert_eq!(textinput.edit_point().line, 0); assert_eq!(textinput.edit_point().index, 0); + textinput.adjust_horizontal(3, Selection::Selected); assert_eq!(textinput.edit_point().line, 0); assert_eq!(textinput.edit_point().index, 3); - textinput.set_content(DOMString::from("de"), true); + textinput.set_content(DOMString::from("de")); assert_eq!(textinput.get_content(), "de"); assert_eq!(textinput.edit_point().line, 0); assert_eq!(textinput.edit_point().index, 2); @@ -634,6 +635,10 @@ 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()); diff --git a/tests/wpt/metadata/MANIFEST.json b/tests/wpt/metadata/MANIFEST.json index f57cf4246e30..cdb7b635b8f1 100644 --- a/tests/wpt/metadata/MANIFEST.json +++ b/tests/wpt/metadata/MANIFEST.json @@ -330871,6 +330871,12 @@ {} ] ], + "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", diff --git a/tests/wpt/metadata/html/semantics/forms/textfieldselection/setSelectionRange.html.ini b/tests/wpt/metadata/html/semantics/forms/textfieldselection/setSelectionRange.html.ini deleted file mode 100644 index e5a3a19ddac9..000000000000 --- a/tests/wpt/metadata/html/semantics/forms/textfieldselection/setSelectionRange.html.ini +++ /dev/null @@ -1,4 +0,0 @@ -[setSelectionRange.html] - [setSelectionRange on line boundaries] - expected: FAIL - diff --git a/tests/wpt/web-platform-tests/html/semantics/forms/textfieldselection/defaultSelection.html b/tests/wpt/web-platform-tests/html/semantics/forms/textfieldselection/defaultSelection.html new file mode 100644 index 000000000000..be965bf5cf8e --- /dev/null +++ b/tests/wpt/web-platform-tests/html/semantics/forms/textfieldselection/defaultSelection.html @@ -0,0 +1,28 @@ + + + + + + + +