1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
/* 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/. */
//! [CSS cascade origins](https://drafts.csswg.org/css-cascade/#cascading-origins).
use std::marker::PhantomData;
use std::ops::BitOrAssign;
/// Each style rule has an origin, which determines where it enters the cascade.
///
/// <https://drafts.csswg.org/css-cascade/#cascading-origins>
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq, ToShmem, PartialOrd, Ord)]
#[repr(u8)]
pub enum Origin {
/// <https://drafts.csswg.org/css-cascade/#cascade-origin-user-agent>
UserAgent = 0x1,
/// <https://drafts.csswg.org/css-cascade/#cascade-origin-user>
User = 0x2,
/// <https://drafts.csswg.org/css-cascade/#cascade-origin-author>
Author = 0x4,
}
impl Origin {
/// Returns an origin that goes in order for `index`.
///
/// This is used for iterating across origins.
fn from_index(index: i8) -> Option<Self> {
Some(match index {
0 => Origin::Author,
1 => Origin::User,
2 => Origin::UserAgent,
_ => return None,
})
}
fn to_index(self) -> i8 {
match self {
Origin::Author => 0,
Origin::User => 1,
Origin::UserAgent => 2,
}
}
/// Returns an iterator from this origin, towards all the less specific
/// origins. So for `UserAgent`, it'd iterate through all origins.
#[inline]
pub fn following_including(self) -> OriginSetIterator {
OriginSetIterator {
set: OriginSet::ORIGIN_USER | OriginSet::ORIGIN_AUTHOR | OriginSet::ORIGIN_USER_AGENT,
cur: self.to_index(),
rev: true,
}
}
}
/// A set of origins. This is equivalent to Gecko's OriginFlags.
#[derive(Clone, Copy, PartialEq, MallocSizeOf)]
pub struct OriginSet(u8);
bitflags! {
impl OriginSet: u8 {
/// <https://drafts.csswg.org/css-cascade/#cascade-origin-user-agent>
const ORIGIN_USER_AGENT = Origin::UserAgent as u8;
/// <https://drafts.csswg.org/css-cascade/#cascade-origin-user>
const ORIGIN_USER = Origin::User as u8;
/// <https://drafts.csswg.org/css-cascade/#cascade-origin-author>
const ORIGIN_AUTHOR = Origin::Author as u8;
}
}
impl OriginSet {
/// Returns an iterator over the origins present in this `OriginSet`.
///
/// See the `OriginSet` documentation for information about the order
/// origins are iterated.
pub fn iter_origins(&self) -> OriginSetIterator {
OriginSetIterator {
set: *self,
cur: 0,
rev: false,
}
}
}
impl From<Origin> for OriginSet {
fn from(origin: Origin) -> Self {
Self::from_bits_retain(origin as u8)
}
}
impl BitOrAssign<Origin> for OriginSet {
fn bitor_assign(&mut self, origin: Origin) {
*self |= OriginSet::from(origin);
}
}
/// Iterates over the origins present in an `OriginSet`, in order from
/// highest priority (author) to lower (user agent).
#[derive(Clone)]
pub struct OriginSetIterator {
set: OriginSet,
cur: i8,
rev: bool,
}
impl Iterator for OriginSetIterator {
type Item = Origin;
fn next(&mut self) -> Option<Origin> {
loop {
let origin = Origin::from_index(self.cur)?;
if self.rev {
self.cur -= 1;
} else {
self.cur += 1;
}
if self.set.contains(origin.into()) {
return Some(origin);
}
}
}
}
/// An object that stores a `T` for each origin of the CSS cascade.
#[derive(Debug, Default, MallocSizeOf)]
pub struct PerOrigin<T> {
/// Data for `Origin::UserAgent`.
pub user_agent: T,
/// Data for `Origin::User`.
pub user: T,
/// Data for `Origin::Author`.
pub author: T,
}
impl<T> PerOrigin<T> {
/// Returns a reference to the per-origin data for the specified origin.
#[inline]
pub fn borrow_for_origin(&self, origin: &Origin) -> &T {
match *origin {
Origin::UserAgent => &self.user_agent,
Origin::User => &self.user,
Origin::Author => &self.author,
}
}
/// Returns a mutable reference to the per-origin data for the specified
/// origin.
#[inline]
pub fn borrow_mut_for_origin(&mut self, origin: &Origin) -> &mut T {
match *origin {
Origin::UserAgent => &mut self.user_agent,
Origin::User => &mut self.user,
Origin::Author => &mut self.author,
}
}
/// Iterates over references to per-origin extra style data, from highest
/// level (author) to lowest (user agent).
pub fn iter_origins(&self) -> PerOriginIter<T> {
PerOriginIter {
data: &self,
cur: 0,
rev: false,
}
}
/// Iterates over references to per-origin extra style data, from lowest
/// level (user agent) to highest (author).
pub fn iter_origins_rev(&self) -> PerOriginIter<T> {
PerOriginIter {
data: &self,
cur: 2,
rev: true,
}
}
/// Iterates over mutable references to per-origin extra style data, from
/// highest level (author) to lowest (user agent).
pub fn iter_mut_origins(&mut self) -> PerOriginIterMut<T> {
PerOriginIterMut {
data: self,
cur: 0,
_marker: PhantomData,
}
}
}
/// Iterator over `PerOrigin<T>`, from highest level (author) to lowest
/// (user agent).
///
/// We rely on this specific order for correctly looking up @font-face,
/// @counter-style and @keyframes rules.
pub struct PerOriginIter<'a, T: 'a> {
data: &'a PerOrigin<T>,
cur: i8,
rev: bool,
}
impl<'a, T> Iterator for PerOriginIter<'a, T>
where
T: 'a,
{
type Item = (&'a T, Origin);
fn next(&mut self) -> Option<Self::Item> {
let origin = Origin::from_index(self.cur)?;
self.cur += if self.rev { -1 } else { 1 };
Some((self.data.borrow_for_origin(&origin), origin))
}
}
/// Like `PerOriginIter<T>`, but iterates over mutable references to the
/// per-origin data.
///
/// We must use unsafe code here since it's not possible for the borrow
/// checker to know that we are safely returning a different reference
/// each time from `next()`.
pub struct PerOriginIterMut<'a, T: 'a> {
data: *mut PerOrigin<T>,
cur: i8,
_marker: PhantomData<&'a mut PerOrigin<T>>,
}
impl<'a, T> Iterator for PerOriginIterMut<'a, T>
where
T: 'a,
{
type Item = (&'a mut T, Origin);
fn next(&mut self) -> Option<Self::Item> {
let origin = Origin::from_index(self.cur)?;
self.cur += 1;
Some((
unsafe { (*self.data).borrow_mut_for_origin(&origin) },
origin,
))
}
}