Skip to content

Commit

Permalink
Bug 1846516 - Move PropertyDeclarationId to its own module r=firefox-…
Browse files Browse the repository at this point in the history
…style-system-reviewers,emilio

Because PropertyDeclarationId and its implementation do not make use of
templating, we might as well move it out of mako.

This will be useful later when creating OwnedPropertyDeclarationId,
which can be added to the same module.

Differential Revision: https://phabricator.services.mozilla.com/D195972
  • Loading branch information
zrhoffman committed Dec 14, 2023
1 parent a7dd52a commit 8326c1a
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 84 deletions.
14 changes: 8 additions & 6 deletions servo/components/style/properties/cascade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ use crate::custom_properties::CustomPropertiesBuilder;
use crate::dom::TElement;
use crate::font_metrics::FontMetricsOrientation;
use crate::logical_geometry::WritingMode;
use crate::properties::declaration_block::{DeclarationImportanceIterator, Importance};
use crate::properties::generated::{
CSSWideKeyword, ComputedValues, LonghandId, LonghandIdSet, PrioritaryPropertyId,
PropertyDeclaration, PropertyDeclarationId, PropertyFlags,
ShorthandsWithPropertyReferencesCache, StyleBuilder, CASCADE_PROPERTY,
PRIORITARY_PROPERTY_COUNT,
use crate::properties::{
declaration_block::{DeclarationImportanceIterator, Importance},
generated::{
CSSWideKeyword, ComputedValues, LonghandId, LonghandIdSet, PrioritaryPropertyId,
PropertyDeclaration, PropertyFlags, ShorthandsWithPropertyReferencesCache, StyleBuilder,
CASCADE_PROPERTY, PRIORITARY_PROPERTY_COUNT,
},
property_declaration::PropertyDeclarationId,
};
use crate::rule_cache::{RuleCache, RuleCacheConditions};
use crate::rule_tree::{CascadeLevel, StrongRuleNode};
Expand Down
3 changes: 2 additions & 1 deletion servo/components/style/properties/declaration_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

use super::generated::{
shorthands, AllShorthand, ComputedValues, LogicalGroupSet, LonghandIdSet,
NonCustomPropertyIdSet, PropertyDeclaration, PropertyDeclarationId, PropertyId, ShorthandId,
NonCustomPropertyIdSet, PropertyDeclaration, PropertyId, ShorthandId,
SourcePropertyDeclaration, SourcePropertyDeclarationDrain, SubpropertiesVec,
};
use super::property_declaration::PropertyDeclarationId;
use crate::applicable_declarations::CascadePriority;
use crate::context::QuirksMode;
use crate::custom_properties::{self, ComputedCustomProperties, CustomPropertiesBuilder};
Expand Down
2 changes: 2 additions & 0 deletions servo/components/style/properties/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
pub mod cascade;
pub mod declaration_block;
pub mod property_declaration;

/// The CSS properties supported by the style system.
/// Generated from the properties.mako.rs template by build.rs
Expand All @@ -25,3 +26,4 @@ pub mod generated {
pub use self::cascade::*;
pub use self::declaration_block::*;
pub use self::generated::*;
pub use self::property_declaration::*;
78 changes: 1 addition & 77 deletions servo/components/style/properties/properties.mako.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use crate::rule_tree::StrongRuleNode;
use crate::str::{CssString, CssStringWriter};
use std::cell::Cell;
use super::declaration_block::AppendableValue;
use super::property_declaration::PropertyDeclarationId;

<%!
from collections import defaultdict
Expand Down Expand Up @@ -1876,83 +1877,6 @@ impl UnparsedValue {
}
}

/// An identifier for a given property declaration, which can be either a
/// longhand or a custom property.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
pub enum PropertyDeclarationId<'a> {
/// A longhand.
Longhand(LonghandId),
/// A custom property declaration.
Custom(&'a crate::custom_properties::Name),
}

impl<'a> ToCss for PropertyDeclarationId<'a> {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
match *self {
PropertyDeclarationId::Longhand(id) => dest.write_str(id.name()),
PropertyDeclarationId::Custom(ref name) => {
dest.write_str("--")?;
serialize_atom_name(name, dest)
}
}
}
}

