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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* 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/. */

//! The [`@import`][import] at-rule.
//!
//! [import]: https://drafts.csswg.org/css-cascade-3/#at-import

use crate::media_queries::MediaList;
use crate::parser::{Parse, ParserContext};
use crate::shared_lock::{
    DeepCloneWithLock, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard,
};
use crate::str::CssStringWriter;
use crate::stylesheets::{
    layer_rule::LayerName, supports_rule::SupportsCondition, CssRule, CssRuleType,
    StylesheetInDocument,
};
use crate::values::CssUrl;
use cssparser::{Parser, SourceLocation};
use std::fmt::{self, Write};
use style_traits::{CssWriter, ToCss};
use to_shmem::{SharedMemoryBuilder, ToShmem};

/// A sheet that is held from an import rule.
#[cfg(feature = "gecko")]
#[derive(Debug)]
pub enum ImportSheet {
    /// A bonafide stylesheet.
    Sheet(crate::gecko::data::GeckoStyleSheet),

    /// An @import created while parsing off-main-thread, whose Gecko sheet has
    /// yet to be created and attached.
    Pending,

    /// An @import created with a false <supports-condition>, so will never be fetched.
    Refused,
}

#[cfg(feature = "gecko")]
impl ImportSheet {
    /// Creates a new ImportSheet from a GeckoStyleSheet.
    pub fn new(sheet: crate::gecko::data::GeckoStyleSheet) -> Self {
        ImportSheet::Sheet(sheet)
    }

    /// Creates a pending ImportSheet for a load that has not started yet.
    pub fn new_pending() -> Self {
        ImportSheet::Pending
    }

    /// Creates a refused ImportSheet for a load that will not happen.
    pub fn new_refused() -> Self {
        ImportSheet::Refused
    }

    /// Returns a reference to the GeckoStyleSheet in this ImportSheet, if it
    /// exists.
    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::Refused | ImportSheet::Pending => None,
        }
    }

    /// Returns the media list for this import rule.
    pub fn media<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> Option<&'a MediaList> {
        self.as_sheet().and_then(|s| s.media(guard))
    }

    /// Returns the rule list for this import rule.
    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,
    ) -> 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 _)
                };
                ImportSheet::Sheet(unsafe { GeckoStyleSheet::from_addrefed(clone) })
            },
            ImportSheet::Pending => ImportSheet::Pending,
            ImportSheet::Refused => ImportSheet::Refused,
        }
    }
}

/// A sheet that is held from an import rule.
#[cfg(feature = "servo")]
#[derive(Debug)]
pub enum ImportSheet {
    /// A bonafide stylesheet.
    Sheet(::servo_arc::Arc<crate::stylesheets::Stylesheet>),

    /// An @import created with a false <supports-condition>, so will never be fetched.
    Refused,
}

#[cfg(feature = "servo")]
impl ImportSheet {
    /// Creates a new ImportSheet from a stylesheet.
    pub fn new(sheet: ::servo_arc::Arc<crate::stylesheets::Stylesheet>) -> Self {
        ImportSheet::Sheet(sheet)
    }

    /// Creates a refused ImportSheet for a load that will not happen.
    pub fn new_refused() -> Self {
        ImportSheet::Refused
    }

    /// Returns a reference to the stylesheet in this ImportSheet, if it exists.
    pub fn as_sheet(&self) -> Option<&::servo_arc::Arc<crate::stylesheets::Stylesheet>> {
        match *self {
            ImportSheet::Sheet(ref s) => Some(s),
            ImportSheet::Refused => None,
        }
    }

    /// Returns the media list for this import rule.
    pub fn media<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> Option<&'a MediaList> {
        self.as_sheet().and_then(|s| s.media(guard))
    }

    /// Returns the rules for this import rule.
    pub fn rules<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> &'a [CssRule] {
        match self.as_sheet() {
            Some(s) => s.rules(guard),
            None => &[],
        }
    }
}

#[cfg(feature = "servo")]
impl DeepCloneWithLock for ImportSheet {
    fn deep_clone_with_lock(
        &self,
        _lock: &SharedRwLock,
        _guard: &SharedRwLockReadGuard,
    ) -> Self {
        match *self {
            ImportSheet::Sheet(ref s) => {
                use servo_arc::Arc;
                ImportSheet::Sheet(Arc::new((&**s).clone()))
            },
            ImportSheet::Refused => ImportSheet::Refused,
        }
    }
}

