mirror of
https://github.com/servo/servo.git
synced 2025-08-02 20:20:14 +01:00
Auto merge of #5923 - nox:limited-unsigned-long, r=jdm
<!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/5923) <!-- Reviewable:end -->
This commit is contained in:
commit
19744984da
15 changed files with 106 additions and 483 deletions
|
@ -54,8 +54,25 @@ impl AttrValue {
|
|||
AttrValue::TokenList(tokens, atoms)
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#reflecting-content-attributes-in-idl-attributes:idl-unsigned-long
|
||||
pub fn from_u32(string: DOMString, default: u32) -> AttrValue {
|
||||
let result = parse_unsigned_integer(string.chars()).unwrap_or(default);
|
||||
let result = if result > 2147483647 {
|
||||
default
|
||||
} else {
|
||||
result
|
||||
};
|
||||
AttrValue::UInt(string, result)
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#limited-to-only-non-negative-numbers-greater-than-zero
|
||||
pub fn from_limited_u32(string: DOMString, default: u32) -> AttrValue {
|
||||
let result = parse_unsigned_integer(string.chars()).unwrap_or(default);
|
||||
let result = if result == 0 || result > 2147483647 {
|
||||
default
|
||||
} else {
|
||||
result
|
||||
};
|
||||
AttrValue::UInt(string, result)
|
||||
}
|
||||
|
||||
|
|
|
@ -710,7 +710,7 @@ pub trait AttributeHandlers {
|
|||
fn get_tokenlist_attribute(self, local_name: &Atom) -> Vec<Atom>;
|
||||
fn set_tokenlist_attribute(self, local_name: &Atom, value: DOMString);
|
||||
fn set_atomic_tokenlist_attribute(self, local_name: &Atom, tokens: Vec<Atom>);
|
||||
fn get_uint_attribute(self, local_name: &Atom) -> u32;
|
||||
fn get_uint_attribute(self, local_name: &Atom, default: u32) -> u32;
|
||||
fn set_uint_attribute(self, local_name: &Atom, value: u32);
|
||||
}
|
||||
|
||||
|
@ -965,7 +965,7 @@ impl<'a> AttributeHandlers for JSRef<'a, Element> {
|
|||
self.set_attribute(local_name, AttrValue::from_atomic_tokens(tokens));
|
||||
}
|
||||
|
||||
fn get_uint_attribute(self, local_name: &Atom) -> u32 {
|
||||
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
|
||||
}));
|
||||
|
@ -978,7 +978,7 @@ impl<'a> AttributeHandlers for JSRef<'a, Element> {
|
|||
implement parse_plain_attribute"),
|
||||
}
|
||||
}
|
||||
None => 0,
|
||||
None => default,
|
||||
}
|
||||
}
|
||||
fn set_uint_attribute(self, local_name: &Atom, value: u32) {
|
||||
|
|
|
@ -242,10 +242,8 @@ impl<'a> HTMLInputElementMethods for JSRef<'a, HTMLInputElement> {
|
|||
make_bool_setter!(SetReadOnly, "readonly");
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-input-size
|
||||
make_uint_getter!(Size);
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-input-size
|
||||
make_uint_setter!(SetSize, "size");
|
||||
make_uint_getter!(Size, "size", DEFAULT_INPUT_SIZE);
|
||||
make_limited_uint_setter!(SetSize, "size", DEFAULT_INPUT_SIZE);
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-input-type
|
||||
make_enumerated_getter!(Type, "text", ("hidden") | ("search") | ("tel") |
|
||||
|
@ -568,7 +566,7 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLInputElement> {
|
|||
|
||||
fn parse_plain_attribute(&self, name: &Atom, value: DOMString) -> AttrValue {
|
||||
match name {
|
||||
&atom!("size") => AttrValue::from_u32(value, DEFAULT_INPUT_SIZE),
|
||||
&atom!("size") => AttrValue::from_limited_u32(value, DEFAULT_INPUT_SIZE),
|
||||
_ => self.super_type().unwrap().parse_plain_attribute(name, value),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use dom::attr::{Attr, AttrHelpers};
|
||||
use dom::attr::{Attr, AttrHelpers, AttrValue};
|
||||
use dom::bindings::codegen::Bindings::HTMLTableCellElementBinding::HTMLTableCellElementMethods;
|
||||
use dom::bindings::codegen::InheritTypes::{HTMLElementCast, HTMLTableCellElementDerived};
|
||||
use dom::bindings::js::JSRef;
|
||||
use dom::document::Document;
|
||||
|
@ -12,9 +13,15 @@ use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
|
|||
use dom::node::NodeTypeId;
|
||||
use dom::virtualmethods::VirtualMethods;
|
||||
|
||||
use cssparser::RGBA;
|
||||
use util::str::{self, DOMString, LengthOrPercentageOrAuto};
|
||||
|
||||
use cssparser::RGBA;
|
||||
use string_cache::Atom;
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::cmp::max;
|
||||
|
||||
const DEFAULT_COLSPAN: u32 = 1;
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
#[jstraceable]
|
||||
|
@ -60,6 +67,12 @@ impl HTMLTableCellElement {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> HTMLTableCellElementMethods for JSRef<'a, HTMLTableCellElement> {
|
||||
// https://html.spec.whatwg.org/multipage/#dom-tdth-colspan
|
||||
make_uint_getter!(ColSpan, "colspan", DEFAULT_COLSPAN);
|
||||
make_uint_setter!(SetColSpan, "colspan");
|
||||
}
|
||||
|
||||
pub trait HTMLTableCellElementHelpers {
|
||||
fn get_background_color(&self) -> Option<RGBA>;
|
||||
fn get_colspan(&self) -> Option<u32>;
|
||||
|
@ -96,8 +109,13 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLTableCellElement> {
|
|||
self.background_color.set(str::parse_legacy_color(&attr.value()).ok())
|
||||
}
|
||||
&atom!("colspan") => {
|
||||
self.colspan.set(str::parse_unsigned_integer(attr.value().chars()));
|
||||
}
|
||||
match *attr.value() {
|
||||
AttrValue::UInt(_, colspan) => {
|
||||
self.colspan.set(Some(max(DEFAULT_COLSPAN, colspan)))
|
||||
},
|
||||
_ => unreachable!(),
|
||||
}
|
||||
},
|
||||
&atom!("width") => self.width.set(str::parse_length(&attr.value())),
|
||||
_ => ()
|
||||
}
|
||||
|
@ -115,5 +133,11 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLTableCellElement> {
|
|||
_ => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_plain_attribute(&self, local_name: &Atom, value: DOMString) -> AttrValue {
|
||||
match local_name {
|
||||
&atom!("colspan") => AttrValue::from_u32(value, DEFAULT_COLSPAN),
|
||||
_ => self.super_type().unwrap().parse_plain_attribute(local_name, value),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -114,10 +114,8 @@ impl<'a> HTMLTextAreaElementMethods for JSRef<'a, HTMLTextAreaElement> {
|
|||
// constraints
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-textarea-cols
|
||||
make_uint_getter!(Cols);
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-textarea-cols
|
||||
make_uint_setter!(SetCols, "cols");
|
||||
make_uint_getter!(Cols, "cols", DEFAULT_COLS);
|
||||
make_limited_uint_setter!(SetCols, "cols", DEFAULT_COLS);
|
||||
|
||||
// https://www.whatwg.org/html/#dom-fe-disabled
|
||||
make_bool_getter!(Disabled);
|
||||
|
@ -150,10 +148,8 @@ impl<'a> HTMLTextAreaElementMethods for JSRef<'a, HTMLTextAreaElement> {
|
|||
make_bool_setter!(SetRequired, "required");
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-textarea-rows
|
||||
make_uint_getter!(Rows);
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-textarea-rows
|
||||
make_uint_setter!(SetRows, "rows");
|
||||
make_uint_getter!(Rows, "rows", DEFAULT_ROWS);
|
||||
make_limited_uint_setter!(SetRows, "rows", DEFAULT_ROWS);
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-textarea-wrap
|
||||
make_getter!(Wrap);
|
||||
|
@ -310,8 +306,8 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLTextAreaElement> {
|
|||
|
||||
fn parse_plain_attribute(&self, name: &Atom, value: DOMString) -> AttrValue {
|
||||
match name {
|
||||
&atom!("cols") => AttrValue::from_u32(value, DEFAULT_COLS),
|
||||
&atom!("rows") => AttrValue::from_u32(value, DEFAULT_ROWS),
|
||||
&atom!("cols") => AttrValue::from_limited_u32(value, DEFAULT_COLS),
|
||||
&atom!("rows") => AttrValue::from_limited_u32(value, DEFAULT_ROWS),
|
||||
_ => self.super_type().unwrap().parse_plain_attribute(name, value),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ macro_rules! make_bool_getter(
|
|||
|
||||
#[macro_export]
|
||||
macro_rules! make_uint_getter(
|
||||
( $attr:ident, $htmlname:expr ) => (
|
||||
($attr:ident, $htmlname:expr, $default:expr) => (
|
||||
fn $attr(self) -> u32 {
|
||||
use dom::element::{Element, AttributeHandlers};
|
||||
use dom::bindings::codegen::InheritTypes::ElementCast;
|
||||
|
@ -47,9 +47,12 @@ macro_rules! make_uint_getter(
|
|||
use std::ascii::AsciiExt;
|
||||
let element: JSRef<Element> = ElementCast::from_ref(self);
|
||||
// FIXME(pcwalton): Do this at compile time, not runtime.
|
||||
element.get_uint_attribute(&Atom::from_slice($htmlname))
|
||||
element.get_uint_attribute(&Atom::from_slice($htmlname), $default)
|
||||
}
|
||||
);
|
||||
($attr:ident, $htmlname:expr) => {
|
||||
make_uint_getter!($attr, $htmlname, 0);
|
||||
};
|
||||
($attr:ident) => {
|
||||
make_uint_getter!($attr, to_lower!(stringify!($attr)));
|
||||
}
|
||||
|
@ -152,15 +155,51 @@ macro_rules! make_bool_setter(
|
|||
|
||||
#[macro_export]
|
||||
macro_rules! make_uint_setter(
|
||||
( $attr:ident, $htmlname:expr ) => (
|
||||
($attr:ident, $htmlname:expr, $default:expr) => (
|
||||
fn $attr(self, value: u32) {
|
||||
use dom::element::{Element, AttributeHandlers};
|
||||
use dom::bindings::codegen::InheritTypes::ElementCast;
|
||||
let value = if value > 2147483647 {
|
||||
$default
|
||||
} else {
|
||||
value
|
||||
};
|
||||
let element: JSRef<Element> = ElementCast::from_ref(self);
|
||||
// FIXME(pcwalton): Do this at compile time, not at runtime.
|
||||
element.set_uint_attribute(&Atom::from_slice($htmlname), value)
|
||||
}
|
||||
);
|
||||
($attr:ident, $htmlname:expr) => {
|
||||
make_uint_setter!($attr, $htmlname, 0);
|
||||
};
|
||||
);
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! make_limited_uint_setter(
|
||||
($attr:ident, $htmlname:expr, $default:expr) => (
|
||||
fn $attr(self, value: u32) -> $crate::dom::bindings::error::ErrorResult {
|
||||
use dom::element::AttributeHandlers;
|
||||
use dom::bindings::codegen::InheritTypes::ElementCast;
|
||||
use string_cache::Atom;
|
||||
let value = if value == 0 {
|
||||
return Err($crate::dom::bindings::error::Error::IndexSize);
|
||||
} else if value > 2147483647 {
|
||||
$default
|
||||
} else {
|
||||
value
|
||||
};
|
||||
let element = ElementCast::from_ref(self);
|
||||
// FIXME(pcwalton): Do this at compile time, not runtime.
|
||||
element.set_uint_attribute(&Atom::from_slice($htmlname), value);
|
||||
Ok(())
|
||||
}
|
||||
);
|
||||
($attr:ident, $htmlname:expr) => {
|
||||
make_limited_uint_setter!($attr, $htmlname, 1);
|
||||
};
|
||||
($attr:ident) => {
|
||||
make_limited_uint_setter!($attr, to_lower!(stringify!($attr)));
|
||||
};
|
||||
);
|
||||
|
||||
/// For use on non-jsmanaged types
|
||||
|
|
|
@ -34,7 +34,8 @@ interface HTMLInputElement : HTMLElement {
|
|||
attribute DOMString placeholder;
|
||||
attribute boolean readOnly;
|
||||
// attribute boolean required;
|
||||
attribute unsigned long size;
|
||||
[SetterThrows]
|
||||
attribute unsigned long size;
|
||||
// attribute DOMString src;
|
||||
// attribute DOMString step;
|
||||
attribute DOMString type;
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
// https://www.whatwg.org/html/#htmltablecellelement
|
||||
interface HTMLTableCellElement : HTMLElement {
|
||||
// attribute unsigned long colSpan;
|
||||
attribute unsigned long colSpan;
|
||||
// attribute unsigned long rowSpan;
|
||||
//[PutForwards=value] readonly attribute DOMSettableTokenList headers;
|
||||
//readonly attribute long cellIndex;
|
||||
|
|
|
@ -7,7 +7,8 @@
|
|||
interface HTMLTextAreaElement : HTMLElement {
|
||||
// attribute DOMString autocomplete;
|
||||
// attribute boolean autofocus;
|
||||
attribute unsigned long cols;
|
||||
[SetterThrows]
|
||||
attribute unsigned long cols;
|
||||
// attribute DOMString dirName;
|
||||
attribute boolean disabled;
|
||||
//readonly attribute HTMLFormElement? form;
|
||||
|
@ -18,6 +19,7 @@ interface HTMLTextAreaElement : HTMLElement {
|
|||
attribute DOMString placeholder;
|
||||
attribute boolean readOnly;
|
||||
attribute boolean required;
|
||||
[SetterThrows]
|
||||
attribute unsigned long rows;
|
||||
attribute DOMString wrap;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue