Move text-emphasis-style variables out of mako templates

This commit is contained in:
Dmitry 2018-03-06 00:10:28 +03:00
parent f1338d3df8
commit 73113e28dd
7 changed files with 175 additions and 177 deletions

View file

@ -195,166 +195,16 @@ ${helpers.predefined_type(
spec="https://drafts.csswg.org/css-text-decor-3/#text-shadow-property",
)}
<%helpers:longhand name="text-emphasis-style" products="gecko" boxed="True"
animation_value_type="discrete"
spec="https://drafts.csswg.org/css-text-decor/#propdef-text-emphasis-style">
use computed_values::writing_mode::T as WritingMode;
use unicode_segmentation::UnicodeSegmentation;
pub mod computed_value {
#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToCss)]
#[cfg_attr(feature = "servo", derive(ToComputedValue))]
pub enum T {
Keyword(KeywordValue),
None,
String(String),
}
#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToCss)]
pub struct KeywordValue {
pub fill: super::FillMode,
pub shape: super::ShapeKeyword,
}
}
#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToCss)]
pub enum SpecifiedValue {
Keyword(KeywordValue),
None,
String(String),
}
#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToCss)]
pub enum KeywordValue {
Fill(FillMode),
Shape(ShapeKeyword),
FillAndShape(FillMode, ShapeKeyword),
}
impl KeywordValue {
fn fill(&self) -> Option<FillMode> {
match *self {
KeywordValue::Fill(fill) |
KeywordValue::FillAndShape(fill, _) => Some(fill),
_ => None,
}
}
fn shape(&self) -> Option<ShapeKeyword> {
match *self {
KeywordValue::Shape(shape) |
KeywordValue::FillAndShape(_, shape) => Some(shape),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, MallocSizeOf, Parse, PartialEq, ToCss)]
pub enum FillMode {
Filled,
Open,
}
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq, ToCss)]
pub enum ShapeKeyword {
Dot,
Circle,
DoubleCircle,
Triangle,
Sesame,
}
impl ShapeKeyword {
pub fn char(&self, fill: FillMode) -> &str {
let fill = fill == FillMode::Filled;
match *self {
ShapeKeyword::Dot => if fill { "\u{2022}" } else { "\u{25e6}" },
ShapeKeyword::Circle => if fill { "\u{25cf}" } else { "\u{25cb}" },
ShapeKeyword::DoubleCircle => if fill { "\u{25c9}" } else { "\u{25ce}" },
ShapeKeyword::Triangle => if fill { "\u{25b2}" } else { "\u{25b3}" },
ShapeKeyword::Sesame => if fill { "\u{fe45}" } else { "\u{fe46}" },
}
}
}
#[inline]
pub fn get_initial_value() -> computed_value::T {
computed_value::T::None
}
#[inline]
pub fn get_initial_specified_value() -> SpecifiedValue {
SpecifiedValue::None
}
impl ToComputedValue for SpecifiedValue {
type ComputedValue = computed_value::T;
#[inline]
fn to_computed_value(&self, context: &Context) -> computed_value::T {
match *self {
SpecifiedValue::Keyword(ref keyword) => {
let default_shape = if context.style().get_inheritedbox()
.clone_writing_mode() == WritingMode::HorizontalTb {
ShapeKeyword::Circle
} else {
ShapeKeyword::Sesame
};
computed_value::T::Keyword(computed_value::KeywordValue {
fill: keyword.fill().unwrap_or(FillMode::Filled),
shape: keyword.shape().unwrap_or(default_shape),
})
},
SpecifiedValue::None => computed_value::T::None,
SpecifiedValue::String(ref s) => {
// Passing `true` to iterate over extended grapheme clusters, following
// recommendation at http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries
let string = s.graphemes(true).next().unwrap_or("").to_string();
computed_value::T::String(string)
}
}
}
#[inline]
fn from_computed_value(computed: &computed_value::T) -> Self {
match *computed {
computed_value::T::Keyword(ref keyword) =>
SpecifiedValue::Keyword(KeywordValue::FillAndShape(keyword.fill,keyword.shape)),
computed_value::T::None => SpecifiedValue::None,
computed_value::T::String(ref string) => SpecifiedValue::String(string.clone())
}
}
}
pub fn parse<'i, 't>(
_context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<SpecifiedValue, ParseError<'i>> {
if input.try(|input| input.expect_ident_matching("none")).is_ok() {
return Ok(SpecifiedValue::None);
}
if let Ok(s) = input.try(|i| i.expect_string().map(|s| s.as_ref().to_owned())) {
// Handle <string>
return Ok(SpecifiedValue::String(s));
}
// Handle a pair of keywords
let mut shape = input.try(ShapeKeyword::parse).ok();
let fill = input.try(FillMode::parse).ok();
if shape.is_none() {
shape = input.try(ShapeKeyword::parse).ok();
}
// At least one of shape or fill must be handled
let keyword_value = match (fill, shape) {
(Some(fill), Some(shape)) => KeywordValue::FillAndShape(fill, shape),
(Some(fill), None) => KeywordValue::Fill(fill),
(None, Some(shape)) => KeywordValue::Shape(shape),
_ => return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)),
};
Ok(SpecifiedValue::Keyword(keyword_value))
}
</%helpers:longhand>
${helpers.predefined_type(
"text-emphasis-style",
"TextEmphasisStyle",
None,
initial_specified_value="SpecifiedValue::None",
products="gecko",
boxed=True,
animation_value_type="discrete",
spec="https://drafts.csswg.org/css-text-decor/#propdef-text-emphasis-style",
)}
<%helpers:longhand name="text-emphasis-position" animation_value_type="discrete" products="gecko"
spec="https://drafts.csswg.org/css-text-decor/#propdef-text-emphasis-position">