Auto merge of #8526 - frewsxcv:parse-length, r=eefriedman

Fix parse_length 0 values, implement <hr> 'width'

Follow-up to https://github.com/servo/servo/issues/8424

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/8526)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2015-11-15 03:30:46 +05:30
commit 3ef0a9a79d
13 changed files with 117 additions and 147 deletions

View file

@ -390,6 +390,9 @@ impl LayoutElementHelpers for LayoutJS<Element> {
this.get_width()
} else if let Some(this) = self.downcast::<HTMLTableCellElement>() {
this.get_width()
} else if let Some(this) = self.downcast::<HTMLHRElement>() {
// https://html.spec.whatwg.org/multipage/#the-hr-element-2:attr-hr-width
this.get_width()
} else {
LengthOrPercentageOrAuto::Auto
};

View file

@ -13,7 +13,7 @@ use dom::htmlelement::HTMLElement;
use dom::node::Node;
use dom::virtualmethods::VirtualMethods;
use string_cache::Atom;
use util::str::DOMString;
use util::str::{DOMString, LengthOrPercentageOrAuto};
#[dom_struct]
pub struct HTMLHRElement {
@ -42,10 +42,17 @@ impl HTMLHRElementMethods for HTMLHRElement {
// https://html.spec.whatwg.org/multipage/#dom-hr-color
make_legacy_color_setter!(SetColor, "color");
// https://html.spec.whatwg.org/multipage/#dom-hr-width
make_getter!(Width);
// https://html.spec.whatwg.org/multipage/#dom-hr-width
make_dimension_setter!(SetWidth, "width");
}
pub trait HTMLHRLayoutHelpers {
fn get_color(&self) -> Option<RGBA>;
fn get_width(&self) -> LengthOrPercentageOrAuto;
}
impl HTMLHRLayoutHelpers for LayoutJS<HTMLHRElement> {
@ -58,6 +65,17 @@ impl HTMLHRLayoutHelpers for LayoutJS<HTMLHRElement> {
.cloned()
}
}
#[allow(unsafe_code)]
fn get_width(&self) -> LengthOrPercentageOrAuto {
unsafe {
(&*self.upcast::<Element>().unsafe_get())
.get_attr_for_layout(&ns!(""), &atom!("width"))
.map(AttrValue::as_dimension)
.cloned()
.unwrap_or(LengthOrPercentageOrAuto::Auto)
}
}
}
@ -69,6 +87,7 @@ impl VirtualMethods for HTMLHRElement {
fn parse_plain_attribute(&self, name: &Atom, value: DOMString) -> AttrValue {
match name {
&atom!("color") => AttrValue::from_legacy_color(value),
&atom!("width") => AttrValue::from_dimension(value),
_ => self.super_type().unwrap().parse_plain_attribute(name, value),
}
}

View file

@ -14,5 +14,5 @@ partial interface HTMLHRElement {
attribute DOMString color;
// attribute boolean noShade;
// attribute DOMString size;
// attribute DOMString width;
attribute DOMString width;
};

View file

@ -1923,6 +1923,7 @@ dependencies = [
name = "util_tests"
version = "0.0.1"
dependencies = [
"app_units 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"euclid 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"plugins 0.0.1",

View file

@ -303,20 +303,39 @@ pub enum LengthOrPercentageOrAuto {
Length(Au),
}
/// Parses a length per HTML5 § 2.4.4.4. If unparseable, `Auto` is returned.
/// TODO: this function can be rewritten to return Result<LengthOrPercentage, _>
/// Parses a dimension value per HTML5 § 2.4.4.4. If unparseable, `Auto` is
/// returned.
/// https://html.spec.whatwg.org/multipage/#rules-for-parsing-dimension-values
pub fn parse_length(mut value: &str) -> LengthOrPercentageOrAuto {
// Steps 1 & 2 are not relevant
// Step 3
value = value.trim_left_matches(WHITESPACE);
if value.is_empty() {
return LengthOrPercentageOrAuto::Auto
}
if value.starts_with("+") {
value = &value[1..]
}
value = value.trim_left_matches('0');
// Step 4
if value.is_empty() {
return LengthOrPercentageOrAuto::Auto
}
// Step 5
if value.starts_with("+") {
value = &value[1..]
}
// Steps 6 & 7
match value.chars().nth(0) {
Some('0'...'9') => {},
_ => return LengthOrPercentageOrAuto::Auto,
}
// Steps 8 to 13
// We trim the string length to the minimum of:
// 1. the end of the string
// 2. the first occurence of a '%' (U+0025 PERCENT SIGN)
// 3. the second occurrence of a '.' (U+002E FULL STOP)
// 4. the occurrence of a character that is neither a digit nor '%' nor '.'
// Note: Step 10 is directly subsumed by FromStr::from_str
let mut end_index = value.len();
let (mut found_full_stop, mut found_percent) = (false, false);
for (i, ch) in value.chars().enumerate() {
@ -348,7 +367,7 @@ pub fn parse_length(mut value: &str) -> LengthOrPercentageOrAuto {
}
match FromStr::from_str(value) {
Ok(number) => LengthOrPercentageOrAuto::Length(Au::from_px(number)),
Ok(number) => LengthOrPercentageOrAuto::Length(Au::from_f64_px(number)),
Err(_) => LengthOrPercentageOrAuto::Auto,
}
}