Skip to content

Commit

Permalink
Auto merge of #19385 - tigercosmos:b1, r=KiChjang
Browse files Browse the repository at this point in the history
implement  "Date type inputs", "Month type inputs"

<!-- Please describe your changes on the following line: -->
implement "Date type inputs", "Month type inputs"

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] `./mach test-unit` does not report any errors

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19385)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Dec 5, 2017
2 parents 1e0b216 + 927fd1d commit 68cefb1
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 312 deletions.
101 changes: 101 additions & 0 deletions components/script/dom/bindings/str.rs
Expand Up @@ -279,6 +279,20 @@ impl DOMString {
_ => false
}
}

/// A valid date string should be "YYYY-MM-DD"
/// YYYY must be four or more digits, MM and DD both must be two digits
/// https://html.spec.whatwg.org/multipage/#valid-date-string
pub fn is_valid_date_string(&self) -> bool {
parse_date_string(&*self.0).is_ok()
}

/// A valid month string should be "YYYY-MM"
/// YYYY must be four or more digits, MM both must be two digits
/// https://html.spec.whatwg.org/multipage/#valid-month-string
pub fn is_valid_month_string(&self) -> bool {
parse_month_string(&*self.0).is_ok()
}
}

impl Borrow<str> for DOMString {
Expand Down Expand Up @@ -403,3 +417,90 @@ impl Extend<char> for DOMString {
self.0.extend(iterable)
}
}

/// https://html.spec.whatwg.org/multipage/#parse-a-month-string
fn parse_month_string(value: &str) -> Result<(u32, u32), ()> {
// Step 1, 2, 3
let (year_int, month_int) = parse_month_component(value)?;

// Step 4
if value.split("-").nth(2).is_some() {
return Err(());
}
// Step 5
Ok((year_int, month_int))
}

/// https://html.spec.whatwg.org/multipage/#parse-a-date-string
fn parse_date_string(value: &str) -> Result<(u32, u32, u32), ()> {
// Step 1, 2, 3
let (year_int, month_int, day_int) = parse_date_component(value)?;

// Step 4
if value.split('-').nth(3).is_some() {
return Err(());
}

// Step 5, 6
Ok((year_int, month_int, day_int))
}

/// https://html.spec.whatwg.org/multipage/#parse-a-month-component
fn parse_month_component(value: &str) -> Result<(u32, u32), ()> {
// Step 3
let mut iterator = value.split('-');
let year = iterator.next().ok_or(())?;
let month = iterator.next().ok_or(())?;

// Step 1, 2
let year_int = year.parse::<u32>().map_err(|_| ())?;
if year.len() < 4 || year_int == 0 {
return Err(());
}

// Step 4, 5
let month_int = month.parse::<u32>().map_err(|_| ())?;
if month.len() != 2 || month_int > 12 || month_int < 1 {
return Err(());
}

// Step 6
Ok((year_int, month_int))
}

/// https://html.spec.whatwg.org/multipage/#parse-a-date-component
fn parse_date_component(value: &str) -> Result<(u32, u32, u32), ()> {
// Step 1
let (year_int, month_int) = parse_month_component(value)?;

// Step 3, 4
let day = value.split('-').nth(2).ok_or(())?;
let day_int = day.parse::<u32>().map_err(|_| ())?;
if day.len() != 2 {
return Err(());
}

// Step 2, 5
let max_day = max_day_in_month(year_int, month_int)?;
if day_int == 0 || day_int > max_day {
return Err(());
}

// Step 6
Ok((year_int, month_int, day_int))
}

fn max_day_in_month(year_num: u32, month_num: u32) -> Result<u32, ()> {
match month_num {
1|3|5|7|8|10|12 => Ok(31),
4|6|9|11 => Ok(30),
2 => {
if year_num % 400 == 0 || (year_num % 4 == 0 && year_num % 100 != 0) {
Ok(29)
} else {
Ok(28)
}
},
_ => Err(())
}
}
14 changes: 13 additions & 1 deletion components/script/dom/htmlinputelement.rs
Expand Up @@ -875,6 +875,18 @@ impl HTMLInputElement {
content.strip_newlines();
content.strip_leading_and_trailing_ascii_whitespace();
}
atom!("date") => {
let mut textinput = self.textinput.borrow_mut();
if !textinput.single_line_content().is_valid_date_string() {
*textinput.single_line_content_mut() = "".into();
}
}
atom!("month") => {
let mut textinput = self.textinput.borrow_mut();
if !textinput.single_line_content().is_valid_month_string() {
*textinput.single_line_content_mut() = "".into();
}
}
atom!("color") => {
let mut textinput = self.textinput.borrow_mut();

Expand Down Expand Up @@ -1042,6 +1054,7 @@ impl VirtualMethods for HTMLInputElement {
let value = mutation.new_value(attr).map(|value| (**value).to_owned());
self.textinput.borrow_mut().set_content(
value.map_or(DOMString::new(), DOMString::from));
self.sanitize_value();
self.update_placeholder_shown_state();
},
&local_name!("name") if self.input_type.get() == InputType::InputRadio => {
Expand Down Expand Up @@ -1117,7 +1130,6 @@ impl VirtualMethods for HTMLInputElement {
if let Some(ref s) = self.super_type() {
s.bind_to_tree(tree_in_doc);
}

self.upcast::<Element>().check_ancestors_disabled_state_for_form_control();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/wpt/metadata/MANIFEST.json
Expand Up @@ -542997,7 +542997,7 @@
"testharness"
],
"html/semantics/forms/the-input-element/month.html": [
"1d833657f1db1e58fcc12a01af2ba06a665344aa",
"d0833c2b8ddd97c35200eb95ba0473795abf1f3b",
"testharness"
],
"html/semantics/forms/the-input-element/number.html": [
Expand Down
Expand Up @@ -5,10 +5,3 @@

[The max attribute, if specified, must have a value that is a valid date string.]
expected: FAIL

[User agents must not allow the user to set the value to a non-empty string that is not a valid date string.]
expected: FAIL

[Number of days]
expected: FAIL

@@ -1,32 +1,4 @@
[month.html]
type: testharness
[The value attribute, if specified and not empty, must have a value that is a valid month string]
expected: FAIL

[The min attribute, if specified, must have a value that is a valid month string.]
expected: FAIL

[The max attribute, if specified, must have a value that is a valid month string]
expected: FAIL

[User agents must not allow the user to set the value to a non-empty string that is not a valid month string.]
expected: FAIL

[When value attribute has two digits year value, the value,which is invalid, must return empty string.]
expected: FAIL

[When value is set with invalid value, the value must return empty string.]
expected: FAIL

[When value is given invalid value to non-empty valid string, the value must be same as before.]
expected: FAIL

[When step attribute is given invalid value, it must ignore the invalid value and use defaul value instead.]
expected: FAIL

[Month should be <= 13: If the value of the element is not a valid month string, then set it to the empty string instead.]
expected: FAIL

[Month should be > 0: If the value of the element is not a valid month string, then set it to the empty string instead.>]
expected: FAIL

This file was deleted.

0 comments on commit 68cefb1

Please sign in to comment.