Refactor CSSStyleDeclaration::setProperty to not synthesize a name: value string to parse.

This commit is contained in:
Simon Sapin 2015-07-25 00:21:12 +02:00
parent e2984349ed
commit d2bd070dc3
3 changed files with 23 additions and 20 deletions

View file

@ -16,7 +16,7 @@ use dom::window::{Window, WindowHelpers};
use util::str::DOMString; use util::str::DOMString;
use selectors::parser::PseudoElement; use selectors::parser::PseudoElement;
use string_cache::Atom; use string_cache::Atom;
use style::properties::{is_supported_property, longhands_from_shorthand, parse_style_attribute}; use style::properties::{is_supported_property, longhands_from_shorthand, parse_one_declaration};
use style::properties::PropertyDeclaration; use style::properties::PropertyDeclaration;
use std::ascii::AsciiExt; use std::ascii::AsciiExt;
@ -228,37 +228,31 @@ impl<'a> CSSStyleDeclarationMethods for &'a CSSStyleDeclaration {
} }
// Step 5 // Step 5
let priority = priority.to_ascii_lowercase(); let priority = match &*priority {
if priority != "!important" && !priority.is_empty() { "" => StylePriority::Normal,
return Ok(()); p if p.eq_ignore_ascii_case("important") => StylePriority::Important,
} _ => return Ok(()),
};
// Step 6 // Step 6
let mut synthesized_declaration = property;
synthesized_declaration.push_str(": ");
synthesized_declaration.push_str(&value);
let owner = self.owner.root(); let owner = self.owner.root();
let window = window_from_node(owner.r()); let window = window_from_node(owner.r());
let decl_block = parse_style_attribute(&synthesized_declaration, &window.r().get_url()); let declarations = parse_one_declaration(&property, &value, &window.r().get_url());
// Step 7 // Step 7
if decl_block.normal.len() == 0 { let declarations = if let Ok(declarations) = declarations {
declarations
} else {
return Ok(()); return Ok(());
} };
let owner = self.owner.root(); let owner = self.owner.root();
let element = ElementCast::from_ref(owner.r()); let element = ElementCast::from_ref(owner.r());
// Step 8 // Step 8
for decl in decl_block.normal.iter() { for decl in declarations {
// Step 9 // Step 9
let style_priority = if priority.is_empty() { element.update_inline_style(decl, priority);
StylePriority::Normal
} else {
StylePriority::Important
};
element.update_inline_style(decl.clone(), style_priority);
} }
let document = document_from_node(element); let document = document_from_node(element);

View file

@ -564,7 +564,7 @@ impl LayoutElementHelpers for LayoutJS<Element> {
} }
} }
#[derive(PartialEq)] #[derive(PartialEq, Eq, Copy, Clone)]
pub enum StylePriority { pub enum StylePriority {
Important, Important,
Normal, Normal,

View file

@ -5534,6 +5534,15 @@ pub fn parse_style_attribute(input: &str, base_url: &Url) -> PropertyDeclaration
parse_property_declaration_list(&context, &mut Parser::new(input)) parse_property_declaration_list(&context, &mut Parser::new(input))
} }
pub fn parse_one_declaration(name: &str, input: &str, base_url: &Url)
-> Result<Vec<PropertyDeclaration>, ()> {
let context = ParserContext::new(Origin::Author, base_url);
let mut results = vec![];
match PropertyDeclaration::parse(name, &context, &mut Parser::new(input), &mut results) {
PropertyDeclarationParseResult::ValidOrIgnoredDeclaration => Ok(results),
_ => Err(())
}
}
struct PropertyDeclarationParser<'a, 'b: 'a> { struct PropertyDeclarationParser<'a, 'b: 'a> {
context: &'a ParserContext<'b>, context: &'a ParserContext<'b>,