Skip to content

Commit

Permalink
style: [css-lists-3] Make 'none' invalid as a <counter-style> in coun…
Browse files Browse the repository at this point in the history
  • Loading branch information
Mats Palmgren authored and emilio committed Sep 12, 2019
1 parent 1406ae7 commit f7a87c4
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 63 deletions.
23 changes: 9 additions & 14 deletions components/style/gecko/values.rs
Expand Up @@ -8,7 +8,7 @@

use crate::counter_style::{Symbol, Symbols};
use crate::gecko_bindings::structs::CounterStylePtr;
use crate::values::generics::CounterStyleOrNone;
use crate::values::generics::CounterStyle;
use crate::values::Either;
use crate::Atom;
use app_units::Au;
Expand Down Expand Up @@ -49,19 +49,17 @@ pub fn round_border_to_device_pixels(width: Au, au_per_device_px: Au) -> Au {
}
}

impl CounterStyleOrNone {
impl CounterStyle {
/// Convert this counter style to a Gecko CounterStylePtr.
pub fn to_gecko_value(self, gecko_value: &mut CounterStylePtr) {
use crate::gecko_bindings::bindings::Gecko_SetCounterStyleToName as set_name;
use crate::gecko_bindings::bindings::Gecko_SetCounterStyleToSymbols as set_symbols;
match self {
CounterStyleOrNone::None => unsafe {
set_name(gecko_value, atom!("none").into_addrefed());
},
CounterStyleOrNone::Name(name) => unsafe {
CounterStyle::Name(name) => unsafe {
debug_assert_ne!(name.0, atom!("none"));
set_name(gecko_value, name.0.into_addrefed());
},
CounterStyleOrNone::Symbols(symbols_type, symbols) => {
CounterStyle::Symbols(symbols_type, symbols) => {
let symbols: Vec<_> = symbols
.0
.iter()
Expand All @@ -86,7 +84,7 @@ impl CounterStyleOrNone {
}
}

/// Convert Gecko CounterStylePtr to CounterStyleOrNone or String.
/// Convert Gecko CounterStylePtr to CounterStyle or String.
pub fn from_gecko_value(gecko_value: &CounterStylePtr) -> Either<Self, String> {
use crate::gecko_bindings::bindings;
use crate::values::generics::SymbolsType;
Expand All @@ -95,11 +93,8 @@ impl CounterStyleOrNone {
let name = unsafe { bindings::Gecko_CounterStyle_GetName(gecko_value) };
if !name.is_null() {
let name = unsafe { Atom::from_raw(name) };
if name == atom!("none") {
Either::First(CounterStyleOrNone::None)
} else {
Either::First(CounterStyleOrNone::Name(CustomIdent(name)))
}
debug_assert_ne!(name, atom!("none"));
Either::First(CounterStyle::Name(CustomIdent(name)))
} else {
let anonymous =
unsafe { bindings::Gecko_CounterStyle_GetAnonymous(gecko_value).as_ref() }.unwrap();
Expand All @@ -113,7 +108,7 @@ impl CounterStyleOrNone {
.iter()
.map(|gecko_symbol| Symbol::String(gecko_symbol.to_string().into()))
.collect();
Either::First(CounterStyleOrNone::Symbols(symbol_type, Symbols(symbols)))
Either::First(CounterStyle::Symbols(symbol_type, Symbols(symbols)))
}
}
}
Expand Down
27 changes: 21 additions & 6 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -2199,10 +2199,15 @@ fn static_assert() {
}

pub fn set_list_style_type(&mut self, v: longhands::list_style_type::computed_value::T) {
use crate::gecko_bindings::bindings::Gecko_SetCounterStyleToName;
use crate::gecko_bindings::bindings::Gecko_SetCounterStyleToString;
use nsstring::{nsACString, nsCStr};
use self::longhands::list_style_type::computed_value::T;
match v {
T::None => unsafe {
Gecko_SetCounterStyleToName(&mut self.gecko.mCounterStyle,
atom!("none").into_addrefed());
}
T::CounterStyle(s) => s.to_gecko_value(&mut self.gecko.mCounterStyle),
T::String(s) => unsafe {
Gecko_SetCounterStyleToString(&mut self.gecko.mCounterStyle,
Expand All @@ -2224,9 +2229,19 @@ fn static_assert() {
pub fn clone_list_style_type(&self) -> longhands::list_style_type::computed_value::T {
use self::longhands::list_style_type::computed_value::T;
use crate::values::Either;
use crate::values::generics::CounterStyleOrNone;
use crate::values::generics::CounterStyle;
use crate::gecko_bindings::bindings;

let result = CounterStyleOrNone::from_gecko_value(&self.gecko.mCounterStyle);
let name = unsafe {
bindings::Gecko_CounterStyle_GetName(&self.gecko.mCounterStyle)
};
if !name.is_null() {
let name = unsafe { Atom::from_raw(name) };
if name == atom!("none") {
return T::None;
}
}
let result = CounterStyle::from_gecko_value(&self.gecko.mCounterStyle);
match result {
Either::First(counter_style) => T::CounterStyle(counter_style),
Either::Second(string) => T::String(string),
Expand Down Expand Up @@ -2573,7 +2588,7 @@ clip-path
pub fn set_content(&mut self, v: longhands::content::computed_value::T) {
use crate::values::CustomIdent;
use crate::values::generics::counters::{Content, ContentItem};
use crate::values::generics::CounterStyleOrNone;
use crate::values::generics::CounterStyle;
use crate::gecko_bindings::structs::nsStyleContentData;
use crate::gecko_bindings::structs::nsStyleContentAttr;
use crate::gecko_bindings::structs::StyleContentType;
Expand All @@ -2594,7 +2609,7 @@ clip-path
content_type: StyleContentType,
name: CustomIdent,
sep: &str,
style: CounterStyleOrNone,
style: CounterStyle,
) {
debug_assert!(content_type == StyleContentType::Counter ||
content_type == StyleContentType::Counters);
Expand Down Expand Up @@ -2724,7 +2739,7 @@ clip-path
use crate::gecko_bindings::structs::StyleContentType;
use crate::values::generics::counters::{Content, ContentItem};
use crate::values::{CustomIdent, Either};
use crate::values::generics::CounterStyleOrNone;
use crate::values::generics::CounterStyle;
use crate::values::specified::Attr;

if self.gecko.mContents.is_empty() {
Expand Down Expand Up @@ -2769,7 +2784,7 @@ clip-path
Atom::from_raw(gecko_function.mIdent.mRawPtr)
});
let style =
CounterStyleOrNone::from_gecko_value(&gecko_function.mCounterStyle);
CounterStyle::from_gecko_value(&gecko_function.mCounterStyle);
let style = match style {
Either::First(counter_style) => counter_style,
Either::Second(_) =>
Expand Down
16 changes: 4 additions & 12 deletions components/style/properties/shorthands/list.mako.rs
Expand Up @@ -61,31 +61,23 @@

let position = unwrap_or_initial!(list_style_position, position);

fn list_style_type_none() -> list_style_type::SpecifiedValue {
% if engine == "gecko":
use crate::values::generics::CounterStyleOrNone;
list_style_type::SpecifiedValue::CounterStyle(CounterStyleOrNone::None)
% else:
list_style_type::SpecifiedValue::None
% endif
}

// If there are two `none`s, then we can't have a type or image; if there is one `none`,
// then we can't have both a type *and* an image; if there is no `none` then we're fine as
// long as we parsed something.
use self::list_style_type::SpecifiedValue as ListStyleType;
match (any, nones, list_style_type, image) {
(true, 2, None, None) => {
Ok(expanded! {
list_style_position: position,
list_style_image: ImageUrlOrNone::none(),
list_style_type: list_style_type_none(),
list_style_type: ListStyleType::None,
})
}
(true, 1, None, Some(image)) => {
Ok(expanded! {
list_style_position: position,
list_style_image: image,
list_style_type: list_style_type_none(),
list_style_type: ListStyleType::None,
})
}
(true, 1, Some(list_style_type), None) => {
Expand All @@ -99,7 +91,7 @@
Ok(expanded! {
list_style_position: position,
list_style_image: ImageUrlOrNone::none(),
list_style_type: list_style_type_none(),
list_style_type: ListStyleType::None,
})
}
(true, 0, list_style_type, image) => {
Expand Down
6 changes: 3 additions & 3 deletions components/style/values/generics/counters.rs
Expand Up @@ -7,7 +7,7 @@
#[cfg(feature = "servo")]
use crate::computed_values::list_style_type::T as ListStyleType;
#[cfg(feature = "gecko")]
use crate::values::generics::CounterStyleOrNone;
use crate::values::generics::CounterStyle;
#[cfg(feature = "gecko")]
use crate::values::specified::Attr;
use crate::values::CustomIdent;
Expand Down Expand Up @@ -127,7 +127,7 @@ impl<I> Counters<I> {
type CounterStyleType = ListStyleType;

#[cfg(feature = "gecko")]
type CounterStyleType = CounterStyleOrNone;
type CounterStyleType = CounterStyle;

#[cfg(feature = "servo")]
#[inline]
Expand All @@ -138,7 +138,7 @@ fn is_decimal(counter_type: &CounterStyleType) -> bool {
#[cfg(feature = "gecko")]
#[inline]
fn is_decimal(counter_type: &CounterStyleType) -> bool {
*counter_type == CounterStyleOrNone::decimal()
*counter_type == CounterStyle::decimal()
}

/// The specified value for the `content` property.
Expand Down
26 changes: 10 additions & 16 deletions components/style/values/generics/mod.rs
Expand Up @@ -92,13 +92,10 @@ impl SymbolsType {

/// <https://drafts.csswg.org/css-counter-styles/#typedef-counter-style>
///
/// Since wherever <counter-style> is used, 'none' is a valid value as
/// well, we combine them into one type to make code simpler.
/// Note that 'none' is not a valid name.
#[cfg_attr(feature = "gecko", derive(MallocSizeOf))]
#[derive(Clone, Debug, Eq, PartialEq, ToComputedValue, ToCss, ToResolvedValue, ToShmem)]
pub enum CounterStyleOrNone {
/// `none`
None,
pub enum CounterStyle {
/// `<counter-style-name>`
Name(CustomIdent),
/// `symbols()`
Expand All @@ -111,28 +108,25 @@ fn is_symbolic(symbols_type: &SymbolsType) -> bool {
*symbols_type == SymbolsType::Symbolic
}

impl CounterStyleOrNone {
impl CounterStyle {
/// disc value
pub fn disc() -> Self {
CounterStyleOrNone::Name(CustomIdent(atom!("disc")))
CounterStyle::Name(CustomIdent(atom!("disc")))
}

/// decimal value
pub fn decimal() -> Self {
CounterStyleOrNone::Name(CustomIdent(atom!("decimal")))
CounterStyle::Name(CustomIdent(atom!("decimal")))
}
}

impl Parse for CounterStyleOrNone {
impl Parse for CounterStyle {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
if let Ok(name) = input.try(|i| parse_counter_style_name(i)) {
return Ok(CounterStyleOrNone::Name(name));
}
if input.try(|i| i.expect_ident_matching("none")).is_ok() {
return Ok(CounterStyleOrNone::None);
return Ok(CounterStyle::Name(name));
}
input.expect_function_matching("symbols")?;
input.parse_nested_block(|input| {
Expand All @@ -151,12 +145,12 @@ impl Parse for CounterStyleOrNone {
if symbols.0.iter().any(|sym| !sym.is_allowed_in_symbols()) {
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
}
Ok(CounterStyleOrNone::Symbols(symbols_type, symbols))
Ok(CounterStyle::Symbols(symbols_type, symbols))
})
}
}

impl SpecifiedValueInfo for CounterStyleOrNone {
impl SpecifiedValueInfo for CounterStyle {
fn collect_completion_keywords(f: KeywordsCollectFn) {
// XXX The best approach for implementing this is probably
// having a CounterStyleName type wrapping CustomIdent, and
Expand All @@ -165,7 +159,7 @@ impl SpecifiedValueInfo for CounterStyleOrNone {
// approach here.
macro_rules! predefined {
($($name:expr,)+) => {
f(&["none", "symbols", $($name,)+]);
f(&["symbols", $($name,)+]);
}
}
include!("../../counter_style/predefined.rs");
Expand Down
8 changes: 4 additions & 4 deletions components/style/values/specified/counters.rs
Expand Up @@ -12,7 +12,7 @@ use crate::values::generics::counters::CounterIncrement as GenericCounterIncreme
use crate::values::generics::counters::CounterPair;
use crate::values::generics::counters::CounterSetOrReset as GenericCounterSetOrReset;
#[cfg(feature = "gecko")]
use crate::values::generics::CounterStyleOrNone;
use crate::values::generics::CounterStyle;
use crate::values::specified::url::SpecifiedImageUrl;
#[cfg(feature = "gecko")]
use crate::values::specified::Attr;
Expand Down Expand Up @@ -98,13 +98,13 @@ impl Content {
}

#[cfg(feature = "gecko")]
fn parse_counter_style(context: &ParserContext, input: &mut Parser) -> CounterStyleOrNone {
fn parse_counter_style(context: &ParserContext, input: &mut Parser) -> CounterStyle {
input
.try(|input| {
input.expect_comma()?;
CounterStyleOrNone::parse(context, input)
CounterStyle::parse(context, input)
})
.unwrap_or(CounterStyleOrNone::decimal())
.unwrap_or(CounterStyle::decimal())
}
}

Expand Down
20 changes: 12 additions & 8 deletions components/style/values/specified/list.rs
Expand Up @@ -6,7 +6,7 @@

use crate::parser::{Parse, ParserContext};
#[cfg(feature = "gecko")]
use crate::values::generics::CounterStyleOrNone;
use crate::values::generics::CounterStyle;
#[cfg(feature = "gecko")]
use crate::values::CustomIdent;
use cssparser::{Parser, Token};
Expand All @@ -27,8 +27,10 @@ use style_traits::{ParseError, StyleParseErrorKind};
ToShmem,
)]
pub enum ListStyleType {
/// <counter-style> | none
CounterStyle(CounterStyleOrNone),
/// `none`
None,
/// <counter-style>
CounterStyle(CounterStyle),
/// <string>
String(String),
}
Expand All @@ -38,7 +40,7 @@ impl ListStyleType {
/// Initial specified value for `list-style-type`.
#[inline]
pub fn disc() -> Self {
ListStyleType::CounterStyle(CounterStyleOrNone::disc())
ListStyleType::CounterStyle(CounterStyle::disc())
}

/// Convert from gecko keyword to list-style-type.
Expand All @@ -50,10 +52,10 @@ impl ListStyleType {
use crate::gecko_bindings::structs;

if value == structs::NS_STYLE_LIST_STYLE_NONE {
return ListStyleType::CounterStyle(CounterStyleOrNone::None);
return ListStyleType::None;
}

ListStyleType::CounterStyle(CounterStyleOrNone::Name(CustomIdent(match value {
ListStyleType::CounterStyle(CounterStyle::Name(CustomIdent(match value {
structs::NS_STYLE_LIST_STYLE_DISC => atom!("disc"),
structs::NS_STYLE_LIST_STYLE_CIRCLE => atom!("circle"),
structs::NS_STYLE_LIST_STYLE_SQUARE => atom!("square"),
Expand All @@ -73,10 +75,12 @@ impl Parse for ListStyleType {
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
if let Ok(style) = input.try(|i| CounterStyleOrNone::parse(context, i)) {
if let Ok(style) = input.try(|i| CounterStyle::parse(context, i)) {
return Ok(ListStyleType::CounterStyle(style));
}

if input.try(|i| i.expect_ident_matching("none")).is_ok() {
return Ok(ListStyleType::None);
}
Ok(ListStyleType::String(
input.expect_string()?.as_ref().to_owned(),
))
Expand Down

0 comments on commit f7a87c4

Please sign in to comment.