forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1846516 - Move PropertyDeclarationId to its own module r=firefox-…
…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
Showing
5 changed files
with
104 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} |