/
origin.rs
157 lines (136 loc) · 4.65 KB
/
origin.rs
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
/* 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 http://mozilla.org/MPL/2.0/. */
///! [CSS cascade origins](https://drafts.csswg.org/css-cascade/#cascading-origins).
use std::marker::PhantomData;
/// Each style rule has an origin, which determines where it enters the cascade.
///
/// https://drafts.csswg.org/css-cascade/#cascading-origins
#[derive(Clone, PartialEq, Eq, Copy, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum Origin {
/// https://drafts.csswg.org/css-cascade/#cascade-origin-us
UserAgent,
/// https://drafts.csswg.org/css-cascade/#cascade-origin-user
User,
/// https://drafts.csswg.org/css-cascade/#cascade-origin-author
Author,
}
/// An object that stores a `T` for each origin of the CSS cascade.
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Debug, Default)]
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,
}
}
}
/// An object that can be cleared.
pub trait PerOriginClear {
/// Clears the object.
fn clear(&mut self);
}
impl<T> PerOriginClear for PerOrigin<T> where T: PerOriginClear {
fn clear(&mut self) {
self.user_agent.clear();
self.user.clear();
self.author.clear();
}
}
/// 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 result = match self.cur {
0 => (&self.data.author, Origin::Author),
1 => (&self.data.user, Origin::User),
2 => (&self.data.user_agent, Origin::UserAgent),
_ => return None,
};
self.cur += if self.rev { -1 } else { 1 };
Some(result)
}
}
/// 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 result = match self.cur {
0 => (unsafe { &mut (*self.data).author }, Origin::Author),
1 => (unsafe { &mut (*self.data).user }, Origin::User),
2 => (unsafe { &mut (*self.data).user_agent }, Origin::UserAgent),
_ => return None,
};
self.cur += 1;
Some(result)
}
}