impl<'a> PropertyDeclarationId<'a> {
/// Whether a given declaration id is either the same as `other`, or a
/// longhand of it.
pub fn is_or_is_longhand_of(&self, other: &PropertyId) -> bool {
match *self {
PropertyDeclarationId::Longhand(id) => {
match *other {
PropertyId::Longhand(other_id) |
PropertyId::LonghandAlias(other_id, _) => id == other_id,
PropertyId::Shorthand(shorthand) |
PropertyId::ShorthandAlias(shorthand, _) => self.is_longhand_of(shorthand),
PropertyId::Custom(_) => false,
}
}
PropertyDeclarationId::Custom(name) => {
matches!(*other, PropertyId::Custom(ref other_name) if name == other_name)
}
}
}

/// Whether a given declaration id is a longhand belonging to this
/// shorthand.
pub fn is_longhand_of(&self, shorthand: ShorthandId) -> bool {
match *self {
PropertyDeclarationId::Longhand(ref id) => id.shorthands().any(|s| s == shorthand),
_ => false,
}
}

/// Returns the name of the property without CSS escaping.
pub fn name(&self) -> Cow<'static, str> {
match *self {
PropertyDeclarationId::Longhand(id) => id.name().into(),
PropertyDeclarationId::Custom(name) => {
let mut s = String::new();
write!(&mut s, "--{}", name).unwrap();
s.into()
}
}
}

/// Returns longhand id if it is, None otherwise.
#[inline]
pub fn as_longhand(&self) -> Option<LonghandId> {
match *self {
PropertyDeclarationId::Longhand(id) => Some(id),
_ => None,
}
}
}

/// Servo's representation of a CSS property, that is, either a longhand, a
/// shorthand, or a custom property.
#[derive(Clone, Eq, PartialEq)]
Expand Down
91 changes: 91 additions & 0 deletions servo/components/style/properties/property_declaration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! Structs used for property declarations.
use super::{LonghandId, PropertyId, ShorthandId};
use crate::custom_properties::Name;
use crate::values::serialize_atom_name;
use std::{
borrow::Cow,
fmt::{self, Write},
};
use style_traits::{CssWriter, ToCss};

/// An identifier for a given property declaration, which can be either a
/// longhand or a custom property.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
pub enum PropertyDeclarationId<'a> {
/// A longhand.
Longhand(LonghandId),
/// A custom property declaration.
Custom(&'a Name),
}

impl<'a> ToCss for PropertyDeclarationId<'a> {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
match *self {
PropertyDeclarationId::Longhand(id) => dest.write_str(id.name()),
PropertyDeclarationId::Custom(ref name) => {
dest.write_str("--")?;
serialize_atom_name(name, dest)
},
}
}
}

impl<'a> PropertyDeclarationId<'a> {
/// Whether a given declaration id is either the same as `other`, or a
/// longhand of it.
pub fn is_or_is_longhand_of(&self, other: &PropertyId) -> bool {
match *self {
PropertyDeclarationId::Longhand(id) => match *other {
PropertyId::Longhand(other_id) | PropertyId::LonghandAlias(other_id, _) => {
id == other_id
},
PropertyId::Shorthand(shorthand) | PropertyId::ShorthandAlias(shorthand, _) => {
self.is_longhand_of(shorthand)
},
PropertyId::Custom(_) => false,
},
PropertyDeclarationId::Custom(name) => {
matches!(*other, PropertyId::Custom(ref other_name) if name == other_name)
},
}
}

/// Whether a given declaration id is a longhand belonging to this
/// shorthand.
pub fn is_longhand_of(&self, shorthand: ShorthandId) -> bool {
match *self {
PropertyDeclarationId::Longhand(ref id) => id.shorthands().any(|s| s == shorthand),
_ => false,
}
}

/// Returns the name of the property without CSS escaping.
pub fn name(&self) -> Cow<'static, str> {
match *self {
PropertyDeclarationId::Longhand(id) => id.name().into(),
PropertyDeclarationId::Custom(name) => {
let mut s = String::new();
write!(&mut s, "--{}", name).unwrap();
s.into()
},
}
}

/// Returns longhand id if it is, None otherwise.
#[inline]
pub fn as_longhand(&self) -> Option<LonghandId> {
match *self {
PropertyDeclarationId::Longhand(id) => Some(id),
_ => None,
}
}
}

0 comments on commit 8326c1a

Please sign in to comment.