Add 'negative' descriptor of @counter-style

This commit is contained in:
Simon Sapin 2017-04-14 09:10:31 +02:00
parent 4477a2da40
commit d1558a2025
2 changed files with 109 additions and 20 deletions

View file

@ -6,7 +6,8 @@
//! //!
//! [counter-style]: https://drafts.csswg.org/css-counter-styles/ //! [counter-style]: https://drafts.csswg.org/css-counter-styles/
use cssparser::{AtRuleParser, DeclarationListParser, DeclarationParser, Parser}; use cssparser::{AtRuleParser, DeclarationListParser, DeclarationParser, Parser, Token};
use cssparser::{serialize_string, serialize_identifier};
#[cfg(feature = "gecko")] use gecko::rules::CounterStyleDescriptors; #[cfg(feature = "gecko")] use gecko::rules::CounterStyleDescriptors;
#[cfg(feature = "gecko")] use gecko_bindings::structs::nsCSSCounterDesc; #[cfg(feature = "gecko")] use gecko_bindings::structs::nsCSSCounterDesc;
use parser::{ParserContext, log_css_error, Parse}; use parser::{ParserContext, log_css_error, Parse};
@ -56,7 +57,7 @@ impl<'a, 'b> AtRuleParser for CounterStyleRuleParser<'a, 'b> {
macro_rules! counter_style_descriptors { macro_rules! counter_style_descriptors {
( (
$( #[$doc: meta] $name: tt $ident: ident / $gecko_ident: ident: $ty: ty = $initial: expr, )+ $( #[$doc: meta] $name: tt $ident: ident / $gecko_ident: ident: $ty: ty = $initial: expr; )+
) => { ) => {
/// An @counter-style rule /// An @counter-style rule
#[derive(Debug)] #[derive(Debug)]
@ -82,7 +83,7 @@ macro_rules! counter_style_descriptors {
#[cfg(feature = "gecko")] #[cfg(feature = "gecko")]
pub fn set_descriptors(&self, descriptors: &mut CounterStyleDescriptors) { pub fn set_descriptors(&self, descriptors: &mut CounterStyleDescriptors) {
$( $(
descriptors[nsCSSCounterDesc::$gecko_ident as usize].set_from(&self.$ident) descriptors[nsCSSCounterDesc::$gecko_ident as usize].set_from(&self.$ident);
)* )*
} }
} }
@ -126,11 +127,15 @@ macro_rules! counter_style_descriptors {
} }
counter_style_descriptors! { counter_style_descriptors! {
/// The algorithm for constructing a string representation of a counter value /// https://drafts.csswg.org/css-counter-styles/#counter-style-system
"system" system / eCSSCounterDesc_System: System = System::Symbolic, "system" system / eCSSCounterDesc_System: System = System::Symbolic;
/// https://drafts.csswg.org/css-counter-styles/#counter-style-negative
"negative" negative / eCSSCounterDesc_Negative: Negative =
Negative(Symbol::String("-".to_owned()), None);
} }
/// Value of the 'system' descriptor /// https://drafts.csswg.org/css-counter-styles/#counter-style-system
#[derive(Debug)] #[derive(Debug)]
pub enum System { pub enum System {
/// 'cyclic' /// 'cyclic'
@ -195,3 +200,58 @@ impl ToCss for System {
} }
} }
} }
/// https://drafts.csswg.org/css-counter-styles/#typedef-symbol
#[derive(Debug)]
pub enum Symbol {
/// <string>
String(String),
/// <ident>
Ident(String),
// Not implemented:
// /// <image>
// Image(Image),
}
impl Parse for Symbol {
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
match input.next() {
Ok(Token::QuotedString(s)) => Ok(Symbol::String(s.into_owned())),
Ok(Token::Ident(s)) => Ok(Symbol::Ident(s.into_owned())),
_ => Err(())
}
}
}
impl ToCss for Symbol {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
Symbol::String(ref s) => serialize_string(s, dest),
Symbol::Ident(ref s) => serialize_identifier(s, dest),
}
}
}
/// https://drafts.csswg.org/css-counter-styles/#counter-style-negative
#[derive(Debug)]
pub struct Negative(pub Symbol, pub Option<Symbol>);
impl Parse for Negative {
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
Ok(Negative(
Symbol::parse(context, input)?,
input.try(|input| Symbol::parse(context, input)).ok(),
))
}
}
impl ToCss for Negative {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.0.to_css(dest)?;
if let Some(ref symbol) = self.1 {
dest.write_char(' ')?;
symbol.to_css(dest)?
}
Ok(())
}
}

