style: Convert text-emphasis-position #defines to enum classes

Differential Revision: https://phabricator.services.mozilla.com/D155557
This commit is contained in:
Anurag Kalia 2022-09-27 07:38:08 +00:00 committed by Martin Robinson
parent 6cfdd989d5
commit 1a9198a5ef
3 changed files with 36 additions and 145 deletions

View file

@ -221,9 +221,9 @@ ${helpers.predefined_type(
${helpers.predefined_type( ${helpers.predefined_type(
"text-emphasis-position", "text-emphasis-position",
"TextEmphasisPosition", "TextEmphasisPosition",
"computed::TextEmphasisPosition::over_right()", "computed::TextEmphasisPosition::DEFAULT",
engines="gecko", engines="gecko",
initial_specified_value="specified::TextEmphasisPosition::over_right()", initial_specified_value="specified::TextEmphasisPosition::DEFAULT",
animation_value_type="discrete", animation_value_type="discrete",
spec="https://drafts.csswg.org/css-text-decor/#propdef-text-emphasis-position", spec="https://drafts.csswg.org/css-text-decor/#propdef-text-emphasis-position",
)} )}

View file

@ -771,146 +771,32 @@ impl Parse for TextEmphasisStyle {
} }
} }
/// The allowed horizontal values for the `text-emphasis-position` property. bitflags! {
#[derive( #[derive(MallocSizeOf, SpecifiedValueInfo, ToComputedValue, ToResolvedValue, ToShmem, Parse, ToCss)]
Clone, #[repr(C)]
Copy, #[css(bitflags(mixed="over,under,left,right", validate_mixed="Self::is_valid"))]
Debug, /// Values for text-emphasis-position:
Eq, /// <https://drafts.csswg.org/css-text-decor/#text-emphasis-position-property>
MallocSizeOf, pub struct TextEmphasisPosition: u8 {
Parse, /// Draws marks to the right of the text in vertical writing mode.
PartialEq, const OVER = 1 << 0;
SpecifiedValueInfo, /// Draw marks under the text in horizontal writing mode.
ToComputedValue, const UNDER = 1 << 1;
ToCss, /// Draw marks to the left of the text in vertical writing mode.
ToResolvedValue, const LEFT = 1 << 2;
ToShmem, /// Draws marks to the right of the text in vertical writing mode.
)] const RIGHT = 1 << 3;
pub enum TextEmphasisHorizontalWritingModeValue { /// Returns the initial value of `text-emphasis-position`
/// Draw marks over the text in horizontal writing mode. const DEFAULT = Self::OVER.bits | Self::RIGHT.bits;
Over, /// Non-standard behavior: Intelligent default for zh locale
/// Draw marks under the text in horizontal writing mode. const DEFAULT_ZH = Self::UNDER.bits | Self::RIGHT.bits;
Under, }
} }
/// 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 { impl TextEmphasisPosition {
#[inline] fn is_valid(self) -> bool {
/// Returns the initial value of `text-emphasis-position` return self.intersects(Self::LEFT) != self.intersects(Self::RIGHT) &&
pub fn over_right() -> Self { self.intersects(Self::OVER) != self.intersects(Self::UNDER);
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<Self, ParseError<'i>> {
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<u8> for TextEmphasisPosition {
fn from(bits: u8) -> Self {
TextEmphasisPosition::from_gecko_keyword(bits as u32)
}
}
#[cfg(feature = "gecko")]
impl From<TextEmphasisPosition> 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
} }
} }

View file

@ -328,24 +328,29 @@ fn derive_single_field_expr(
} }
#[derive(Default, FromMeta)] #[derive(Default, FromMeta)]
#[darling(default)]
pub struct CssBitflagAttrs { pub struct CssBitflagAttrs {
/// Flags that can only go on their own, comma-separated. /// Flags that can only go on their own, comma-separated.
pub single: String, pub single: Option<String>,
/// Flags that can go mixed with each other, comma-separated. /// Flags that can go mixed with each other, comma-separated.
pub mixed: String, pub mixed: Option<String>,
/// Extra validation of the resulting mixed flags. /// Extra validation of the resulting mixed flags.
#[darling(default)]
pub validate_mixed: Option<Path>, pub validate_mixed: Option<Path>,
/// Whether there are overlapping bits we need to take care of when /// Whether there are overlapping bits we need to take care of when
/// serializing. /// serializing.
#[darling(default)]
pub overlapping_bits: bool, pub overlapping_bits: bool,
} }
impl CssBitflagAttrs { impl CssBitflagAttrs {
/// Returns a vector of (rust_name, css_name) of a given flag list. /// Returns a vector of (rust_name, css_name) of a given flag list.
fn names(s: &str) -> Vec<(String, String)> { fn names(s: &Option<String>) -> Vec<(String, String)> {
s.split(',').map(|css_name| (cg::to_scream_case(css_name), css_name.to_owned())).collect() 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)> { pub fn single_flags(&self) -> Vec<(String, String)> {