servo: Merge #10962 - Parse HTMLInputElement attributes (from KiChjang:input-attr-parse); r=asajeffrey
authorKeith Yeung <kungfukeith11@gmail.com>
Tue, 24 May 2016 07:42:23 -0700
changeset 477062 069b529edd2aeddf68a9e7f8b0d5ee116e3ce29e
parent 477061 c1f26391396ed3fcedb9b654c162a59ddcf5df83
child 477063 624e6af47f5f6ab9822441142192c9568435dfd4
push id44079
push userbmo:gps@mozilla.com
push dateSat, 04 Feb 2017 00:14:49 +0000
reviewersasajeffrey
servo: Merge #10962 - Parse HTMLInputElement attributes (from KiChjang:input-attr-parse); r=asajeffrey Fixes #10491. Source-Repo: https://github.com/servo/servo Source-Revision: 8c4929a196ca933532d0c6948c9eebd92be4013a
servo/components/script/dom/htmlinputelement.rs
servo/components/style/attr.rs
servo/components/util/str.rs
--- a/servo/components/script/dom/htmlinputelement.rs
+++ b/servo/components/script/dom/htmlinputelement.rs
@@ -266,32 +266,32 @@ impl LayoutHTMLInputElementHelpers for L
     #[allow(unsafe_code)]
     unsafe fn indeterminate_state_for_layout(self) -> bool {
         self.upcast::<Element>().get_state_for_layout().contains(IN_INDETERMINATE_STATE)
     }
 }
 
 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
     make_bool_getter!(Disabled, "disabled");
 
     // https://html.spec.whatwg.org/multipage/#dom-fe-disabled
     make_bool_setter!(SetDisabled, "disabled");
 
@@ -410,20 +410,20 @@ impl HTMLInputElementMethods for HTMLInp
     make_setter!(SetDefaultValue, "value");
 
     // https://html.spec.whatwg.org/multipage/#attr-fe-name
     make_getter!(Name, "name");
 
     // 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
     make_url_or_base_getter!(FormAction, "formaction");
 
     // https://html.spec.whatwg.org/multipage/#dom-input-formaction
     make_setter!(SetFormAction, "formaction");
 
@@ -449,62 +449,62 @@ impl HTMLInputElementMethods for HTMLInp
     make_setter!(SetFormTarget, "formtarget");
 
     // https://html.spec.whatwg.org/multipage/#attr-fs-formnovalidate
     make_bool_getter!(FormNoValidate, "formnovalidate");
 
     // 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
     make_int_getter!(MaxLength, "maxlength", DEFAULT_MAX_LENGTH);
 
     // 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
     fn Indeterminate(&self) -> bool {
         self.upcast::<Element>().state().contains(IN_INDETERMINATE_STATE)
     }
 
     // https://html.spec.whatwg.org/multipage/#dom-input-indeterminate
@@ -876,16 +876,17 @@ 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()),
             &atom!("maxlength") => AttrValue::from_limited_i32(value.into(), DEFAULT_MAX_LENGTH),
             _ => self.super_type().unwrap().parse_plain_attribute(name, value),
         }
     }
 
--- a/servo/components/style/attr.rs
+++ b/servo/components/style/attr.rs
@@ -6,17 +6,17 @@ use app_units::Au;
 use cssparser::{self, Color, RGBA};
 use euclid::num::Zero;
 use num_traits::ToPrimitive;
 use std::ascii::AsciiExt;
 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.
 const UNSIGNED_LONG_MAX: u32 = 2147483647;
 
 #[derive(PartialEq, Clone, HeapSizeOf)]
 pub enum AttrValue {
     String(String),
@@ -119,16 +119,25 @@ impl AttrValue {
             .map(Atom::from)
             .fold(vec![], |mut acc, atom| {
                 if !acc.contains(&atom) { acc.push(atom) }
                 acc
             });
         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;
         // TODO(ajeffrey): effecient conversion of Vec<Atom> to String
         let tokens = String::from(str_join(&atoms, "\x20"));
         AttrValue::TokenList(tokens, atoms)
     }
 
@@ -146,22 +155,22 @@ impl AttrValue {
     pub fn from_i32(string: String, default: i32) -> AttrValue {
         let result = parse_integer(string.chars()).unwrap_or(default);
         AttrValue::Int(string, result)
     }
 
     // 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
     pub fn from_limited_i32(string: String, default: i32) -> AttrValue {
         let result = parse_integer(string.chars()).unwrap_or(default);
 
         if result < 0 {
             AttrValue::Int(string, default)
--- a/servo/components/util/str.rs
+++ b/servo/components/util/str.rs
@@ -36,16 +36,20 @@ pub fn is_whitespace(s: &str) -> bool {
 }
 
 pub fn split_html_space_chars<'a>(s: &'a str) ->
                                   Filter<Split<'a, StaticCharVec>, fn(&&str) -> bool> {
     fn not_empty(&split: &&str) -> bool { !split.is_empty() }
     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 {
         '0'...'9' => true,
         _ => false,
     }
 }