Bug 1756130 [wpt PR 32898] - [CSP] Enhance unsafe-eval test to check both realms...
[gecko.git] / servo / components / style / macros.rs
blobe27a554acf4b7504691d2ada32329d300f891597
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
5 //! Various macro helpers.
7 macro_rules! exclusive_value {
8     (($value:ident, $set:expr) => $ident:path) => {
9         if $value.intersects($set) {
10             return Err(());
11         } else {
12             $ident
13         }
14     };
17 #[cfg(feature = "gecko")]
18 macro_rules! impl_gecko_keyword_conversions {
19     ($name:ident, $utype:ty) => {
20         impl From<$utype> for $name {
21             fn from(bits: $utype) -> $name {
22                 $name::from_gecko_keyword(bits)
23             }
24         }
26         impl From<$name> for $utype {
27             fn from(v: $name) -> $utype {
28                 v.to_gecko_keyword()
29             }
30         }
31     };
34 macro_rules! trivial_to_computed_value {
35     ($name:ty) => {
36         impl $crate::values::computed::ToComputedValue for $name {
37             type ComputedValue = $name;
39             fn to_computed_value(&self, _: &$crate::values::computed::Context) -> Self {
40                 self.clone()
41             }
43             fn from_computed_value(other: &Self) -> Self {
44                 other.clone()
45             }
46         }
47     };
50 /// A macro to parse an identifier, or return an `UnexpectedIdent` error
51 /// otherwise.
52 ///
53 /// FIXME(emilio): The fact that `UnexpectedIdent` is a `SelectorParseError`
54 /// doesn't make a lot of sense to me.
55 macro_rules! try_match_ident_ignore_ascii_case {
56     ($input:expr, $( $match_body:tt )*) => {{
57         let location = $input.current_source_location();
58         let ident = $input.expect_ident_cloned()?;
59         match_ignore_ascii_case! { &ident,
60             $( $match_body )*
61             _ => return Err(location.new_custom_error(
62                 ::selectors::parser::SelectorParseErrorKind::UnexpectedIdent(ident.clone())
63             ))
64         }
65     }}
68 macro_rules! define_keyword_type {
69     ($name:ident, $css:expr) => {
70         #[allow(missing_docs)]
71         #[derive(
72             Animate,
73             Clone,
74             ComputeSquaredDistance,
75             Copy,
76             MallocSizeOf,
77             PartialEq,
78             SpecifiedValueInfo,
79             ToAnimatedValue,
80             ToAnimatedZero,
81             ToComputedValue,
82             ToCss,
83             ToResolvedValue,
84             ToShmem,
85         )]
86         pub struct $name;
88         impl fmt::Debug for $name {
89             fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
90                 f.write_str($css)
91             }
92         }
94         impl $crate::parser::Parse for $name {
95             fn parse<'i, 't>(
96                 _context: &$crate::parser::ParserContext,
97                 input: &mut ::cssparser::Parser<'i, 't>,
98             ) -> Result<$name, ::style_traits::ParseError<'i>> {
99                 input
100                     .expect_ident_matching($css)
101                     .map(|_| $name)
102                     .map_err(|e| e.into())
103             }
104         }
105     };
108 #[cfg(feature = "servo")]
109 macro_rules! local_name {
110     ($s:tt) => {
111         $crate::values::GenericAtomIdent(html5ever::local_name!($s))
112     };
115 #[cfg(feature = "servo")]
116 macro_rules! ns {
117     () => {
118         $crate::values::GenericAtomIdent(html5ever::ns!())
119     };
120     ($s:tt) => {
121         $crate::values::GenericAtomIdent(html5ever::ns!($s))
122     };
125 #[cfg(feature = "gecko")]
126 macro_rules! local_name {
127     ($s:tt) => {
128         $crate::values::AtomIdent(atom!($s))
129     };