Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakhisharma committed Jul 27, 2017
1 parent 9757f71 commit 5fc23a3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
20 changes: 13 additions & 7 deletions components/script/dom/htmlimageelement.rs
Expand Up @@ -62,8 +62,8 @@ use style::context::QuirksMode;
use style::error_reporting::NullReporter;
use style::media_queries::MediaQuery;
use style::parser::ParserContext;
use style::values::specified::length::NoCalcLength;
use style::values::specified::{Length, ViewportPercentageLength};
use style::values::specified::length::NoCalcLength;
use style::attr::{AttrValue, LengthOrPercentageOrAuto};
use style_traits::ParsingMode;
use task_source::TaskSource;
Expand Down Expand Up @@ -797,16 +797,16 @@ impl LayoutHTMLImageElementHelpers for LayoutJS<HTMLImageElement> {
}
}

pub fn parse_a_sizes_attribute(input: DOMString, width: Option<u32>) -> Vec<Size> {
pub fn parse_a_sizes_attribute(input: DOMString, width: Option<u32>) -> Result<Vec<Size>, String> {
let mut sizes = Vec::<Size>::new();
let unparsed_sizes = input.deref().split(',').collect::<Vec<_>>();

for unparsed_size in &unparsed_sizes {
let whitespace = unparsed_size.chars().rev().take_while(|c| char::is_whitespace(*c)).count();
let trimmed: String = unparsed_size.chars().take(unparsed_size.chars().count() - whitespace).collect();
// TODO: do we need to throw/assert

if trimmed.is_empty() {
warn!("parse error while parsing sizes attribute");
// return Err("reason".to_owned());
continue;
}
let mut input = ParserInput::new(&trimmed);
Expand All @@ -825,7 +825,7 @@ let unparsed_sizes = input.deref().split(',').collect::<Vec<_>>();
query: None
}),
Err(_) => {
println!("Starts with media expression and not length");
// return Err("reason".to_owned());
let mut media_query_parser = Parser::new(&mut input);
let media_query = media_query_parser.try(|i| MediaQuery::parse(&context, i));
match media_query {
Expand All @@ -839,7 +839,7 @@ let unparsed_sizes = input.deref().split(',').collect::<Vec<_>>();
}
},
Err(_) => {
println!("Could not convert to MediaQuery/Length");
// return Err("reason".to_owned());
continue;
},
}
Expand All @@ -859,7 +859,7 @@ let unparsed_sizes = input.deref().split(',').collect::<Vec<_>>();
};
sizes.push(size);
}
sizes
return Ok(sizes)
}

impl HTMLImageElementMethods for HTMLImageElement {
Expand All @@ -873,6 +873,12 @@ impl HTMLImageElementMethods for HTMLImageElement {
// https://html.spec.whatwg.org/multipage/#dom-img-src
make_setter!(SetSrc, "src");

// https://html.spec.whatwg.org/multipage/images.html#parse-a-sizes-attribute
make_url_getter!(Sizes, "sizes");
// https://html.spec.whatwg.org/multipage/images.html#parse-a-sizes-attribute
make_setter!(SetSizes, "sizes");


// https://html.spec.whatwg.org/multipage/#dom-img-crossOrigin
fn GetCrossOrigin(&self) -> Option<DOMString> {
reflect_cross_origin_attribute(self.upcast::<Element>())
Expand Down
2 changes: 2 additions & 0 deletions components/script/dom/webidls/HTMLImageElement.webidl
Expand Up @@ -9,6 +9,8 @@ interface HTMLImageElement : HTMLElement {
attribute DOMString alt;
[CEReactions]
attribute DOMString src;
[CEReactions]
attribute DOMString sizes;
// [CEReactions]
// attribute DOMString srcset;
[CEReactions]
Expand Down
4 changes: 4 additions & 0 deletions components/script/test.rs
Expand Up @@ -15,6 +15,10 @@ pub mod area {
pub use dom::htmlareaelement::{Area, Shape};
}

pub mod sizes {
pub use dom::htmlimageelement::parse_a_sizes_attribute;
}

pub mod size_of {
use dom::characterdata::CharacterData;
use dom::element::Element;
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/script/htmlimageelement.rs
@@ -0,0 +1,16 @@

use script::test::sizes::parse_a_sizes_attribute;
use script::test::DOMString;

#[test]
fn empty_vector() {
assert!(parse_a_sizes_attribute(DOMString::new(), None).is_ok());
}

#[test]
fn test_whitespace() {
assert!(parse_a_sizes_attribute(DOMString::from(" (min-width: 500px)"),
None).is_ok());
}


2 changes: 2 additions & 0 deletions tests/unit/script/lib.rs
Expand Up @@ -12,4 +12,6 @@ extern crate servo_url;
#[cfg(test)] mod textinput;
#[cfg(test)] mod headers;
#[cfg(test)] mod htmlareaelement;
#[cfg(test)] mod htmlimageelement;


0 comments on commit 5fc23a3

Please sign in to comment.