/// The layer specified in an import rule (can be none, anonymous, or named).
#[derive(Debug, Clone)]
pub enum ImportLayer {
    /// No layer specified
    None,

    /// Anonymous layer (`layer`)
    Anonymous,

    /// Named layer (`layer(name)`)
    Named(LayerName),
}

/// The supports condition in an import rule.
#[derive(Debug, Clone)]
pub struct ImportSupportsCondition {
    /// The supports condition.
    pub condition: SupportsCondition,

    /// If the import is enabled, from the result of the import condition.
    pub enabled: bool,
}

impl ToCss for ImportLayer {
    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
    where
        W: Write,
    {
        match *self {
            ImportLayer::None => Ok(()),
            ImportLayer::Anonymous => dest.write_str("layer"),
            ImportLayer::Named(ref name) => {
                dest.write_str("layer(")?;
                name.to_css(dest)?;
                dest.write_char(')')
            },
        }
    }
}

/// The [`@import`][import] at-rule.
///
/// [import]: https://drafts.csswg.org/css-cascade-3/#at-import
#[derive(Debug)]
pub struct ImportRule {
    /// The `<url>` this `@import` rule is loading.
    pub url: CssUrl,

    /// The stylesheet is always present. However, in the case of gecko async
    /// parsing, we don't actually have a Gecko sheet at first, and so the
    /// ImportSheet just has stub behavior until it appears.
    pub stylesheet: ImportSheet,

    /// A <supports-condition> for the rule.
    pub supports: Option<ImportSupportsCondition>,

    /// A `layer()` function name.
    pub layer: ImportLayer,

    /// The line and column of the rule's source code.
    pub source_location: SourceLocation,
}

impl ImportRule {
    /// Parses the layer() / layer / supports() part of the import header, as per
    /// https://drafts.csswg.org/css-cascade-5/#at-import:
    ///
    ///     [ layer | layer(<layer-name>) ]?
    ///     [ supports([ <supports-condition> | <declaration> ]) ]?
    ///
    /// We do this here so that the import preloader can look at this without having to parse the
    /// whole import rule or parse the media query list or what not.
    pub fn parse_layer_and_supports<'i, 't>(
        input: &mut Parser<'i, 't>,
        context: &mut ParserContext,
    ) -> (ImportLayer, Option<ImportSupportsCondition>) {
        let layer = if input
            .try_parse(|input| input.expect_ident_matching("layer"))
            .is_ok()
        {
            ImportLayer::Anonymous
        } else {
            input
                .try_parse(|input| {
                    input.expect_function_matching("layer")?;
                    input
                        .parse_nested_block(|input| LayerName::parse(context, input))
                        .map(|name| ImportLayer::Named(name))
                })
                .ok()
                .unwrap_or(ImportLayer::None)
        };

        let supports = if !static_prefs::pref!("layout.css.import-supports.enabled") {
            None
        } else {
            input
                .try_parse(SupportsCondition::parse_for_import)
                .map(|condition| {
                    let enabled = context
                        .nest_for_rule(CssRuleType::Style, |context| condition.eval(context));
                    ImportSupportsCondition { condition, enabled }
                })
                .ok()
        };

        (layer, supports)
    }
}

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,
    ) -> Self {
        ImportRule {
            url: self.url.clone(),
            stylesheet: self.stylesheet.deep_clone_with_lock(lock, guard),
            supports: self.supports.clone(),
            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 !matches!(self.layer, ImportLayer::None) {
            dest.write_char(' ')?;
            self.layer.to_css(&mut CssWriter::new(dest))?;
        }

        if let Some(ref supports) = self.supports {
            dest.write_str(" supports(")?;
            supports.condition.to_css(&mut CssWriter::new(dest))?;
            dest.write_char(')')?;
        }

        if let Some(media) = self.stylesheet.media(guard) {
            if !media.is_empty() {
                dest.write_char(' ')?;
                media.to_css(&mut CssWriter::new(dest))?;
            }
        }

        dest.write_char(';')
    }
}