Skip to content

Commit

Permalink
Parse HTMLInputElement attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
KiChjang committed May 24, 2016
1 parent e18bf81 commit c93ed39
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 132 deletions.
49 changes: 25 additions & 24 deletions components/script/dom/htmlinputelement.rs
Expand Up @@ -271,22 +271,22 @@ impl LayoutHTMLInputElementHelpers for LayoutJS<HTMLInputElement> {

impl HTMLInputElementMethods for HTMLInputElement {

// https://html.spec.whatwg.org/multipage/#attr-input-accept
// https://html.spec.whatwg.org/multipage/#dom-input-accept
make_getter!(Accept, "accept");

// https://html.spec.whatwg.org/multipage/#attr-input-accept
// https://html.spec.whatwg.org/multipage/#dom-input-accept
make_setter!(SetAccept, "accept");

// https://html.spec.whatwg.org/multipage/#attr-input-alt
// https://html.spec.whatwg.org/multipage/#dom-input-alt
make_getter!(Alt, "alt");

// https://html.spec.whatwg.org/multipage/#attr-input-alt
// https://html.spec.whatwg.org/multipage/#dom-input-alt
make_setter!(SetAlt, "alt");

// https://html.spec.whatwg.org/multipage/#attr-input-dirName
// https://html.spec.whatwg.org/multipage/#dom-input-dirName
make_getter!(DirName, "dirname");

// https://html.spec.whatwg.org/multipage/#attr-input-dirName
// https://html.spec.whatwg.org/multipage/#dom-input-dirName
make_setter!(SetDirName, "dirname");

// https://html.spec.whatwg.org/multipage/#dom-fe-disabled
Expand Down Expand Up @@ -415,10 +415,10 @@ impl HTMLInputElementMethods for HTMLInputElement {
// https://html.spec.whatwg.org/multipage/#attr-fe-name
make_atomic_setter!(SetName, "name");

// https://html.spec.whatwg.org/multipage/#attr-input-placeholder
// https://html.spec.whatwg.org/multipage/#dom-input-placeholder
make_getter!(Placeholder, "placeholder");

// https://html.spec.whatwg.org/multipage/#attr-input-placeholder
// https://html.spec.whatwg.org/multipage/#dom-input-placeholder
make_setter!(SetPlaceholder, "placeholder");

// https://html.spec.whatwg.org/multipage/#dom-input-formaction
Expand Down Expand Up @@ -454,10 +454,10 @@ impl HTMLInputElementMethods for HTMLInputElement {
// https://html.spec.whatwg.org/multipage/#attr-fs-formnovalidate
make_bool_setter!(SetFormNoValidate, "formnovalidate");

// https://html.spec.whatwg.org/multipage/#attr-input-max
// https://html.spec.whatwg.org/multipage/#dom-input-max
make_getter!(Max, "max");

// https://html.spec.whatwg.org/multipage/#attr-input-max
// https://html.spec.whatwg.org/multipage/#dom-input-max
make_setter!(SetMax, "max");

// https://html.spec.whatwg.org/multipage/#dom-input-maxlength
Expand All @@ -466,40 +466,40 @@ impl HTMLInputElementMethods for HTMLInputElement {
// https://html.spec.whatwg.org/multipage/#dom-input-maxlength
make_limited_int_setter!(SetMaxLength, "maxlength", DEFAULT_MAX_LENGTH);

// https://html.spec.whatwg.org/multipage/#attr-input-min
// https://html.spec.whatwg.org/multipage/#dom-input-min
make_getter!(Min, "min");

// https://html.spec.whatwg.org/multipage/#attr-input-min
// https://html.spec.whatwg.org/multipage/#dom-input-min
make_setter!(SetMin, "min");

// https://html.spec.whatwg.org/multipage/#attr-input-multiple
// https://html.spec.whatwg.org/multipage/#dom-input-multiple
make_bool_getter!(Multiple, "multiple");

// https://html.spec.whatwg.org/multipage/#attr-input-multiple
// https://html.spec.whatwg.org/multipage/#dom-input-multiple
make_bool_setter!(SetMultiple, "multiple");

// https://html.spec.whatwg.org/multipage/#attr-input-pattern
// https://html.spec.whatwg.org/multipage/#dom-input-pattern
make_getter!(Pattern, "pattern");

// https://html.spec.whatwg.org/multipage/#attr-input-pattern
// https://html.spec.whatwg.org/multipage/#dom-input-pattern
make_setter!(SetPattern, "pattern");

// https://html.spec.whatwg.org/multipage/#attr-input-required
// https://html.spec.whatwg.org/multipage/#dom-input-required
make_bool_getter!(Required, "required");

// https://html.spec.whatwg.org/multipage/#attr-input-required
// https://html.spec.whatwg.org/multipage/#dom-input-required
make_bool_setter!(SetRequired, "required");

// https://html.spec.whatwg.org/multipage/#attr-input-src
make_getter!(Src, "src");
// https://html.spec.whatwg.org/multipage/#dom-input-src
make_url_getter!(Src, "src");

// https://html.spec.whatwg.org/multipage/#attr-input-src
make_setter!(SetSrc, "src");
// https://html.spec.whatwg.org/multipage/#dom-input-src
make_url_setter!(SetSrc, "src");

// https://html.spec.whatwg.org/multipage/#attr-input-step
// https://html.spec.whatwg.org/multipage/#dom-input-step
make_getter!(Step, "step");

// https://html.spec.whatwg.org/multipage/#attr-input-step
// https://html.spec.whatwg.org/multipage/#dom-input-step
make_setter!(SetStep, "step");

// https://html.spec.whatwg.org/multipage/#dom-input-indeterminate
Expand Down Expand Up @@ -881,6 +881,7 @@ impl VirtualMethods for HTMLInputElement {

fn parse_plain_attribute(&self, name: &Atom, value: DOMString) -> AttrValue {
match name {
&atom!("accept") => AttrValue::from_comma_separated_tokenlist(value.into()),
&atom!("name") => AttrValue::from_atomic(value.into()),
&atom!("size") => AttrValue::from_limited_u32(value.into(), DEFAULT_INPUT_SIZE),
&atom!("type") => AttrValue::from_atomic(value.into()),
Expand Down
21 changes: 15 additions & 6 deletions components/style/attr.rs
Expand Up @@ -11,7 +11,7 @@ use std::str::FromStr;
use string_cache::{Atom, Namespace};
use url::Url;
use util::str::{LengthOrPercentageOrAuto, HTML_SPACE_CHARACTERS};
use util::str::{read_exponent, read_fraction, read_numbers, split_html_space_chars};
use util::str::{read_exponent, read_fraction, read_numbers, split_commas, split_html_space_chars};
use values::specified::{Length};

// Duplicated from script::dom::values.
Expand Down Expand Up @@ -124,6 +124,15 @@ impl AttrValue {
AttrValue::TokenList(tokens, atoms)
}

pub fn from_comma_separated_tokenlist(tokens: String) -> AttrValue {
let atoms = split_commas(&tokens).map(Atom::from)
.fold(vec![], |mut acc, atom| {
if !acc.contains(&atom) { acc.push(atom) }
acc
});
AttrValue::TokenList(tokens, atoms)
}

#[cfg(not(feature = "gecko"))] // Gecko can't borrow atoms as UTF-8.
pub fn from_atomic_tokens(atoms: Vec<Atom>) -> AttrValue {
use util::str::str_join;
Expand Down Expand Up @@ -151,12 +160,12 @@ impl AttrValue {
// https://html.spec.whatwg.org/multipage/#reflecting-content-attributes-in-idl-attributes:idl-double
pub fn from_double(string: String, default: f64) -> AttrValue {
let result = parse_double(&string).unwrap_or(default);
let result = if result.is_infinite() {
default

if result.is_normal() {
AttrValue::Double(string, result)
} else {
result
};
AttrValue::Double(string, result)
AttrValue::Double(string, default)
}
}

// https://html.spec.whatwg.org/multipage/#limited-to-only-non-negative-numbers
Expand Down
4 changes: 4 additions & 0 deletions components/util/str.rs
Expand Up @@ -41,6 +41,10 @@ pub fn split_html_space_chars<'a>(s: &'a str) ->
s.split(HTML_SPACE_CHARACTERS).filter(not_empty as fn(&&str) -> bool)
}

pub fn split_commas<'a>(s: &'a str) -> Filter<Split<'a, char>, fn(&&str) -> bool> {
fn not_empty(&split: &&str) -> bool { !split.is_empty() }
s.split(',').filter(not_empty as fn(&&str) -> bool)
}

fn is_ascii_digit(c: &char) -> bool {
match *c {
Expand Down
102 changes: 0 additions & 102 deletions tests/wpt/metadata/html/dom/reflection-forms.html.ini
Expand Up @@ -4341,108 +4341,6 @@
[input.inputMode: IDL set to "URL" followed by IDL get]
expected: FAIL

[input.src: setAttribute() to "" followed by IDL get]
expected: FAIL

[input.src: setAttribute() to " foo " followed by IDL get]
expected: FAIL

[input.src: setAttribute() to "//site.example/path???@#l" followed by IDL get]
expected: FAIL

[input.src: setAttribute() to "\\0\\x01\\x02\\x03\\x04\\x05\\x06\\x07 \\b\\t\\n\\v\\f\\r\\x0e\\x0f \\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17 \\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f " followed by IDL get]
expected: FAIL

[input.src: setAttribute() to undefined followed by IDL get]
expected: FAIL

[input.src: setAttribute() to 7 followed by IDL get]
expected: FAIL

[input.src: setAttribute() to 1.5 followed by IDL get]
expected: FAIL

[input.src: setAttribute() to true followed by IDL get]
expected: FAIL

[input.src: setAttribute() to false followed by IDL get]
expected: FAIL

[input.src: setAttribute() to object "[object Object\]" followed by IDL get]
expected: FAIL

[input.src: setAttribute() to NaN followed by IDL get]
expected: FAIL

[input.src: setAttribute() to Infinity followed by IDL get]
expected: FAIL

[input.src: setAttribute() to -Infinity followed by IDL get]
expected: FAIL

[input.src: setAttribute() to "\\0" followed by IDL get]
expected: FAIL

[input.src: setAttribute() to null followed by IDL get]
expected: FAIL

[input.src: setAttribute() to object "test-toString" followed by IDL get]
expected: FAIL

[input.src: setAttribute() to object "test-valueOf" followed by IDL get]
expected: FAIL

[input.src: IDL set to "" followed by IDL get]
expected: FAIL

[input.src: IDL set to " foo " followed by IDL get]
expected: FAIL

[input.src: IDL set to "//site.example/path???@#l" followed by IDL get]
expected: FAIL

[input.src: IDL set to "\\0\\x01\\x02\\x03\\x04\\x05\\x06\\x07 \\b\\t\\n\\v\\f\\r\\x0e\\x0f \\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17 \\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f " followed by IDL get]
expected: FAIL

[input.src: IDL set to undefined followed by IDL get]
expected: FAIL

[input.src: IDL set to 7 followed by IDL get]
expected: FAIL

[input.src: IDL set to 1.5 followed by IDL get]
expected: FAIL

[input.src: IDL set to true followed by IDL get]
expected: FAIL

[input.src: IDL set to false followed by IDL get]
expected: FAIL

[input.src: IDL set to object "[object Object\]" followed by IDL get]
expected: FAIL

[input.src: IDL set to NaN followed by IDL get]
expected: FAIL

[input.src: IDL set to Infinity followed by IDL get]
expected: FAIL

[input.src: IDL set to -Infinity followed by IDL get]
expected: FAIL

[input.src: IDL set to "\\0" followed by IDL get]
expected: FAIL

[input.src: IDL set to null followed by IDL get]
expected: FAIL

[input.src: IDL set to object "test-toString" followed by IDL get]
expected: FAIL

[input.src: IDL set to object "test-valueOf" followed by IDL get]
expected: FAIL

[input.align: typeof IDL attribute]
expected: FAIL

Expand Down

0 comments on commit c93ed39

Please sign in to comment.