Skip to content

Commit

Permalink
Makes int_getter macro, and uses -1 as default maxlength instead of m…
Browse files Browse the repository at this point in the history
…axint
  • Loading branch information
samfoo committed Dec 3, 2015
1 parent d26c555 commit eecdfdf
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
21 changes: 21 additions & 0 deletions components/script/dom/element.rs
Expand Up @@ -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);
Expand Down
20 changes: 11 additions & 9 deletions components/script/dom/htmlinputelement.rs
Expand Up @@ -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>,
Expand Down Expand Up @@ -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 {
Expand All @@ -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())
Expand Down Expand Up @@ -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
}
}

Expand Down
2 changes: 0 additions & 2 deletions components/script/dom/htmltextareaelement.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions components/script/dom/macros.rs
Expand Up @@ -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) => (
Expand Down

0 comments on commit eecdfdf

Please sign in to comment.