View file

@ -6,7 +6,7 @@
use computed_values::{font_style, font_weight, font_stretch}; use computed_values::{font_style, font_weight, font_stretch};
use computed_values::font_family::FamilyName; use computed_values::font_family::FamilyName;
use counter_style::System; use counter_style;
use cssparser::UnicodeRange; use cssparser::UnicodeRange;
use font_face::{FontFaceRuleData, Source}; use font_face::{FontFaceRuleData, Source};
use gecko_bindings::bindings; use gecko_bindings::bindings;
@ -138,23 +138,52 @@ impl ToCssWithGuard for FontFaceRule {
/// The type of nsCSSCounterStyleRule::mValues /// The type of nsCSSCounterStyleRule::mValues
pub type CounterStyleDescriptors = [nsCSSValue; nsCSSCounterDesc::eCSSCounterDesc_COUNT as usize]; pub type CounterStyleDescriptors = [nsCSSValue; nsCSSCounterDesc::eCSSCounterDesc_COUNT as usize];
impl ToNsCssValue for System { impl ToNsCssValue for counter_style::System {
fn convert(&self, v: &mut nsCSSValue) { fn convert(&self, nscssvalue: &mut nsCSSValue) {
use counter_style::System::*;
match *self { match *self {
System::Cyclic => v.set_enum(structs::NS_STYLE_COUNTER_SYSTEM_CYCLIC as i32), Cyclic => nscssvalue.set_enum(structs::NS_STYLE_COUNTER_SYSTEM_CYCLIC as i32),
System::Numeric => v.set_enum(structs::NS_STYLE_COUNTER_SYSTEM_NUMERIC as i32), Numeric => nscssvalue.set_enum(structs::NS_STYLE_COUNTER_SYSTEM_NUMERIC as i32),
System::Alphabetic => v.set_enum(structs::NS_STYLE_COUNTER_SYSTEM_ALPHABETIC as i32), Alphabetic => nscssvalue.set_enum(structs::NS_STYLE_COUNTER_SYSTEM_ALPHABETIC as i32),
System::Symbolic => v.set_enum(structs::NS_STYLE_COUNTER_SYSTEM_SYMBOLIC as i32), Symbolic => nscssvalue.set_enum(structs::NS_STYLE_COUNTER_SYSTEM_SYMBOLIC as i32),
System::Additive => v.set_enum(structs::NS_STYLE_COUNTER_SYSTEM_ADDITIVE as i32), Additive => nscssvalue.set_enum(structs::NS_STYLE_COUNTER_SYSTEM_ADDITIVE as i32),
System::Fixed { first_symbol_value: _ } => { Fixed { first_symbol_value } => {
// FIXME: add bindings for nsCSSValue::SetPairValue or equivalent let mut a = nsCSSValue::null();
unimplemented!() let mut b = nsCSSValue::null();
a.set_enum(structs::NS_STYLE_COUNTER_SYSTEM_FIXED as i32);
b.set_integer(first_symbol_value.unwrap_or(1));
//nscssvalue.set_pair(a, b); // FIXME: add bindings for nsCSSValue::SetPairValue
} }
System::Extends(ref _other) => { Extends(ref other) => {
// FIXME: add bindings for nsCSSValue::SetPairValue or equivalent let mut a = nsCSSValue::null();
unimplemented!() let mut b = nsCSSValue::null();
a.set_enum(structs::NS_STYLE_COUNTER_SYSTEM_EXTENDS as i32);
b.set_string_from_atom(&other.0);
//nscssvalue.set_pair(a, b); // FIXME: add bindings for nsCSSValue::SetPairValue
} }
} }
} }
} }
impl ToNsCssValue for counter_style::Negative {
fn convert(&self, nscssvalue: &mut nsCSSValue) {
if let Some(ref second) = self.1 {
let mut a = nsCSSValue::null();
let mut b = nsCSSValue::null();
a.set_from(&self.0);
b.set_from(second);
//nscssvalue.set_pair(a, b); // FIXME: add bindings for nsCSSValue::SetPairValue
} else {
nscssvalue.set_from(&self.0)
}
}
}
impl ToNsCssValue for counter_style::Symbol {
fn convert(&self, nscssvalue: &mut nsCSSValue) {
match *self {
counter_style::Symbol::String(ref s) => nscssvalue.set_string(s),
counter_style::Symbol::Ident(ref s) => nscssvalue.set_ident(s),
}
}
}