From 7af9a087aaf655490ba1e4e8060a8c074dc37176 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Mon, 24 Feb 2020 13:32:57 +0000 Subject: [PATCH] style: Use enums for text-align / text-align-last. This also fixes some backwards logic in nsBlockFrame::ReflowDirtyLines, and adds some static assertions to nsGenericHTMLElement that almost cause a very subtle bug. Depends on D63792 Differential Revision: https://phabricator.services.mozilla.com/D63793 --- components/style/gecko/conversions.rs | 36 +-------------- components/style/properties/data.py | 1 + components/style/properties/gecko.mako.rs | 9 +--- .../longhands/inherited_text.mako.rs | 7 +-- components/style/values/computed/mod.rs | 2 +- components/style/values/computed/text.rs | 6 ++- components/style/values/specified/mod.rs | 1 + components/style/values/specified/text.rs | 46 ++++++++++++++----- 8 files changed, 47 insertions(+), 61 deletions(-) diff --git a/components/style/gecko/conversions.rs b/components/style/gecko/conversions.rs index 8fbe3f47540..f4c994bb1c7 100644 --- a/components/style/gecko/conversions.rs +++ b/components/style/gecko/conversions.rs @@ -10,10 +10,9 @@ #![allow(unsafe_code)] -use crate::gecko_bindings::structs::{self, Matrix4x4Components, nsresult}; +use crate::gecko_bindings::structs::{Matrix4x4Components, nsresult}; use crate::stylesheets::RulesMutateError; use crate::values::computed::transform::Matrix3D; -use crate::values::computed::TextAlign; impl From for nsresult { fn from(other: RulesMutateError) -> Self { @@ -26,39 +25,6 @@ impl From for nsresult { } } -impl TextAlign { - /// Obtain a specified value from a Gecko keyword value - /// - /// Intended for use with presentation attributes, not style structs - pub fn from_gecko_keyword(kw: u32) -> Self { - match kw { - structs::NS_STYLE_TEXT_ALIGN_LEFT => TextAlign::Left, - structs::NS_STYLE_TEXT_ALIGN_RIGHT => TextAlign::Right, - structs::NS_STYLE_TEXT_ALIGN_CENTER => TextAlign::Center, - structs::NS_STYLE_TEXT_ALIGN_JUSTIFY => TextAlign::Justify, - structs::NS_STYLE_TEXT_ALIGN_MOZ_LEFT => TextAlign::MozLeft, - structs::NS_STYLE_TEXT_ALIGN_MOZ_RIGHT => TextAlign::MozRight, - structs::NS_STYLE_TEXT_ALIGN_MOZ_CENTER => TextAlign::MozCenter, - structs::NS_STYLE_TEXT_ALIGN_CHAR => TextAlign::Char, - structs::NS_STYLE_TEXT_ALIGN_END => TextAlign::End, - _ => panic!("Found unexpected value in style struct for text-align property"), - } - } -} - -/// Convert to String from given chars pointer. -pub unsafe fn string_from_chars_pointer(p: *const u16) -> String { - use std::slice; - let mut length = 0; - let mut iter = p; - while *iter != 0 { - length += 1; - iter = iter.offset(1); - } - let char_vec = slice::from_raw_parts(p, length as usize); - String::from_utf16_lossy(char_vec) -} - impl<'a> From<&'a Matrix4x4Components> for Matrix3D { fn from(m: &'a Matrix4x4Components) -> Matrix3D { Matrix3D { diff --git a/components/style/properties/data.py b/components/style/properties/data.py index 15ab380789b..b10ee63c6b4 100644 --- a/components/style/properties/data.py +++ b/components/style/properties/data.py @@ -382,6 +382,7 @@ class Longhand(object): "ScrollSnapStrictness", "ScrollSnapType", "TextAlign", + "TextAlignLast", "TextDecorationLine", "TextEmphasisPosition", "TextTransform", diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index 6286b81d009..908735c60a6 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -2016,16 +2016,9 @@ fn static_assert() { <%self:impl_trait style_struct_name="InheritedText" - skip_longhands="text-align -webkit-text-stroke-width"> - - <% text_align_keyword = Keyword("text-align", - "start end left right center justify -moz-center -moz-left -moz-right char", - gecko_strip_moz_prefix=False) %> - ${impl_keyword('text_align', 'mTextAlign', text_align_keyword)} - + skip_longhands="-webkit-text-stroke-width"> ${impl_non_negative_length('_webkit_text_stroke_width', 'mWebkitTextStrokeWidth')} - <%self:impl_trait style_struct_name="Text" skip_longhands="initial-letter"> diff --git a/components/style/properties/longhands/inherited_text.mako.rs b/components/style/properties/longhands/inherited_text.mako.rs index f01bedb177e..41ac96b6dd4 100644 --- a/components/style/properties/longhands/inherited_text.mako.rs +++ b/components/style/properties/longhands/inherited_text.mako.rs @@ -141,11 +141,12 @@ ${helpers.predefined_type( % endif -${helpers.single_keyword( +${helpers.predefined_type( "text-align-last", - "auto start end left right center justify", + "TextAlignLast", + "computed::text::TextAlignLast::Auto", + needs_context=False, engines="gecko", - gecko_constant_prefix="NS_STYLE_TEXT_ALIGN", animation_value_type="discrete", spec="https://drafts.csswg.org/css-text/#propdef-text-align-last", )} diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index 1566ab0f391..01bbfcf5cbd 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -78,7 +78,7 @@ pub use self::svg::{SVGPaintOrder, SVGStrokeDashArray, SVGWidth}; pub use self::text::TextUnderlinePosition; pub use self::text::{InitialLetter, LetterSpacing, LineBreak, LineHeight}; pub use self::text::{OverflowWrap, TextOverflow, WordBreak, WordSpacing}; -pub use self::text::{TextAlign, TextEmphasisPosition, TextEmphasisStyle}; +pub use self::text::{TextAlign, TextAlignLast, TextEmphasisPosition, TextEmphasisStyle}; pub use self::text::{TextDecorationLength, TextDecorationSkipInk}; pub use self::time::Time; pub use self::transform::{Rotate, Scale, Transform, TransformOperation}; diff --git a/components/style/values/computed/text.rs b/components/style/values/computed/text.rs index 0ca2e6044ed..b77695e06c0 100644 --- a/components/style/values/computed/text.rs +++ b/components/style/values/computed/text.rs @@ -18,8 +18,7 @@ use crate::Zero; use std::fmt::{self, Write}; use style_traits::{CssWriter, ToCss}; -pub use crate::values::specified::TextAlignKeyword as TextAlign; -pub use crate::values::specified::TextUnderlinePosition; +pub use crate::values::specified::text::{TextAlignLast, TextUnderlinePosition}; pub use crate::values::specified::{LineBreak, OverflowWrap, WordBreak}; pub use crate::values::specified::{TextDecorationLine, TextEmphasisPosition}; pub use crate::values::specified::{TextDecorationSkipInk, TextTransform}; @@ -30,6 +29,9 @@ pub type InitialLetter = GenericInitialLetter; /// Implements type for `text-decoration-thickness` property. pub type TextDecorationLength = GenericTextDecorationLength; +/// The computed value of `text-align`. +pub type TextAlign = specified::TextAlignKeyword; + /// A computed value for the `letter-spacing` property. #[repr(transparent)] #[derive( diff --git a/components/style/values/specified/mod.rs b/components/style/values/specified/mod.rs index 84e2b747234..d666081aa50 100644 --- a/components/style/values/specified/mod.rs +++ b/components/style/values/specified/mod.rs @@ -85,6 +85,7 @@ pub use self::text::{InitialLetter, LetterSpacing, LineBreak, LineHeight, TextAl pub use self::text::{OverflowWrap, TextEmphasisPosition, TextEmphasisStyle, WordBreak}; pub use self::text::{TextAlignKeyword, TextDecorationLine, TextOverflow, WordSpacing}; pub use self::text::{TextDecorationLength, TextDecorationSkipInk, TextTransform}; +pub use self::text::TextAlignLast; pub use self::time::Time; pub use self::transform::{Rotate, Scale, Transform}; pub use self::transform::{TransformOrigin, TransformStyle, Translate}; diff --git a/components/style/values/specified/text.rs b/components/style/values/specified/text.rs index 1c231b0a9cc..8f9a9bcb1cd 100644 --- a/components/style/values/specified/text.rs +++ b/components/style/values/specified/text.rs @@ -517,6 +517,35 @@ impl ToCss for TextTransformOther { } } +/// Specified and computed value of text-align-last. +#[derive( + Clone, + Copy, + Debug, + Eq, + FromPrimitive, + Hash, + MallocSizeOf, + Parse, + PartialEq, + SpecifiedValueInfo, + ToComputedValue, + ToCss, + ToResolvedValue, + ToShmem, +)] +#[allow(missing_docs)] +#[repr(u8)] +pub enum TextAlignLast { + Auto, + Start, + End, + Left, + Right, + Center, + Justify, +} + /// Specified value of text-align keyword value. #[derive( Clone, @@ -535,14 +564,18 @@ impl ToCss for TextTransformOther { ToShmem, )] #[allow(missing_docs)] +#[repr(u8)] pub enum TextAlignKeyword { Start, - End, Left, Right, Center, #[cfg(any(feature = "gecko", feature = "servo-layout-2013"))] Justify, + #[css(skip)] + #[cfg(feature = "gecko")] + Char, + End, #[cfg(feature = "gecko")] MozCenter, #[cfg(feature = "gecko")] @@ -555,9 +588,6 @@ pub enum TextAlignKeyword { ServoLeft, #[cfg(feature = "servo-layout-2013")] ServoRight, - #[css(skip)] - #[cfg(feature = "gecko")] - Char, } /// Specified value of text-align property. @@ -579,14 +609,6 @@ pub enum TextAlign { MozCenterOrInherit, } -impl TextAlign { - /// Convert an enumerated value coming from Gecko to a `TextAlign`. - #[cfg(feature = "gecko")] - pub fn from_gecko_keyword(kw: u32) -> Self { - TextAlign::Keyword(TextAlignKeyword::from_gecko_keyword(kw)) - } -} - impl ToComputedValue for TextAlign { type ComputedValue = TextAlignKeyword;