From 617e8e97689e8646ba48c1b5b620c9ae26567044 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Fri, 14 Apr 2017 10:41:05 +0200 Subject: [PATCH] CSSOM requires @counter-style to keep track of which descriptors were specified --- components/style/counter_style.rs | 48 ++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/components/style/counter_style.rs b/components/style/counter_style.rs index 95e3d971f53..763037884b1 100644 --- a/components/style/counter_style.rs +++ b/components/style/counter_style.rs @@ -14,6 +14,7 @@ use cssparser::{serialize_string, serialize_identifier}; use parser::{ParserContext, log_css_error, Parse}; use shared_lock::{SharedRwLockReadGuard, ToCssWithGuard}; use std::ascii::AsciiExt; +use std::borrow::Cow; use std::fmt; use std::ops::Range; use style_traits::{ToCss, OneOrMoreCommaSeparated}; @@ -27,7 +28,7 @@ pub fn parse_counter_style_name(input: &mut Parser) -> Result { /// Parse the body (inside `{}`) of an @counter-style rule pub fn parse_counter_style_body(name: CustomIdent, context: &ParserContext, input: &mut Parser) -> Result { - let mut rule = CounterStyleRule::initial(name); + let mut rule = CounterStyleRule::empty(name); { let parser = CounterStyleRuleParser { context: context, @@ -68,25 +69,38 @@ macro_rules! counter_style_descriptors { name: CustomIdent, $( #[$doc] - $ident: $ty, + $ident: Option<$ty>, )+ } impl CounterStyleRule { - fn initial(name: CustomIdent) -> Self { + fn empty(name: CustomIdent) -> Self { CounterStyleRule { name: name, $( - $ident: $initial, + $ident: None, )+ } } + $( + #[$doc] + pub fn $ident(&self) -> Cow<$ty> { + if let Some(ref value) = self.$ident { + Cow::Borrowed(value) + } else { + Cow::Owned($initial) + } + } + )+ + /// Convert to Gecko types #[cfg(feature = "gecko")] pub fn set_descriptors(&self, descriptors: &mut CounterStyleDescriptors) { $( - descriptors[nsCSSCounterDesc::$gecko_ident as usize].set_from(&self.$ident); + if let Some(ref value) = self.$ident { + descriptors[nsCSSCounterDesc::$gecko_ident as usize].set_from(value) + } )* } } @@ -103,7 +117,7 @@ macro_rules! counter_style_descriptors { // but in this case we do because we set the value as a side effect // rather than returning it. let value = input.parse_entirely(|i| Parse::parse(self.context, i))?; - self.rule.$ident = value + self.rule.$ident = Some(value) } )* _ => return Err(()) @@ -119,9 +133,11 @@ macro_rules! counter_style_descriptors { self.name.to_css(dest)?; dest.write_str(" {\n")?; $( - dest.write_str(concat!(" ", $name, ": "))?; - ToCss::to_css(&self.$ident, dest)?; - dest.write_str(";\n")?; + if let Some(ref value) = self.$ident { + dest.write_str(concat!(" ", $name, ": "))?; + ToCss::to_css(value, dest)?; + dest.write_str(";\n")?; + } )+ dest.write_str("}") } @@ -159,7 +175,7 @@ counter_style_descriptors! { } /// https://drafts.csswg.org/css-counter-styles/#counter-style-system -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum System { /// 'cyclic' Cyclic, @@ -225,7 +241,7 @@ impl ToCss for System { } /// https://drafts.csswg.org/css-counter-styles/#typedef-symbol -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum Symbol { /// String(String), @@ -256,7 +272,7 @@ impl ToCss for Symbol { } /// https://drafts.csswg.org/css-counter-styles/#counter-style-negative -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Negative(pub Symbol, pub Option); impl Parse for Negative { @@ -282,7 +298,7 @@ impl ToCss for Negative { /// https://drafts.csswg.org/css-counter-styles/#counter-style-range /// /// Empty Vec represents 'auto' -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Ranges(pub Vec>>); impl Parse for Ranges { @@ -344,7 +360,7 @@ fn bound_to_css(range: Option, dest: &mut W) -> fmt::Result where W: fmt } /// https://drafts.csswg.org/css-counter-styles/#counter-style-pad -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Pad(pub u32, pub Symbol); impl Parse for Pad { @@ -368,7 +384,7 @@ impl ToCss for Pad { } /// https://drafts.csswg.org/css-counter-styles/#counter-style-fallback -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Fallback(pub CustomIdent); impl Parse for Fallback { @@ -384,7 +400,7 @@ impl ToCss for Fallback { } /// https://drafts.csswg.org/css-counter-styles/#counter-style-symbols -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Symbols(pub Vec); impl Parse for Symbols {