style/
lib.rs

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/. */
4
5//! Calculate [specified][specified] and [computed values][computed] from a
6//! tree of DOM nodes and a set of stylesheets.
7//!
8//! [computed]: https://drafts.csswg.org/css-cascade/#computed
9//! [specified]: https://drafts.csswg.org/css-cascade/#specified
10//!
11//! In particular, this crate contains the definitions of supported properties,
12//! the code to parse them into specified values and calculate the computed
13//! values based on the specified values, as well as the code to serialize both
14//! specified and computed values.
15//!
16//! The main entry point is [`recalc_style_at`][recalc_style_at].
17//!
18//! [recalc_style_at]: traversal/fn.recalc_style_at.html
19//!
20//! A list of supported style properties can be found as [docs::supported_properties]
21//!
22//! Major dependencies are the [cssparser][cssparser] and [selectors][selectors]
23//! crates.
24//!
25//! [cssparser]: ../cssparser/index.html
26//! [selectors]: ../selectors/index.html
27
28#![deny(missing_docs)]
29
30pub(crate) use cssparser;
31
32#[macro_use]
33extern crate bitflags;
34#[macro_use]
35#[cfg(feature = "gecko")]
36extern crate gecko_profiler;
37#[cfg(feature = "gecko")]
38#[macro_use]
39pub mod gecko_string_cache;
40#[macro_use]
41extern crate log;
42#[macro_use]
43extern crate serde;
44pub use servo_arc;
45#[cfg(feature = "servo")]
46#[macro_use]
47extern crate stylo_atoms;
48#[macro_use]
49extern crate static_assertions;
50
51#[macro_use]
52mod macros;
53
54mod derives {
55    pub(crate) use derive_more::{Add, AddAssign, Deref, DerefMut, From};
56    pub(crate) use malloc_size_of_derive::MallocSizeOf;
57    pub(crate) use num_derive::FromPrimitive;
58    pub(crate) use style_derive::{
59        Animate, ComputeSquaredDistance, Parse, SpecifiedValueInfo, ToAnimatedValue,
60        ToAnimatedZero, ToComputedValue, ToCss, ToResolvedValue, ToTyped,
61    };
62    pub(crate) use to_shmem_derive::ToShmem;
63}
64
65pub mod applicable_declarations;
66pub mod author_styles;
67pub mod bezier;
68pub mod bloom;
69pub mod color;
70#[path = "properties/computed_value_flags.rs"]
71pub mod computed_value_flags;
72pub mod context;
73pub mod counter_style;
74pub mod custom_properties;
75pub mod custom_properties_map;
76pub mod data;
77pub mod device;
78pub mod dom;
79pub mod dom_apis;
80pub mod driver;
81pub mod error_reporting;
82pub mod font_face;
83pub mod font_metrics;
84#[cfg(feature = "gecko")]
85#[allow(unsafe_code)]
86pub mod gecko_bindings;
87pub mod global_style_data;
88pub mod invalidation;
89#[allow(missing_docs)] // TODO.
90pub mod logical_geometry;
91pub mod matching;
92pub mod media_queries;
93pub mod parallel;
94pub mod parser;
95pub mod piecewise_linear;
96pub mod properties_and_values;
97#[macro_use]
98pub mod queries;
99pub mod rule_cache;
100pub mod rule_collector;
101pub mod rule_tree;
102pub mod scoped_tls;
103pub mod selector_map;
104pub mod selector_parser;
105pub mod shared_lock;
106pub mod sharing;
107mod simple_buckets_map;
108pub mod str;
109pub mod style_adjuster;
110pub mod style_resolver;
111pub mod stylesheet_set;
112pub mod stylesheets;
113pub mod stylist;
114pub mod thread_state;
115pub mod traversal;
116pub mod traversal_flags;
117pub mod typed_om;
118pub mod use_counters;
119
120#[macro_use]
121#[allow(non_camel_case_types)]
122pub mod values;
123
124#[cfg(all(doc, feature = "servo"))]
125/// Documentation
126pub mod docs {
127    /// The CSS properties supported by the style system.
128    /// Generated from the `properties.mako.rs` template by `build.rs`
129    pub mod supported_properties {
130        #![doc = include_str!(concat!(env!("OUT_DIR"), "/css-properties.html"))]
131    }
132}
133
134#[cfg(feature = "gecko")]
135pub use crate::gecko_string_cache as string_cache;
136#[cfg(feature = "gecko")]
137pub use crate::gecko_string_cache::Atom;
138/// The namespace prefix type for Gecko, which is just an atom.
139#[cfg(feature = "gecko")]
140pub type Prefix = crate::values::AtomIdent;
141/// The local name of an element for Gecko, which is just an atom.
142#[cfg(feature = "gecko")]
143pub type LocalName = crate::values::AtomIdent;
144#[cfg(feature = "gecko")]
145pub use crate::gecko_string_cache::Namespace;
146
147#[cfg(feature = "servo")]
148pub use stylo_atoms::Atom;
149
150#[cfg(feature = "servo")]
151#[allow(missing_docs)]
152pub type LocalName = crate::values::GenericAtomIdent<web_atoms::LocalNameStaticSet>;
153#[cfg(feature = "servo")]
154#[allow(missing_docs)]
155pub type Namespace = crate::values::GenericAtomIdent<web_atoms::NamespaceStaticSet>;
156#[cfg(feature = "servo")]
157#[allow(missing_docs)]
158pub type Prefix = crate::values::GenericAtomIdent<web_atoms::PrefixStaticSet>;
159
160pub use style_traits::arc_slice::ArcSlice;
161pub use style_traits::owned_slice::OwnedSlice;
162pub use style_traits::owned_str::OwnedStr;
163
164use std::hash::{BuildHasher, Hash};
165
166#[cfg_attr(feature = "servo", macro_use)]
167pub mod properties;
168
169#[cfg(feature = "gecko")]
170#[allow(unsafe_code)]
171pub mod gecko;
172
173// uses a macro from properties
174#[cfg(feature = "servo")]
175#[allow(unsafe_code)]
176pub mod servo;
177#[cfg(feature = "servo")]
178pub use servo::{animation, attr};
179
180macro_rules! reexport_computed_values {
181    ( $( { $name: ident } )+ ) => {
182        /// Types for [computed values][computed].
183        ///
184        /// [computed]: https://drafts.csswg.org/css-cascade/#computed
185        pub mod computed_values {
186            $(
187                pub use crate::properties::longhands::$name::computed_value as $name;
188            )+
189            // Don't use a side-specific name needlessly:
190            pub use crate::properties::longhands::border_top_style::computed_value as border_style;
191        }
192    }
193}
194longhand_properties_idents!(reexport_computed_values);
195#[cfg(feature = "gecko")]
196use crate::gecko_string_cache::WeakAtom;
197#[cfg(feature = "servo")]
198use stylo_atoms::Atom as WeakAtom;
199
200/// Extension methods for selectors::attr::CaseSensitivity
201pub trait CaseSensitivityExt {
202    /// Return whether two atoms compare equal according to this case sensitivity.
203    fn eq_atom(self, a: &WeakAtom, b: &WeakAtom) -> bool;
204}
205
206impl CaseSensitivityExt for selectors::attr::CaseSensitivity {
207    #[inline]
208    fn eq_atom(self, a: &WeakAtom, b: &WeakAtom) -> bool {
209        match self {
210            selectors::attr::CaseSensitivity::CaseSensitive => a == b,
211            selectors::attr::CaseSensitivity::AsciiCaseInsensitive => a.eq_ignore_ascii_case(b),
212        }
213    }
214}
215
216/// A trait pretty much similar to num_traits::Zero, but without the need of
217/// implementing `Add`.
218pub trait Zero {
219    /// Returns the zero value.
220    fn zero() -> Self;
221
222    /// Returns whether this value is zero.
223    fn is_zero(&self) -> bool;
224}
225
226impl<T> Zero for T
227where
228    T: num_traits::Zero,
229{
230    fn zero() -> Self {
231        <Self as num_traits::Zero>::zero()
232    }
233
234    fn is_zero(&self) -> bool {
235        <Self as num_traits::Zero>::is_zero(self)
236    }
237}
238
239/// A trait implementing a function to tell if the number is zero without a percent
240pub trait ZeroNoPercent {
241    /// So, `0px` should return `true`, but `0%` or `1px` should return `false`
242    fn is_zero_no_percent(&self) -> bool;
243}
244
245/// A trait pretty much similar to num_traits::One, but without the need of
246/// implementing `Mul`.
247pub trait One {
248    /// Reutrns the one value.
249    fn one() -> Self;
250
251    /// Returns whether this value is one.
252    fn is_one(&self) -> bool;
253}
254
255impl<T> One for T
256where
257    T: num_traits::One + PartialEq,
258{
259    fn one() -> Self {
260        <Self as num_traits::One>::one()
261    }
262
263    fn is_one(&self) -> bool {
264        *self == One::one()
265    }
266}
267
268/// An allocation error.
269///
270/// TODO(emilio): Would be nice to have more information here, or for SmallVec
271/// to return the standard error type (and then we can just return that).
272///
273/// But given we use these mostly to bail out and ignore them, it's not a big
274/// deal.
275#[derive(Debug)]
276pub struct AllocErr;
277
278impl From<smallvec::CollectionAllocErr> for AllocErr {
279    #[inline]
280    fn from(_: smallvec::CollectionAllocErr) -> Self {
281        Self
282    }
283}
284
285impl From<std::collections::TryReserveError> for AllocErr {
286    #[inline]
287    fn from(_: std::collections::TryReserveError) -> Self {
288        Self
289    }
290}
291
292/// Shrink the capacity of the collection if needed.
293pub(crate) trait ShrinkIfNeeded {
294    fn shrink_if_needed(&mut self);
295}
296
297/// We shrink the capacity of a collection if we're wasting more than a 25% of
298/// its capacity, and if the collection is arbitrarily big enough
299/// (>= CAPACITY_THRESHOLD entries).
300#[inline]
301fn should_shrink(len: usize, capacity: usize) -> bool {
302    const CAPACITY_THRESHOLD: usize = 64;
303    capacity >= CAPACITY_THRESHOLD && len + capacity / 4 < capacity
304}
305
306impl<K, V, H> ShrinkIfNeeded for std::collections::HashMap<K, V, H>
307where
308    K: Eq + Hash,
309    H: BuildHasher,
310{
311    fn shrink_if_needed(&mut self) {
312        if should_shrink(self.len(), self.capacity()) {
313            self.shrink_to_fit();
314        }
315    }
316}
317
318impl<T, H> ShrinkIfNeeded for std::collections::HashSet<T, H>
319where
320    T: Eq + Hash,
321    H: BuildHasher,
322{
323    fn shrink_if_needed(&mut self) {
324        if should_shrink(self.len(), self.capacity()) {
325            self.shrink_to_fit();
326        }
327    }
328}
329
330// TODO(emilio): Measure and see if we're wasting a lot of memory on Vec /
331// SmallVec, and if so consider shrinking those as well.