mirror of
https://github.com/servo/servo.git
synced 2025-06-14 19:34:29 +00:00
Makes int_getter macro, and uses -1 as default maxlength instead of maxint
This commit is contained in:
parent
d26c555e2a
commit
eecdfdf6c1
4 changed files with 51 additions and 11 deletions
|
@ -1090,6 +1090,27 @@ impl Element {
|
||||||
self.set_attribute(local_name, AttrValue::from_atomic_tokens(tokens));
|
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 {
|
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));
|
assert!(local_name.chars().all(|ch| !ch.is_ascii() || ch.to_ascii_lowercase() == ch));
|
||||||
let attribute = self.get_attribute(&ns!(), local_name);
|
let attribute = self.get_attribute(&ns!(), local_name);
|
||||||
|
|
|
@ -65,6 +65,7 @@ pub struct HTMLInputElement {
|
||||||
placeholder: DOMRefCell<DOMString>,
|
placeholder: DOMRefCell<DOMString>,
|
||||||
value_changed: Cell<bool>,
|
value_changed: Cell<bool>,
|
||||||
size: Cell<u32>,
|
size: Cell<u32>,
|
||||||
|
maxlength: Cell<i32>,
|
||||||
#[ignore_heap_size_of = "#7193"]
|
#[ignore_heap_size_of = "#7193"]
|
||||||
textinput: DOMRefCell<TextInput<ConstellationChan<ConstellationMsg>>>,
|
textinput: DOMRefCell<TextInput<ConstellationChan<ConstellationMsg>>>,
|
||||||
activation_state: DOMRefCell<InputActivationState>,
|
activation_state: DOMRefCell<InputActivationState>,
|
||||||
|
@ -104,6 +105,7 @@ impl InputActivationState {
|
||||||
}
|
}
|
||||||
|
|
||||||
static DEFAULT_INPUT_SIZE: u32 = 20;
|
static DEFAULT_INPUT_SIZE: u32 = 20;
|
||||||
|
static DEFAULT_MAX_LENGTH : i32 = -1;
|
||||||
|
|
||||||
impl HTMLInputElement {
|
impl HTMLInputElement {
|
||||||
fn new_inherited(localName: DOMString, prefix: Option<DOMString>, document: &Document) -> HTMLInputElement {
|
fn new_inherited(localName: DOMString, prefix: Option<DOMString>, document: &Document) -> HTMLInputElement {
|
||||||
|
@ -116,6 +118,7 @@ impl HTMLInputElement {
|
||||||
placeholder: DOMRefCell::new(DOMString::new()),
|
placeholder: DOMRefCell::new(DOMString::new()),
|
||||||
checked_changed: Cell::new(false),
|
checked_changed: Cell::new(false),
|
||||||
value_changed: Cell::new(false),
|
value_changed: Cell::new(false),
|
||||||
|
maxlength: Cell::new(DEFAULT_MAX_LENGTH),
|
||||||
size: Cell::new(DEFAULT_INPUT_SIZE),
|
size: Cell::new(DEFAULT_INPUT_SIZE),
|
||||||
textinput: DOMRefCell::new(TextInput::new(Single, DOMString::new(), chan, None)),
|
textinput: DOMRefCell::new(TextInput::new(Single, DOMString::new(), chan, None)),
|
||||||
activation_state: DOMRefCell::new(InputActivationState::new())
|
activation_state: DOMRefCell::new(InputActivationState::new())
|
||||||
|
@ -339,17 +342,16 @@ impl HTMLInputElementMethods for HTMLInputElement {
|
||||||
make_setter!(SetFormTarget, "formtarget");
|
make_setter!(SetFormTarget, "formtarget");
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/#dom-input-maxlength
|
// https://html.spec.whatwg.org/multipage/#dom-input-maxlength
|
||||||
fn MaxLength(&self) -> i32 {
|
make_int_getter!(MaxLength, "maxlength", DEFAULT_MAX_LENGTH);
|
||||||
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
|
// https://html.spec.whatwg.org/multipage/#dom-input-maxlength
|
||||||
fn SetMaxLength(&self, max_length: i32) {
|
fn SetMaxLength(&self, val: i32) {
|
||||||
if max_length > 0 {
|
self.maxlength.set(val);
|
||||||
self.textinput.borrow_mut().max_length = Some(max_length as usize)
|
|
||||||
|
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 script_task::{CommonScriptMsg, Runnable};
|
||||||
use selectors::states::*;
|
use selectors::states::*;
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::i32;
|
|
||||||
use string_cache::Atom;
|
use string_cache::Atom;
|
||||||
use textinput::{KeyReaction, Lines, TextInput};
|
use textinput::{KeyReaction, Lines, TextInput};
|
||||||
use util::str::DOMString;
|
use util::str::DOMString;
|
||||||
|
@ -90,7 +89,6 @@ impl<'a> RawLayoutHTMLTextAreaElementHelpers for &'a HTMLTextAreaElement {
|
||||||
|
|
||||||
static DEFAULT_COLS: u32 = 20;
|
static DEFAULT_COLS: u32 = 20;
|
||||||
static DEFAULT_ROWS: u32 = 2;
|
static DEFAULT_ROWS: u32 = 2;
|
||||||
static DEFAULT_MAX_LENGTH: i32 = i32::MAX;
|
|
||||||
|
|
||||||
impl HTMLTextAreaElement {
|
impl HTMLTextAreaElement {
|
||||||
fn new_inherited(localName: DOMString,
|
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_export]
|
||||||
macro_rules! make_uint_getter(
|
macro_rules! make_uint_getter(
|
||||||
($attr:ident, $htmlname:tt, $default:expr) => (
|
($attr:ident, $htmlname:tt, $default:expr) => (
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue