Adds support for input element's maxlength attr

servo/servo#7320
servo/servo#7004
This commit is contained in:
Sam Gibson 2015-08-23 16:11:04 +12:00
parent 2be60be062
commit d26c555e2a
6 changed files with 196 additions and 13 deletions

View file

@ -8,8 +8,8 @@ use dom::attr::{Attr, AttrValue};
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::AttrBinding::AttrMethods;
use dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use dom::bindings::codegen::Bindings::HTMLInputElementBinding;
use dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods;
use dom::bindings::codegen::Bindings::HTMLInputElementBinding;
use dom::bindings::codegen::Bindings::KeyboardEventBinding::KeyboardEventMethods;
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::Castable;
@ -32,6 +32,7 @@ use msg::constellation_msg::ScriptMsg as ConstellationMsg;
use selectors::states::*;
use std::borrow::ToOwned;
use std::cell::Cell;
use std::i32;
use string_cache::Atom;
use textinput::KeyReaction::{DispatchInput, Nothing, RedrawSelection, TriggerDefaultAction};
use textinput::Lines::Single;
@ -116,7 +117,7 @@ impl HTMLInputElement {
checked_changed: Cell::new(false),
value_changed: Cell::new(false),
size: Cell::new(DEFAULT_INPUT_SIZE),
textinput: DOMRefCell::new(TextInput::new(Single, DOMString::new(), chan)),
textinput: DOMRefCell::new(TextInput::new(Single, DOMString::new(), chan, None)),
activation_state: DOMRefCell::new(InputActivationState::new())
}
}
@ -337,6 +338,21 @@ impl HTMLInputElementMethods for HTMLInputElement {
// https://html.spec.whatwg.org/multipage/#dom-input-formtarget
make_setter!(SetFormTarget, "formtarget");
// https://html.spec.whatwg.org/multipage/#dom-input-maxlength
fn MaxLength(&self) -> i32 {
match self.textinput.borrow().max_length {
Some(max_length) => max_length as i32,
None => i32::MAX
}
}
// https://html.spec.whatwg.org/multipage/#dom-input-maxlength
fn SetMaxLength(&self, max_length: i32) {
if max_length > 0 {
self.textinput.borrow_mut().max_length = Some(max_length as usize)
}
}
// https://html.spec.whatwg.org/multipage/#dom-input-indeterminate
fn Indeterminate(&self) -> bool {
self.upcast::<Element>().get_state().contains(IN_INDETERMINATE_STATE)
@ -511,6 +527,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() {
&atom!("disabled") => {
let disabled_state = match mutation {
@ -581,6 +598,18 @@ impl VirtualMethods for HTMLInputElement {
self.radio_group_updated(
mutation.new_value(attr).as_ref().map(|name| name.as_atom()));
},
&atom!("maxlength") => {
match *attr.value() {
AttrValue::Int(_, value) => {
if value < 0 {
self.textinput.borrow_mut().max_length = None
} else {
self.textinput.borrow_mut().max_length = Some(value as usize)
}
},
_ => panic!("Expected an AttrValue::UInt"),
}
}
&atom!("placeholder") => {
// FIXME(ajeffrey): Should we do in-place mutation of the placeholder?
let mut placeholder = self.placeholder.borrow_mut();
@ -599,6 +628,7 @@ impl VirtualMethods for HTMLInputElement {
&atom!("name") => AttrValue::from_atomic(value),
&atom!("size") => AttrValue::from_limited_u32(value, DEFAULT_INPUT_SIZE),
&atom!("type") => AttrValue::from_atomic(value),
&atom!("maxlength") => AttrValue::from_i32(value, i32::MAX),
_ => self.super_type().unwrap().parse_plain_attribute(name, value),
}
}