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
use crate::media_queries::MediaList;
use crate::shared_lock::{DeepCloneParams, DeepCloneWithLock};
use crate::shared_lock::{SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
use crate::str::CssStringWriter;
use crate::stylesheets::layer_rule::LayerName;
use crate::stylesheets::{CssRule, StylesheetInDocument};
use crate::values::CssUrl;
use cssparser::SourceLocation;
use std::fmt::{self, Write};
use style_traits::{CssWriter, ToCss};
use to_shmem::{self, SharedMemoryBuilder, ToShmem};
#[cfg(feature = "gecko")]
#[derive(Debug)]
pub enum ImportSheet {
Sheet(crate::gecko::data::GeckoStyleSheet),
Pending,
}
#[cfg(feature = "gecko")]
impl ImportSheet {
pub fn new(sheet: crate::gecko::data::GeckoStyleSheet) -> Self {
ImportSheet::Sheet(sheet)
}
pub fn new_pending() -> Self {
ImportSheet::Pending
}
pub fn as_sheet(&self) -> Option<&crate::gecko::data::GeckoStyleSheet> {
match *self {
ImportSheet::Sheet(ref s) => {
debug_assert!(!s.hack_is_null());
if s.hack_is_null() {
return None;
}
Some(s)
},
ImportSheet::Pending => None,
}
}
pub fn media<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> Option<&'a MediaList> {
self.as_sheet().and_then(|s| s.media(guard))
}
pub fn rules<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> &'a [CssRule] {
match self.as_sheet() {
Some(s) => s.rules(guard),
None => &[],
}
}
}
#[cfg(feature = "gecko")]
impl DeepCloneWithLock for ImportSheet {
fn deep_clone_with_lock(
&self,
_lock: &SharedRwLock,
_guard: &SharedRwLockReadGuard,
params: &DeepCloneParams,
) -> Self {
use crate::gecko::data::GeckoStyleSheet;
use crate::gecko_bindings::bindings;
match *self {
ImportSheet::Sheet(ref s) => {
let clone = unsafe {
bindings::Gecko_StyleSheet_Clone(s.raw() as *const _, params.reference_sheet)
};
ImportSheet::Sheet(unsafe { GeckoStyleSheet::from_addrefed(clone) })
},
ImportSheet::Pending => ImportSheet::Pending,
}
}
}
#[cfg(feature = "servo")]
#[derive(Debug)]
pub struct ImportSheet(pub ::servo_arc::Arc<crate::stylesheets::Stylesheet>);
#[cfg(feature = "servo")]
impl ImportSheet {
pub fn media<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> Option<&'a MediaList> {
self.0.media(guard)
}
pub fn rules<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> &'a [CssRule] {
self.0.rules(guard)
}
}
#[cfg(feature = "servo")]
impl DeepCloneWithLock for ImportSheet {
fn deep_clone_with_lock(
&self,
_lock: &SharedRwLock,
_guard: &SharedRwLockReadGuard,
_params: &DeepCloneParams,
) -> Self {
use servo_arc::Arc;
ImportSheet(Arc::new((&*self.0).clone()))
}
}
#[derive(Debug, Clone)]
pub struct ImportLayer {
pub name: Option<LayerName>,
}
impl ToCss for ImportLayer {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
match self.name {
None => dest.write_str("layer"),
Some(ref name) => {
dest.write_str("layer(")?;
name.to_css(dest)?;
dest.write_char(')')
},
}
}
}
#[derive(Debug)]
pub struct ImportRule {
pub url: CssUrl,
pub stylesheet: ImportSheet,
pub layer: Option<ImportLayer>,
pub source_location: SourceLocation,
}
impl ToShmem for ImportRule {
fn to_shmem(&self, _builder: &mut SharedMemoryBuilder) -> to_shmem::Result<Self> {
Err(String::from(
"ToShmem failed for ImportRule: cannot handle imported style sheets",
))
}
}
impl DeepCloneWithLock for ImportRule {
fn deep_clone_with_lock(
&self,
lock: &SharedRwLock,
guard: &SharedRwLockReadGuard,
params: &DeepCloneParams,
) -> Self {
ImportRule {
url: self.url.clone(),
stylesheet: self.stylesheet.deep_clone_with_lock(lock, guard, params),
layer: self.layer.clone(),
source_location: self.source_location.clone(),
}
}
}
impl ToCssWithGuard for ImportRule {
fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut CssStringWriter) -> fmt::Result {
dest.write_str("@import ")?;
self.url.to_css(&mut CssWriter::new(dest))?;
if let Some(media) = self.stylesheet.media(guard) {
if !media.is_empty() {
dest.write_char(' ')?;
media.to_css(&mut CssWriter::new(dest))?;
}
}
if let Some(ref layer) = self.layer {
dest.write_char(' ')?;
layer.to_css(&mut CssWriter::new(dest))?;
}
dest.write_char(';')
}
}