Make LayoutHTMLInputElementHelpers::value_for_layout return a cow

This commit is contained in:
Anthony Ramine 2020-03-31 14:30:18 +02:00
parent 3e875ce3eb
commit e1e913d33c
2 changed files with 31 additions and 26 deletions

View file

@ -67,7 +67,7 @@ use profile_traits::ipc;
use script_layout_interface::rpc::TextIndexResponse; use script_layout_interface::rpc::TextIndexResponse;
use script_traits::ScriptToConstellationChan; use script_traits::ScriptToConstellationChan;
use servo_atoms::Atom; use servo_atoms::Atom;
use std::borrow::ToOwned; use std::borrow::Cow;
use std::cell::Cell; use std::cell::Cell;
use std::ops::Range; use std::ops::Range;
use std::ptr::NonNull; use std::ptr::NonNull;
@ -705,9 +705,8 @@ impl HTMLInputElement {
} }
} }
pub trait LayoutHTMLInputElementHelpers { pub trait LayoutHTMLInputElementHelpers<'dom> {
#[allow(unsafe_code)] fn value_for_layout(self) -> Cow<'dom, str>;
unsafe fn value_for_layout(self) -> String;
#[allow(unsafe_code)] #[allow(unsafe_code)]
unsafe fn size_for_layout(self) -> u32; unsafe fn size_for_layout(self) -> u32;
#[allow(unsafe_code)] #[allow(unsafe_code)]
@ -726,41 +725,47 @@ unsafe fn get_raw_textinput_value(input: LayoutDom<HTMLInputElement>) -> DOMStri
.get_content() .get_content()
} }
impl LayoutHTMLInputElementHelpers for LayoutDom<'_, HTMLInputElement> { impl<'dom> LayoutHTMLInputElementHelpers<'dom> for LayoutDom<'dom, HTMLInputElement> {
#[allow(unsafe_code)] #[allow(unsafe_code)]
unsafe fn value_for_layout(self) -> String { fn value_for_layout(self) -> Cow<'dom, str> {
#[allow(unsafe_code)] fn get_raw_attr_value<'dom>(
unsafe fn get_raw_attr_value( input: LayoutDom<'dom, HTMLInputElement>,
input: LayoutDom<'_, HTMLInputElement>, default: &'static str,
default: &str, ) -> Cow<'dom, str> {
) -> String { unsafe {
let elem = input.upcast::<Element>(); input
let value = (*elem.unsafe_get()) .upcast::<Element>()
.get_attr_val_for_layout(&ns!(), &local_name!("value")) .unsafe_get()
.unwrap_or(default); .get_attr_val_for_layout(&ns!(), &local_name!("value"))
String::from(value) .unwrap_or(default)
.into()
}
} }
match (*self.unsafe_get()).input_type() { let placeholder = unsafe { &**self.unsafe_get().placeholder.borrow_for_layout() };
InputType::Checkbox | InputType::Radio => String::new(), match unsafe { self.unsafe_get().input_type() } {
InputType::File | InputType::Image => String::new(), InputType::Checkbox | InputType::Radio => "".into(),
InputType::File | InputType::Image => "".into(),
InputType::Button => get_raw_attr_value(self, ""), InputType::Button => get_raw_attr_value(self, ""),
InputType::Submit => get_raw_attr_value(self, DEFAULT_SUBMIT_VALUE), InputType::Submit => get_raw_attr_value(self, DEFAULT_SUBMIT_VALUE),
InputType::Reset => get_raw_attr_value(self, DEFAULT_RESET_VALUE), InputType::Reset => get_raw_attr_value(self, DEFAULT_RESET_VALUE),
InputType::Password => { InputType::Password => {
let text = get_raw_textinput_value(self); let text = unsafe { get_raw_textinput_value(self) };
if !text.is_empty() { if !text.is_empty() {
text.chars().map(|_| PASSWORD_REPLACEMENT_CHAR).collect() text.chars()
.map(|_| PASSWORD_REPLACEMENT_CHAR)
.collect::<String>()
.into()
} else { } else {
String::from((*self.unsafe_get()).placeholder.borrow_for_layout().clone()) placeholder.into()
} }
}, },
_ => { _ => {
let text = get_raw_textinput_value(self); let text = unsafe { get_raw_textinput_value(self) };
if !text.is_empty() { if !text.is_empty() {
String::from(text) text.into()
} else { } else {
String::from((*self.unsafe_get()).placeholder.borrow_for_layout().clone()) placeholder.into()
} }
}, },
} }

View file

@ -1463,7 +1463,7 @@ impl<'dom> LayoutNodeHelpers<'dom> for LayoutDom<'dom, Node> {
} }
if let Some(input) = self.downcast::<HTMLInputElement>() { if let Some(input) = self.downcast::<HTMLInputElement>() {
return unsafe { input.value_for_layout() }; return input.value_for_layout().into_owned();
} }
if let Some(area) = self.downcast::<HTMLTextAreaElement>() { if let Some(area) = self.downcast::<HTMLTextAreaElement>() {