style: Share computed and specified value representation of -moz-context-properties.

Differential Revision: https://phabricator.services.mozilla.com/D30545
This commit is contained in:
Emilio Cobos Álvarez 2019-05-16 23:03:29 +00:00
parent 2bc4c42d45
commit bbc77e3977
6 changed files with 89 additions and 70 deletions

View file

@ -174,6 +174,7 @@ impl<A: Debug, B: Debug> Debug for Either<A, B> {
ToResolvedValue,
ToShmem,
)]
#[repr(C)]
pub struct CustomIdent(pub Atom);
impl CustomIdent {

View file

@ -12,7 +12,7 @@ use crate::values::specified::AllowQuirks;
use crate::values::specified::LengthPercentage;
use crate::values::specified::{NonNegativeLengthPercentage, Opacity};
use crate::values::CustomIdent;
use cssparser::Parser;
use cssparser::{Parser, Token};
use std::fmt::{self, Write};
use style_traits::{CommaWithSpace, CssWriter, ParseError, Separator};
use style_traits::{StyleParseErrorKind, ToCss};
@ -243,11 +243,28 @@ impl ToCss for SVGPaintOrder {
}
}
bitflags! {
/// The context properties we understand.
#[derive(Default, MallocSizeOf, SpecifiedValueInfo, ToComputedValue, ToResolvedValue, ToShmem)]
#[repr(C)]
pub struct ContextPropertyBits: u8 {
/// `fill`
const FILL = 1 << 0;
/// `stroke`
const STROKE = 1 << 1;
/// `fill-opacity`
const FILL_OPACITY = 1 << 2;
/// `stroke-opacity`
const STROKE_OPACITY = 1 << 3;
}
}
/// Specified MozContextProperties value.
/// Nonstandard (https://developer.mozilla.org/en-US/docs/Web/CSS/-moz-context-properties)
#[derive(
Clone,
Debug,
Default,
MallocSizeOf,
PartialEq,
SpecifiedValueInfo,
@ -256,19 +273,65 @@ impl ToCss for SVGPaintOrder {
ToResolvedValue,
ToShmem,
)]
pub struct MozContextProperties(pub CustomIdent);
#[repr(C)]
pub struct MozContextProperties {
#[css(iterable, if_empty = "none")]
#[ignore_malloc_size_of = "Arc"]
idents: crate::ArcSlice<CustomIdent>,
#[css(skip)]
bits: ContextPropertyBits,
}
impl Parse for MozContextProperties {
fn parse<'i, 't>(
_context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<MozContextProperties, ParseError<'i>> {
let location = input.current_source_location();
let i = input.expect_ident()?;
Ok(MozContextProperties(CustomIdent::from_ident(
location,
i,
&["all", "none", "auto"],
)?))
let mut values = vec![];
let mut bits = ContextPropertyBits::empty();
loop {
{
let location = input.current_source_location();
let ident = input.expect_ident()?;
if ident.eq_ignore_ascii_case("none") && values.is_empty() {
return Ok(Self::default());
}
let ident = CustomIdent::from_ident(
location,
ident,
&["all", "none", "auto"],
)?;
if ident.0 == atom!("fill") {
bits.insert(ContextPropertyBits::FILL);
} else if ident.0 == atom!("stroke") {
bits.insert(ContextPropertyBits::STROKE);
} else if ident.0 == atom!("fill-opacity") {
bits.insert(ContextPropertyBits::FILL_OPACITY);
} else if ident.0 == atom!("stroke-opacity") {
bits.insert(ContextPropertyBits::STROKE_OPACITY);
}
values.push(ident);
}
let location = input.current_source_location();
match input.next() {
Ok(&Token::Comma) => continue,
Err(..) => break,
Ok(other) => return Err(location.new_unexpected_token_error(other.clone())),
}
}
if values.is_empty() {
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
}
Ok(MozContextProperties {
idents: crate::ArcSlice::from_iter(values.into_iter()),
bits,
})
}
}