Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for input element's maxlength attr #7323

Merged
merged 14 commits into from Dec 3, 2015

Makes int_getter macro, and uses -1 as default maxlength instead of m…

…axint
  • Loading branch information
samfoo committed Dec 3, 2015
commit eecdfdf6c17b71b03a9d6404d83de482880aa26c
@@ -1090,6 +1090,27 @@ impl Element {
self.set_attribute(local_name, AttrValue::from_atomic_tokens(tokens));
}

pub fn get_int_attribute(&self, local_name: &Atom, default: i32) -> i32 {
// TODO: Is this assert necessary?
assert!(local_name.chars().all(|ch| {
!ch.is_ascii() || ch.to_ascii_lowercase() == ch
}));
let attribute = self.get_attribute(&ns!(""), local_name);

match attribute {
Some(ref attribute) => {
match *attribute.r().value() {
AttrValue::Int(_, value) => value,
_ => panic!("Expected an AttrValue::Int: \
implement parse_plain_attribute"),
}
}
None => default,
}
}

// TODO: set_int_attribute(...)

pub fn get_uint_attribute(&self, local_name: &Atom, default: u32) -> u32 {
assert!(local_name.chars().all(|ch| !ch.is_ascii() || ch.to_ascii_lowercase() == ch));
let attribute = self.get_attribute(&ns!(), local_name);
@@ -65,6 +65,7 @@ pub struct HTMLInputElement {
placeholder: DOMRefCell<DOMString>,
value_changed: Cell<bool>,
size: Cell<u32>,
maxlength: Cell<i32>,
#[ignore_heap_size_of = "#7193"]
textinput: DOMRefCell<TextInput<ConstellationChan<ConstellationMsg>>>,
activation_state: DOMRefCell<InputActivationState>,
@@ -104,6 +105,7 @@ impl InputActivationState {
}

static DEFAULT_INPUT_SIZE: u32 = 20;
static DEFAULT_MAX_LENGTH : i32 = -1;

impl HTMLInputElement {
fn new_inherited(localName: DOMString, prefix: Option<DOMString>, document: &Document) -> HTMLInputElement {
@@ -116,6 +118,7 @@ impl HTMLInputElement {
placeholder: DOMRefCell::new(DOMString::new()),
checked_changed: Cell::new(false),
value_changed: Cell::new(false),
maxlength: Cell::new(DEFAULT_MAX_LENGTH),
size: Cell::new(DEFAULT_INPUT_SIZE),
textinput: DOMRefCell::new(TextInput::new(Single, DOMString::new(), chan, None)),
activation_state: DOMRefCell::new(InputActivationState::new())
@@ -339,17 +342,16 @@ impl HTMLInputElementMethods for HTMLInputElement {
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
}
}
make_int_getter!(MaxLength, "maxlength", DEFAULT_MAX_LENGTH);

// 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)
fn SetMaxLength(&self, val: i32) {
self.maxlength.set(val);

if val >= 0 {
self.textinput.borrow_mut().max_length = Some(val as usize)
} else {
self.textinput.borrow_mut().max_length = None
}
}

@@ -30,7 +30,6 @@ use script_task::ScriptTaskEventCategory::InputEvent;
use script_task::{CommonScriptMsg, Runnable};
use selectors::states::*;
use std::cell::Cell;
use std::i32;
use string_cache::Atom;
use textinput::{KeyReaction, Lines, TextInput};
use util::str::DOMString;
@@ -90,7 +89,6 @@ impl<'a> RawLayoutHTMLTextAreaElementHelpers for &'a HTMLTextAreaElement {

static DEFAULT_COLS: u32 = 20;
static DEFAULT_ROWS: u32 = 2;
static DEFAULT_MAX_LENGTH: i32 = i32::MAX;

impl HTMLTextAreaElement {
fn new_inherited(localName: DOMString,
@@ -26,6 +26,25 @@ macro_rules! make_bool_getter(
);
);

#[macro_export]
macro_rules! make_int_getter(
($attr:ident, $htmlname:expr, $default:expr) => (
fn $attr(&self) -> i32 {
use dom::bindings::codegen::InheritTypes::ElementCast;
use string_cache::Atom;
let element = ElementCast::from_ref(self);
// FIXME(pcwalton): Do this at compile time, not runtime.
element.get_int_attribute(&Atom::from_slice($htmlname), $default)
}
);
($attr:ident, $htmlname:expr) => {
make_int_getter!($attr, $htmlname, 0);
};
($attr:ident) => {
make_int_getter!($attr, to_lower!(stringify!($attr)));
}
);

#[macro_export]
macro_rules! make_uint_getter(
($attr:ident, $htmlname:tt, $default:expr) => (
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.