diff options
author | Oriol Brufau <obrufau@igalia.com> | 2023-05-16 08:05:27 +0200 |
---|---|---|
committer | Oriol Brufau <obrufau@igalia.com> | 2023-05-16 12:56:06 +0200 |
commit | 17ae374c655a94c6be394f1b11c6a7095c46506c (patch) | |
tree | d15ebb8b41754aa46fd0f711f629c80505b59848 /components/style/values/specified | |
parent | b222bd3162b36aa91863f794709eaa94d31b8ea4 (diff) | |
download | servo-17ae374c655a94c6be394f1b11c6a7095c46506c.tar.gz servo-17ae374c655a94c6be394f1b11c6a7095c46506c.zip |
style: part 1 - Support parsing ruby-position: alternate
Differential Revision: https://phabricator.services.mozilla.com/D107382
Diffstat (limited to 'components/style/values/specified')
-rw-r--r-- | components/style/values/specified/mod.rs | 1 | ||||
-rw-r--r-- | components/style/values/specified/text.rs | 83 |
2 files changed, 84 insertions, 0 deletions
diff --git a/components/style/values/specified/mod.rs b/components/style/values/specified/mod.rs index d91f9b8195f..946bf3c2cc9 100644 --- a/components/style/values/specified/mod.rs +++ b/components/style/values/specified/mod.rs @@ -86,6 +86,7 @@ pub use self::svg::{SVGPaintOrder, SVGStrokeDashArray, SVGWidth}; pub use self::svg_path::SVGPathData; pub use self::text::TextAlignLast; pub use self::text::TextUnderlinePosition; +pub use self::text::RubyPosition; pub use self::text::{InitialLetter, LetterSpacing, LineBreak, LineHeight, TextAlign}; pub use self::text::{OverflowWrap, TextEmphasisPosition, TextEmphasisStyle, WordBreak}; pub use self::text::{TextAlignKeyword, TextDecorationLine, TextOverflow, WordSpacing}; diff --git a/components/style/values/specified/text.rs b/components/style/values/specified/text.rs index 7c5c712eb3e..3fe3b92c354 100644 --- a/components/style/values/specified/text.rs +++ b/components/style/values/specified/text.rs @@ -22,6 +22,7 @@ use selectors::parser::SelectorParseErrorKind; use std::fmt::{self, Write}; use style_traits::values::SequenceWriter; use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss}; +use style_traits::{KeywordsCollectFn, SpecifiedValueInfo}; use unicode_segmentation::UnicodeSegmentation; /// A specified type for the `initial-letter` property. @@ -1193,3 +1194,85 @@ impl ToCss for TextUnderlinePosition { Ok(()) } } + +/// Values for `ruby-position` property +#[repr(u8)] +#[derive( + Clone, + Copy, + Debug, + Eq, + MallocSizeOf, + PartialEq, + ToComputedValue, + ToResolvedValue, + ToShmem, +)] +#[allow(missing_docs)] +pub enum RubyPosition { + AlternateOver, + AlternateUnder, + Over, + Under, +} + +impl Default for RubyPosition { + fn default() -> Self { + if static_prefs::pref!("layout.css.ruby.position-alternate.enabled") { + RubyPosition::AlternateOver + } else { + RubyPosition::Over + } + } +} + +impl Parse for RubyPosition { + fn parse<'i, 't>( + _context: &ParserContext, + input: &mut Parser<'i, 't>, + ) -> Result<RubyPosition, ParseError<'i>> { + // Parse alternate before + let alternate_enabled = static_prefs::pref!("layout.css.ruby.position-alternate.enabled"); + let alternate = alternate_enabled && + input.try_parse(|i| i.expect_ident_matching("alternate")).is_ok(); + if alternate && input.is_exhausted() { + return Ok(RubyPosition::AlternateOver); + } + // Parse over / under + let over = try_match_ident_ignore_ascii_case! { input, + "over" => true, + "under" => false, + }; + // Parse alternate after + let alternate = alternate || + (alternate_enabled && + input.try_parse(|i| i.expect_ident_matching("alternate")).is_ok()); + + Ok(match (over, alternate) { + (true, true) => RubyPosition::AlternateOver, + (false, true) => RubyPosition::AlternateUnder, + (true, false) => RubyPosition::Over, + (false, false) => RubyPosition::Under, + }) + } +} + +impl ToCss for RubyPosition { + fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result + where + W: Write, + { + dest.write_str(match self { + RubyPosition::AlternateOver => "alternate", + RubyPosition::AlternateUnder => "alternate under", + RubyPosition::Over => "over", + RubyPosition::Under => "under", + }) + } +} + +impl SpecifiedValueInfo for RubyPosition { + fn collect_completion_keywords(f: KeywordsCollectFn) { + f(&["alternate", "over", "under"]) + } +} |