aboutsummaryrefslogtreecommitdiffstats
path: root/components
diff options
context:
space:
mode:
authorSam Gibson <sam@ifdown.net>2015-09-01 11:14:29 +1200
committerSam Gibson <sam@ifdown.net>2015-12-03 14:00:52 +1100
commiteecdfdf6c17b71b03a9d6404d83de482880aa26c (patch)
tree678602a59cecf6b601ef4b8b740450140463a449 /components
parentd26c555e2a2fe0e10b9237e1ccf48d96665828c7 (diff)
downloadservo-eecdfdf6c17b71b03a9d6404d83de482880aa26c.tar.gz
servo-eecdfdf6c17b71b03a9d6404d83de482880aa26c.zip
Makes int_getter macro, and uses -1 as default maxlength instead of maxint
Diffstat (limited to 'components')
-rw-r--r--components/script/dom/element.rs21
-rw-r--r--components/script/dom/htmlinputelement.rs20
-rw-r--r--components/script/dom/htmltextareaelement.rs2
-rw-r--r--components/script/dom/macros.rs19
4 files changed, 51 insertions, 11 deletions
diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs
index e7d6a47076f..3cec88f148f 100644
--- a/components/script/dom/element.rs
+++ b/components/script/dom/element.rs
@@ -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);
diff --git a/components/script/dom/htmlinputelement.rs b/components/script/dom/htmlinputelement.rs
index 554030c288a..c23f73f3220 100644
--- a/components/script/dom/htmlinputelement.rs
+++ b/components/script/dom/htmlinputelement.rs
@@ -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
}
}
diff --git a/components/script/dom/htmltextareaelement.rs b/components/script/dom/htmltextareaelement.rs
index 94057d4eee0..76d25092435 100644
--- a/components/script/dom/htmltextareaelement.rs
+++ b/components/script/dom/htmltextareaelement.rs
@@ -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,
diff --git a/components/script/dom/macros.rs b/components/script/dom/macros.rs
index 166b52d82d5..a292737ac47 100644
--- a/components/script/dom/macros.rs
+++ b/components/script/dom/macros.rs
@@ -27,6 +27,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) => (
fn $attr(&self) -> u32 {