Relayout text input elements on blur

This commit is contained in:
Manish Goregaokar 2015-12-15 17:52:09 +05:30
parent 1b0053f8b1
commit 23e7dfa57b
5 changed files with 33 additions and 6 deletions

View file

@ -957,9 +957,10 @@ impl<'ln> ThreadSafeLayoutNode<'ln> for ServoThreadSafeLayoutNode<'ln> {
}; };
if let Some(area) = this.downcast::<HTMLTextAreaElement>() { if let Some(area) = this.downcast::<HTMLTextAreaElement>() {
let insertion_point = unsafe { area.get_absolute_insertion_point_for_layout() }; if let Some(insertion_point) = unsafe { area.get_absolute_insertion_point_for_layout() } {
let text = unsafe { area.get_value_for_layout() }; let text = unsafe { area.get_value_for_layout() };
return Some(CharIndex(search_index(insertion_point, text.char_indices()))); return Some(CharIndex(search_index(insertion_point, text.char_indices())));
}
} }
if let Some(input) = this.downcast::<HTMLInputElement>() { if let Some(input) = this.downcast::<HTMLInputElement>() {
let insertion_point_index = unsafe { input.get_insertion_point_index_for_layout() }; let insertion_point_index = unsafe { input.get_insertion_point_index_for_layout() };

View file

@ -72,6 +72,7 @@ use dom::touchevent::TouchEvent;
use dom::touchlist::TouchList; use dom::touchlist::TouchList;
use dom::treewalker::TreeWalker; use dom::treewalker::TreeWalker;
use dom::uievent::UIEvent; use dom::uievent::UIEvent;
use dom::virtualmethods::{VirtualMethods, vtable_for};
use dom::window::{ReflowReason, Window}; use dom::window::{ReflowReason, Window};
use euclid::point::Point2D; use euclid::point::Point2D;
use html5ever::tree_builder::{LimitedQuirks, NoQuirks, Quirks, QuirksMode}; use html5ever::tree_builder::{LimitedQuirks, NoQuirks, Quirks, QuirksMode};
@ -593,6 +594,8 @@ impl Document {
if let Some(ref elem) = self.focused.get() { if let Some(ref elem) = self.focused.get() {
elem.set_focus_state(false); elem.set_focus_state(false);
let node = vtable_for(&elem.upcast::<Node>());
node.handle_blur();
} }
self.focused.set(self.possibly_focused.get().r()); self.focused.set(self.possibly_focused.get().r());

View file

@ -208,6 +208,9 @@ impl LayoutHTMLInputElementHelpers for LayoutJS<HTMLInputElement> {
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
#[allow(unsafe_code)] #[allow(unsafe_code)]
unsafe fn get_insertion_point_index_for_layout(self) -> Option<isize> { unsafe fn get_insertion_point_index_for_layout(self) -> Option<isize> {
if !(*self.unsafe_get()).upcast::<Element>().get_focus_state() {
return None;
}
match (*self.unsafe_get()).input_type.get() { match (*self.unsafe_get()).input_type.get() {
InputType::InputText => { InputType::InputText => {
let raw = self.get_value_for_layout(); let raw = self.get_value_for_layout();
@ -716,6 +719,13 @@ impl VirtualMethods for HTMLInputElement {
} }
} }
} }
fn handle_blur(&self) {
if let Some(s) = self.super_type() {
s.handle_blur();
}
self.force_relayout();
}
} }
impl FormControl for HTMLInputElement {} impl FormControl for HTMLInputElement {}

View file

@ -45,7 +45,7 @@ pub trait LayoutHTMLTextAreaElementHelpers {
#[allow(unsafe_code)] #[allow(unsafe_code)]
unsafe fn get_value_for_layout(self) -> String; unsafe fn get_value_for_layout(self) -> String;
#[allow(unsafe_code)] #[allow(unsafe_code)]
unsafe fn get_absolute_insertion_point_for_layout(self) -> usize; unsafe fn get_absolute_insertion_point_for_layout(self) -> Option<usize>;
#[allow(unsafe_code)] #[allow(unsafe_code)]
fn get_cols(self) -> u32; fn get_cols(self) -> u32;
#[allow(unsafe_code)] #[allow(unsafe_code)]
@ -61,8 +61,13 @@ impl LayoutHTMLTextAreaElementHelpers for LayoutJS<HTMLTextAreaElement> {
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
#[allow(unsafe_code)] #[allow(unsafe_code)]
unsafe fn get_absolute_insertion_point_for_layout(self) -> usize { unsafe fn get_absolute_insertion_point_for_layout(self) -> Option<usize> {
(*self.unsafe_get()).textinput.borrow_for_layout().get_absolute_insertion_point() if (*self.unsafe_get()).upcast::<Element>().get_focus_state() {
Some((*self.unsafe_get()).textinput.borrow_for_layout()
.get_absolute_insertion_point())
} else {
None
}
} }
#[allow(unsafe_code)] #[allow(unsafe_code)]

View file

@ -102,6 +102,14 @@ pub trait VirtualMethods {
} }
} }
/// Called when a previously focused element is no longer focused.
/// Use this to trigger relayouts
fn handle_blur(&self) {
if let Some(s) = self.super_type() {
s.handle_blur();
}
}
/// https://dom.spec.whatwg.org/#concept-node-adopt-ext /// https://dom.spec.whatwg.org/#concept-node-adopt-ext
fn adopting_steps(&self, old_doc: &Document) { fn adopting_steps(&self, old_doc: &Document) {
if let Some(ref s) = self.super_type() { if let Some(ref s) = self.super_type() {