Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse sizes attribute implementation #10989

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Next

Initial parse sizes attribute implementation

  • Loading branch information
srm09 committed May 3, 2016
commit e34d977937e8a31f2ea405b4bef7c1ef22c3f144
@@ -3,6 +3,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use app_units::Au;
use core::ops::Deref;
use cssparser::Parser;
use dom::attr::Attr;
use dom::attr::AttrValue;
use dom::bindings::cell::DOMRefCell;
@@ -31,7 +33,11 @@ use script_runtime::{CommonScriptMsg, ScriptChan};
use script_thread::Runnable;
use std::sync::Arc;
use string_cache::Atom;
use style::media_queries::MediaQuery;
use style::values::CSSFloat;
use style::values::specified::{Length, ViewportPercentageLength};
use url::Url;
use util;
use util::str::{DOMString, LengthOrPercentageOrAuto};

#[derive(JSTraceable, HeapSizeOf)]
@@ -49,6 +55,11 @@ struct ImageRequest {
image: Option<Arc<Image>>,
metadata: Option<ImageMetadata>,
}
#[allow(dead_code)]
pub struct Size {
pub query: Option<MediaQuery>,
pub length: Length,
}
#[dom_struct]
pub struct HTMLImageElement {
htmlelement: HTMLElement,
@@ -398,3 +409,60 @@ fn image_dimension_setter(element: &Element, attr: Atom, value: u32) {
let value = AttrValue::Dimension(DOMString::from(value.to_string()), dim);
element.set_attribute(&attr, value);
}

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

for unparsed_size in &unparsed_sizes {
let temp = *unparsed_size;
let whitespace = temp.chars().rev().take_while(|c| util::str::char_is_whitespace(*c)).count();
let trimmed: String = unparsed_size.chars().take(temp.chars().count() - whitespace).collect();
// TODO: do we need to throw/assert
if trimmed.is_empty() {
warn!("parse error while parsing sizes attribute");
continue;
}
let length = Parser::new(&trimmed).try(Length::parse_non_negative);
match length {
Ok(len) => sizes.push(Size {
length: len,
query: None
}),
Err(_) => {
println!("Starts with media expression and not length");
let mut media_query_parser = Parser::new(&trimmed);
let media_query = media_query_parser.try(MediaQuery::parse);
match media_query {
Ok(query) => {
let length = media_query_parser.try(Length::parse_non_negative);
if length.is_ok() {
sizes.push (Size {
length: length.unwrap(),
query: Some(query)
})
}
},
Err(_) => {
println!("Could not convert to MediaQuery/Length");
continue;
},
}
},
}
}
if sizes.len() == 0 {
let size = match width {
Some(w) => Size {
length: Length::from_px(w as f32),
query: None
},
None => Size {
length: Length::ViewportPercentage(ViewportPercentageLength::Vw(100 as f32)),
query: None
},
};
sizes.push(size);
}
sizes
}
@@ -140,7 +140,7 @@ impl Expression {
}

impl MediaQuery {
fn parse(input: &mut Parser) -> Result<MediaQuery, ()> {
pub fn parse(input: &mut Parser) -> Result<MediaQuery, ()> {
let mut expressions = vec![];

let qualifier = if input.try(|input| input.expect_ident_matching("only")).is_ok() {
@@ -4,6 +4,17 @@

#![feature(plugin)]
#![plugin(plugins)]
use script::dom::htmlimageelement::parse_a_sizes_attribute;
use util::str::DOMString;

#[test]
fn some_parse_sizes_test() {
let result = parse_a_sizes_attribute(DOMString::from("(min-width: 900px) 1000px,
(max-width: 900px) and (min-width: 400px) 50em,
100vw"),
None);
assert_eq!(result.len(), 3);
}

extern crate msg;
extern crate script;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.