diff options
author | Youngmin Yoo <youngmin.yoo@samsung.com> | 2014-02-19 15:52:58 +0900 |
---|---|---|
committer | Youngmin Yoo <youngmin.yoo@samsung.com> | 2014-02-19 17:23:50 +0900 |
commit | 7b56e75adb0002a3bd001aa61b55249c938fc60e (patch) | |
tree | ad070df96a1a874cf7df22d9c8ac34124366d9e7 /src | |
parent | a12608d2e4d44da49d8f26fc9d111b7dfd01bfd7 (diff) | |
download | servo-7b56e75adb0002a3bd001aa61b55249c938fc60e.tar.gz servo-7b56e75adb0002a3bd001aa61b55249c938fc60e.zip |
Support base_url in stylesheet
Diffstat (limited to 'src')
-rw-r--r-- | src/components/script/dom/element.rs | 4 | ||||
-rw-r--r-- | src/components/script/html/hubbub_html_parser.rs | 7 | ||||
-rw-r--r-- | src/components/style/media_queries.rs | 7 | ||||
-rw-r--r-- | src/components/style/properties.rs.mako | 129 | ||||
-rw-r--r-- | src/components/style/stylesheets.rs | 12 |
5 files changed, 133 insertions, 26 deletions
diff --git a/src/components/script/dom/element.rs b/src/components/script/dom/element.rs index 9ffb17c3254..2eb0553f2f6 100644 --- a/src/components/script/dom/element.rs +++ b/src/components/script/dom/element.rs @@ -221,7 +221,9 @@ impl Element { match local_name.as_slice() { "style" => { - self.style_attribute = Some(style::parse_style_attribute(value)) + let doc = self.node.owner_doc(); + let base_url = doc.document().url.clone(); + self.style_attribute = Some(style::parse_style_attribute(value, &base_url)) } "id" if abstract_self.is_in_doc() => { // XXX: this dual declaration are workaround to avoid the compile error: diff --git a/src/components/script/html/hubbub_html_parser.rs b/src/components/script/html/hubbub_html_parser.rs index 4ba46343817..9b66e4ecb1a 100644 --- a/src/components/script/html/hubbub_html_parser.rs +++ b/src/components/script/html/hubbub_html_parser.rs @@ -26,7 +26,6 @@ use servo_util::url::parse_url; use std::cast; use std::cell::RefCell; use std::comm::{Port, SharedChan}; -use std::from_str::FromStr; use std::str; use style::Stylesheet; @@ -277,7 +276,8 @@ pub fn parse_html(cx: *JSContext, debug!("Fetched page; metadata is {:?}", load_response.metadata); - let url2 = load_response.metadata.final_url.clone(); + let base_url = load_response.metadata.final_url.clone(); + let url2 = base_url.clone(); let url3 = url2.clone(); // Store the final URL before we start parsing, so that DOM routines @@ -485,7 +485,6 @@ pub fn parse_html(cx: *JSContext, // We've reached the end of a <style> so we can submit all the text to the parser. unsafe { let style: AbstractNode = NodeWrapping::from_hubbub_node(style); - let url = FromStr::from_str("http://example.com/"); // FIXME let mut data = ~[]; debug!("iterating over children {:?}", style.first_child()); for child in style.children() { @@ -496,7 +495,7 @@ pub fn parse_html(cx: *JSContext, } debug!("style data = {:?}", data); - let provenance = InlineProvenance(url.unwrap(), data.concat()); + let provenance = InlineProvenance(base_url.clone(), data.concat()); css_chan3.send(CSSTaskNewFile(provenance)); } }, diff --git a/src/components/style/media_queries.rs b/src/components/style/media_queries.rs index 06866736305..a7ed89e8230 100644 --- a/src/components/style/media_queries.rs +++ b/src/components/style/media_queries.rs @@ -9,6 +9,7 @@ use cssparser::ast::*; use errors::{ErrorLoggerIterator, log_css_error}; use stylesheets::{CSSRule, CSSMediaRule, parse_style_rule, parse_nested_at_rule}; use namespaces::NamespaceMap; +use extra::url::Url; pub struct MediaRule { @@ -48,7 +49,7 @@ pub struct Device { pub fn parse_media_rule(rule: AtRule, parent_rules: &mut ~[CSSRule], - namespaces: &NamespaceMap) { + namespaces: &NamespaceMap, base_url: &Url) { let media_queries = parse_media_query_list(rule.prelude); let block = match rule.block { Some(block) => block, @@ -60,9 +61,9 @@ pub fn parse_media_rule(rule: AtRule, parent_rules: &mut ~[CSSRule], let mut rules = ~[]; for rule in ErrorLoggerIterator(parse_rule_list(block.move_iter())) { match rule { - QualifiedRule(rule) => parse_style_rule(rule, &mut rules, namespaces), + QualifiedRule(rule) => parse_style_rule(rule, &mut rules, namespaces, base_url), AtRule(rule) => parse_nested_at_rule( - rule.name.to_ascii_lower(), rule, &mut rules, namespaces), + rule.name.to_ascii_lower(), rule, &mut rules, namespaces, base_url), } } parent_rules.push(CSSMediaRule(MediaRule { diff --git a/src/components/style/properties.rs.mako b/src/components/style/properties.rs.mako index e8325110d1d..bdd51a702a8 100644 --- a/src/components/style/properties.rs.mako +++ b/src/components/style/properties.rs.mako @@ -7,6 +7,7 @@ use std::ascii::StrAsciiExt; pub use servo_util::url::parse_url; pub use extra::arc::Arc; +pub use extra::url::Url; use servo_util::cowarc::CowArc; pub use cssparser::*; @@ -50,7 +51,9 @@ STYLE_STRUCTS = [] THIS_STYLE_STRUCT = None LONGHANDS = [] LONGHANDS_BY_NAME = {} +LONGHANDS_WITH_URL = [] SHORTHANDS = [] +SHORTHANDS_WITH_URL = [] def new_style_struct(name, is_inherited): global THIS_STYLE_STRUCT @@ -103,6 +106,32 @@ pub mod longhands { } </%def> + <%def name="url_longhand(name, no_super=False)"> + <% + property = Longhand(name) + THIS_STYLE_STRUCT.longhands.append(property) + LONGHANDS_WITH_URL.append(property) + LONGHANDS_BY_NAME[name] = property + %> + pub mod ${property.ident} { + % if not no_super: + use super::*; + % endif + pub use self::computed_value::*; + ${caller.body()} + + pub fn parse_declared(input: &[ComponentValue], base_url: &Url) + -> Option<DeclaredValue<SpecifiedValue>> { + match CSSWideKeyword::parse(input) { + Some(Some(keyword)) => Some(CSSWideKeyword(keyword)), + Some(None) => Some(CSSWideKeyword(${ + "Inherit" if THIS_STYLE_STRUCT.inherited else "Initial"})), + None => parse_specified(input, base_url), + } + } + } + </%def> + <%def name="longhand(name, no_super=False)"> <%self:raw_longhand name="${name}"> ${caller.body()} @@ -475,7 +504,7 @@ pub mod longhands { ${predefined_type("background-color", "CSSColor", "RGBA(RGBA { red: 0., green: 0., blue: 0., alpha: 0. }) /* transparent */")} - <%self:single_component_value name="background-image"> + <%self:url_longhand name="background-image"> // The computed value is the same as the specified value. pub use to_computed_value = super::computed_as_specified; pub mod computed_value { @@ -486,16 +515,29 @@ pub mod longhands { #[inline] pub fn get_initial_value() -> SpecifiedValue { None } - pub fn from_component_value(component_value: &ComponentValue) -> Option<SpecifiedValue> { + pub fn parse(input: &[ComponentValue], base_url: &Url) -> Option<SpecifiedValue> { + from_iter(input.skip_whitespace(), base_url) + } + pub fn from_iter<'a>(mut iter: SkipWhitespaceIterator<'a>, base_url: &Url) -> Option<SpecifiedValue> { + match iter.next() { + Some(v) => from_component_value_with_url(v, base_url), + _ => None + } + } + pub fn from_component_value_with_url(component_value: &ComponentValue, base_url: &Url) -> Option<SpecifiedValue> { match component_value { &ast::URL(ref url) => { - let image_url = parse_url(url.as_slice(), None); + let image_url = parse_url(url.as_slice(), Some(base_url.clone())); Some(Some(image_url)) }, _ => None, } } - </%self:single_component_value> + pub fn parse_specified(input: &[ComponentValue], base_url: &Url) + -> Option<DeclaredValue<SpecifiedValue>> { + parse(input, base_url).map(super::SpecifiedValue) + } + </%self:url_longhand> ${new_style_struct("Color", is_inherited=True)} @@ -805,6 +847,24 @@ pub mod shorthands { } </%def> + <%def name="url_shorthand(name, sub_properties)"> + <% + shorthand = Shorthand(name, sub_properties.split()) + SHORTHANDS_WITH_URL.append(shorthand) + %> + pub mod ${shorthand.ident} { + use super::*; + + struct Longhands { + % for sub_property in shorthand.sub_properties: + ${sub_property.ident}: Option<${sub_property.ident}::SpecifiedValue>, + % endfor + } + pub fn parse(input: &[ComponentValue], base_url: &Url) -> Option<Longhands> { + ${caller.body()} + } + } + </%def> <%def name="four_sides_shorthand(name, sub_property_pattern, parser_function)"> <%self:shorthand name="${name}" sub_properties="${ ' '.join(sub_property_pattern % side @@ -833,7 +893,7 @@ pub mod shorthands { </%def> // TODO: other background-* properties - <%self:shorthand name="background" sub_properties="background-color background-image"> + <%self:url_shorthand name="background" sub_properties="background-color background-image"> let mut color = None; let mut image = None; let mut any = false; @@ -847,7 +907,7 @@ pub mod shorthands { } if image.is_none() { - match background_image::from_component_value(component_value) { + match background_image::from_component_value_with_url(component_value, base_url) { Some(v) => { image = Some(v); any = true; continue }, None => (), } @@ -856,7 +916,7 @@ pub mod shorthands { } if any { Some(Longhands { background_color: color, background_image: image }) } else { None } - </%self:shorthand> + </%self:url_shorthand> ${four_sides_shorthand("margin", "margin-%s", "margin_top::from_component_value")} ${four_sides_shorthand("padding", "padding-%s", "padding_top::from_component_value")} @@ -1014,12 +1074,12 @@ pub struct PropertyDeclarationBlock { } -pub fn parse_style_attribute(input: &str) -> PropertyDeclarationBlock { - parse_property_declaration_list(tokenize(input)) +pub fn parse_style_attribute(input: &str, base_url: &Url) -> PropertyDeclarationBlock { + parse_property_declaration_list(tokenize(input), base_url) } -pub fn parse_property_declaration_list<I: Iterator<Node>>(input: I) -> PropertyDeclarationBlock { +pub fn parse_property_declaration_list<I: Iterator<Node>>(input: I, base_url: &Url) -> PropertyDeclarationBlock { let mut important = ~[]; let mut normal = ~[]; for item in ErrorLoggerIterator(parse_declaration_list(input)) { @@ -1029,7 +1089,7 @@ pub fn parse_property_declaration_list<I: Iterator<Node>>(input: I) -> PropertyD Declaration(Declaration{ location: l, name: n, value: v, important: i}) => { // TODO: only keep the last valid declaration for a given name. let list = if i { &mut important } else { &mut normal }; - match PropertyDeclaration::parse(n, v, list) { + match PropertyDeclaration::parse(n, v, list, base_url) { UnknownProperty => log_css_error(l, format!( "Unsupported property: {}:{}", n, v.iter().to_css())), InvalidValue => log_css_error(l, format!( @@ -1077,6 +1137,9 @@ pub enum PropertyDeclaration { % for property in LONGHANDS: ${property.ident}_declaration(DeclaredValue<longhands::${property.ident}::SpecifiedValue>), % endfor + % for property in LONGHANDS_WITH_URL: + ${property.ident}_declaration(DeclaredValue<longhands::${property.ident}::SpecifiedValue>), + % endfor } @@ -1086,9 +1149,11 @@ enum PropertyDeclarationParseResult { ValidDeclaration, } + impl PropertyDeclaration { pub fn parse(name: &str, value: &[ComponentValue], - result_list: &mut ~[PropertyDeclaration]) -> PropertyDeclarationParseResult { + result_list: &mut ~[PropertyDeclaration], + base_url: &Url) -> PropertyDeclarationParseResult { // FIXME: local variable to work around Rust #10683 let name_lower = name.to_ascii_lower(); match name_lower.as_slice() { @@ -1100,6 +1165,14 @@ impl PropertyDeclaration { } )), % endfor + % for property in LONGHANDS_WITH_URL: + "${property.name}" => result_list.push(${property.ident}_declaration( + match longhands::${property.ident}::parse_declared(value, base_url) { + Some(value) => value, + None => return InvalidValue, + } + )), + % endfor % for shorthand in SHORTHANDS: "${shorthand.name}" => match CSSWideKeyword::parse(value) { Some(Some(keyword)) => { @@ -1132,6 +1205,38 @@ impl PropertyDeclaration { } }, % endfor + % for shorthand in SHORTHANDS_WITH_URL: + "${shorthand.name}" => match CSSWideKeyword::parse(value) { + Some(Some(keyword)) => { + % for sub_property in shorthand.sub_properties: + result_list.push(${sub_property.ident}_declaration( + CSSWideKeyword(keyword) + )); + % endfor + }, + Some(None) => { + % for sub_property in shorthand.sub_properties: + result_list.push(${sub_property.ident}_declaration( + CSSWideKeyword(${ + "Inherit" if sub_property.style_struct.inherited else "Initial"}) + )); + % endfor + }, + None => match shorthands::${shorthand.ident}::parse(value, base_url) { + Some(result) => { + % for sub_property in shorthand.sub_properties: + result_list.push(${sub_property.ident}_declaration( + match result.${sub_property.ident} { + Some(value) => SpecifiedValue(value), + None => CSSWideKeyword(Initial), + } + )); + % endfor + }, + None => return InvalidValue, + } + }, + % endfor _ => return UnknownProperty, } ValidDeclaration diff --git a/src/components/style/stylesheets.rs b/src/components/style/stylesheets.rs index 0b5b1406457..6c66b707022 100644 --- a/src/components/style/stylesheets.rs +++ b/src/components/style/stylesheets.rs @@ -75,7 +75,7 @@ impl Stylesheet { match rule { QualifiedRule(rule) => { next_state = STATE_BODY; - parse_style_rule(rule, &mut rules, &namespaces) + parse_style_rule(rule, &mut rules, &namespaces, &base_url) }, AtRule(rule) => { let lower_name = rule.name.to_ascii_lower(); @@ -112,7 +112,7 @@ impl Stylesheet { }, _ => { next_state = STATE_BODY; - parse_nested_at_rule(lower_name, rule, &mut rules, &namespaces) + parse_nested_at_rule(lower_name, rule, &mut rules, &namespaces, &base_url) }, } }, @@ -125,14 +125,14 @@ impl Stylesheet { pub fn parse_style_rule(rule: QualifiedRule, parent_rules: &mut ~[CSSRule], - namespaces: &NamespaceMap) { + namespaces: &NamespaceMap, base_url: &Url) { let QualifiedRule{location: location, prelude: prelude, block: block} = rule; // FIXME: avoid doing this for valid selectors let serialized = prelude.iter().to_css(); match selectors::parse_selector_list(prelude, namespaces) { Some(selectors) => parent_rules.push(CSSStyleRule(StyleRule{ selectors: selectors, - declarations: properties::parse_property_declaration_list(block.move_iter()) + declarations: properties::parse_property_declaration_list(block.move_iter(), base_url) })), None => log_css_error(location, format!( "Invalid/unsupported selector: {}", serialized)), @@ -142,9 +142,9 @@ pub fn parse_style_rule(rule: QualifiedRule, parent_rules: &mut ~[CSSRule], // lower_name is passed explicitly to avoid computing it twice. pub fn parse_nested_at_rule(lower_name: &str, rule: AtRule, - parent_rules: &mut ~[CSSRule], namespaces: &NamespaceMap) { + parent_rules: &mut ~[CSSRule], namespaces: &NamespaceMap, base_url: &Url) { match lower_name { - "media" => parse_media_rule(rule, parent_rules, namespaces), + "media" => parse_media_rule(rule, parent_rules, namespaces, base_url), _ => log_css_error(rule.location, format!("Unsupported at-rule: @{:s}", lower_name)) } } |