From 1a9198a5ef59cbaf2417133076352cd2be0a801d Mon Sep 17 00:00:00 2001 From: Anurag Kalia Date: Tue, 27 Sep 2022 07:38:08 +0000 Subject: [PATCH] style: Convert text-emphasis-position #defines to enum classes Differential Revision: https://phabricator.services.mozilla.com/D155557 --- .../longhands/inherited_text.mako.rs | 4 +- components/style/values/specified/text.rs | 160 +++--------------- components/style_derive/to_css.rs | 17 +- 3 files changed, 36 insertions(+), 145 deletions(-) diff --git a/components/style/properties/longhands/inherited_text.mako.rs b/components/style/properties/longhands/inherited_text.mako.rs index 2eaf243dc1a..627822994bc 100644 --- a/components/style/properties/longhands/inherited_text.mako.rs +++ b/components/style/properties/longhands/inherited_text.mako.rs @@ -221,9 +221,9 @@ ${helpers.predefined_type( ${helpers.predefined_type( "text-emphasis-position", "TextEmphasisPosition", - "computed::TextEmphasisPosition::over_right()", + "computed::TextEmphasisPosition::DEFAULT", engines="gecko", - initial_specified_value="specified::TextEmphasisPosition::over_right()", + initial_specified_value="specified::TextEmphasisPosition::DEFAULT", animation_value_type="discrete", spec="https://drafts.csswg.org/css-text-decor/#propdef-text-emphasis-position", )} diff --git a/components/style/values/specified/text.rs b/components/style/values/specified/text.rs index 9c58b6a4572..02711ba61e9 100644 --- a/components/style/values/specified/text.rs +++ b/components/style/values/specified/text.rs @@ -771,146 +771,32 @@ impl Parse for TextEmphasisStyle { } } -/// The allowed horizontal values for the `text-emphasis-position` property. -#[derive( - Clone, - Copy, - Debug, - Eq, - MallocSizeOf, - Parse, - PartialEq, - SpecifiedValueInfo, - ToComputedValue, - ToCss, - ToResolvedValue, - ToShmem, -)] -pub enum TextEmphasisHorizontalWritingModeValue { - /// Draw marks over the text in horizontal writing mode. - Over, - /// Draw marks under the text in horizontal writing mode. - Under, +bitflags! { + #[derive(MallocSizeOf, SpecifiedValueInfo, ToComputedValue, ToResolvedValue, ToShmem, Parse, ToCss)] + #[repr(C)] + #[css(bitflags(mixed="over,under,left,right", validate_mixed="Self::is_valid"))] + /// Values for text-emphasis-position: + /// + pub struct TextEmphasisPosition: u8 { + /// Draws marks to the right of the text in vertical writing mode. + const OVER = 1 << 0; + /// Draw marks under the text in horizontal writing mode. + const UNDER = 1 << 1; + /// Draw marks to the left of the text in vertical writing mode. + const LEFT = 1 << 2; + /// Draws marks to the right of the text in vertical writing mode. + const RIGHT = 1 << 3; + /// Returns the initial value of `text-emphasis-position` + const DEFAULT = Self::OVER.bits | Self::RIGHT.bits; + /// Non-standard behavior: Intelligent default for zh locale + const DEFAULT_ZH = Self::UNDER.bits | Self::RIGHT.bits; + } } -/// The allowed vertical values for the `text-emphasis-position` property. -#[derive( - Clone, - Copy, - Debug, - Eq, - MallocSizeOf, - Parse, - PartialEq, - SpecifiedValueInfo, - ToComputedValue, - ToCss, - ToResolvedValue, - ToShmem, -)] -pub enum TextEmphasisVerticalWritingModeValue { - /// Draws marks to the right of the text in vertical writing mode. - Right, - /// Draw marks to the left of the text in vertical writing mode. - Left, -} - -/// Specified value of `text-emphasis-position` property. -#[derive( - Clone, - Copy, - Debug, - MallocSizeOf, - PartialEq, - SpecifiedValueInfo, - ToComputedValue, - ToCss, - ToResolvedValue, - ToShmem, -)] -pub struct TextEmphasisPosition( - pub TextEmphasisHorizontalWritingModeValue, - pub TextEmphasisVerticalWritingModeValue, -); - impl TextEmphasisPosition { - #[inline] - /// Returns the initial value of `text-emphasis-position` - pub fn over_right() -> Self { - TextEmphasisPosition( - TextEmphasisHorizontalWritingModeValue::Over, - TextEmphasisVerticalWritingModeValue::Right, - ) - } - - #[cfg(feature = "gecko")] - /// Converts an enumerated value coming from Gecko to a `TextEmphasisPosition`. - pub fn from_gecko_keyword(kw: u32) -> Self { - use crate::gecko_bindings::structs; - - let vert = if kw & structs::NS_STYLE_TEXT_EMPHASIS_POSITION_RIGHT != 0 { - TextEmphasisVerticalWritingModeValue::Right - } else { - debug_assert!(kw & structs::NS_STYLE_TEXT_EMPHASIS_POSITION_LEFT != 0); - TextEmphasisVerticalWritingModeValue::Left - }; - let horiz = if kw & structs::NS_STYLE_TEXT_EMPHASIS_POSITION_OVER != 0 { - TextEmphasisHorizontalWritingModeValue::Over - } else { - debug_assert!(kw & structs::NS_STYLE_TEXT_EMPHASIS_POSITION_UNDER != 0); - TextEmphasisHorizontalWritingModeValue::Under - }; - TextEmphasisPosition(horiz, vert) - } -} - -impl Parse for TextEmphasisPosition { - fn parse<'i, 't>( - _context: &ParserContext, - input: &mut Parser<'i, 't>, - ) -> Result> { - if let Ok(horizontal) = - input.try_parse(|input| TextEmphasisHorizontalWritingModeValue::parse(input)) - { - let vertical = TextEmphasisVerticalWritingModeValue::parse(input)?; - Ok(TextEmphasisPosition(horizontal, vertical)) - } else { - let vertical = TextEmphasisVerticalWritingModeValue::parse(input)?; - let horizontal = TextEmphasisHorizontalWritingModeValue::parse(input)?; - Ok(TextEmphasisPosition(horizontal, vertical)) - } - } -} - -#[cfg(feature = "gecko")] -impl From for TextEmphasisPosition { - fn from(bits: u8) -> Self { - TextEmphasisPosition::from_gecko_keyword(bits as u32) - } -} - -#[cfg(feature = "gecko")] -impl From for u8 { - fn from(v: TextEmphasisPosition) -> u8 { - use crate::gecko_bindings::structs; - - let mut result = match v.0 { - TextEmphasisHorizontalWritingModeValue::Over => { - structs::NS_STYLE_TEXT_EMPHASIS_POSITION_OVER - }, - TextEmphasisHorizontalWritingModeValue::Under => { - structs::NS_STYLE_TEXT_EMPHASIS_POSITION_UNDER - }, - }; - match v.1 { - TextEmphasisVerticalWritingModeValue::Right => { - result |= structs::NS_STYLE_TEXT_EMPHASIS_POSITION_RIGHT; - }, - TextEmphasisVerticalWritingModeValue::Left => { - result |= structs::NS_STYLE_TEXT_EMPHASIS_POSITION_LEFT; - }, - }; - result as u8 + fn is_valid(self) -> bool { + return self.intersects(Self::LEFT) != self.intersects(Self::RIGHT) && + self.intersects(Self::OVER) != self.intersects(Self::UNDER); } } diff --git a/components/style_derive/to_css.rs b/components/style_derive/to_css.rs index ac0bf05b900..9ffba064d16 100644 --- a/components/style_derive/to_css.rs +++ b/components/style_derive/to_css.rs @@ -328,24 +328,29 @@ fn derive_single_field_expr( } #[derive(Default, FromMeta)] +#[darling(default)] pub struct CssBitflagAttrs { /// Flags that can only go on their own, comma-separated. - pub single: String, + pub single: Option, /// Flags that can go mixed with each other, comma-separated. - pub mixed: String, + pub mixed: Option, /// Extra validation of the resulting mixed flags. - #[darling(default)] pub validate_mixed: Option, /// Whether there are overlapping bits we need to take care of when /// serializing. - #[darling(default)] pub overlapping_bits: bool, } impl CssBitflagAttrs { /// Returns a vector of (rust_name, css_name) of a given flag list. - fn names(s: &str) -> Vec<(String, String)> { - s.split(',').map(|css_name| (cg::to_scream_case(css_name), css_name.to_owned())).collect() + fn names(s: &Option) -> Vec<(String, String)> { + let s = match s { + Some(s) => s, + None => return vec![], + }; + s.split(',') + .map(|css_name| (cg::to_scream_case(css_name), css_name.to_owned())) + .collect() } pub fn single_flags(&self) -> Vec<(String, String)> {