style/stylesheets/
stylesheet.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
5use crate::context::QuirksMode;
6use crate::derives::*;
7use crate::device::Device;
8use crate::error_reporting::{ContextualParseError, ParseErrorReporter};
9use crate::media_queries::MediaList;
10use crate::parser::ParserContext;
11use crate::shared_lock::{DeepCloneWithLock, Locked};
12use crate::shared_lock::{SharedRwLock, SharedRwLockReadGuard};
13use crate::stylesheets::loader::StylesheetLoader;
14use crate::stylesheets::rule_parser::{State, TopLevelRuleParser};
15use crate::stylesheets::rules_iterator::{EffectiveRules, EffectiveRulesIterator};
16use crate::stylesheets::rules_iterator::{NestedRuleIterationCondition, RulesIterator};
17use crate::stylesheets::{
18    CssRule, CssRules, CustomMediaEvaluator, CustomMediaMap, Origin, UrlExtraData,
19};
20use crate::use_counters::UseCounters;
21use crate::{Namespace, Prefix};
22use cssparser::{Parser, ParserInput, StyleSheetParser};
23#[cfg(feature = "gecko")]
24use malloc_size_of::{MallocSizeOfOps, MallocUnconditionalShallowSizeOf};
25use rustc_hash::FxHashMap;
26use servo_arc::Arc;
27use std::ops::Deref;
28use std::sync::atomic::{AtomicBool, Ordering};
29use style_traits::ParsingMode;
30
31use super::scope_rule::ImplicitScopeRoot;
32
33/// A set of namespaces applying to a given stylesheet.
34///
35/// The namespace id is used in gecko
36#[derive(Clone, Debug, Default, MallocSizeOf)]
37#[allow(missing_docs)]
38pub struct Namespaces {
39    pub default: Option<Namespace>,
40    pub prefixes: FxHashMap<Prefix, Namespace>,
41}
42
43/// The contents of a given stylesheet. This effectively maps to a
44/// StyleSheetInner in Gecko.
45#[derive(Debug)]
46pub struct StylesheetContents {
47    /// List of rules in the order they were found (important for
48    /// cascading order)
49    pub rules: Arc<Locked<CssRules>>,
50    /// The origin of this stylesheet.
51    pub origin: Origin,
52    /// The url data this stylesheet should use.
53    pub url_data: UrlExtraData,
54    /// The namespaces that apply to this stylesheet.
55    pub namespaces: Namespaces,
56    /// The quirks mode of this stylesheet.
57    pub quirks_mode: QuirksMode,
58    /// This stylesheet's source map URL.
59    pub source_map_url: Option<String>,
60    /// This stylesheet's source URL.
61    pub source_url: Option<String>,
62    /// The use counters of the original stylesheet.
63    pub use_counters: UseCounters,
64
65    /// We don't want to allow construction outside of this file, to guarantee
66    /// that all contents are created with Arc<>.
67    _forbid_construction: (),
68}
69
70impl StylesheetContents {
71    /// Parse a given CSS string, with a given url-data, origin, and
72    /// quirks mode.
73    pub fn from_str(
74        css: &str,
75        url_data: UrlExtraData,
76        origin: Origin,
77        shared_lock: &SharedRwLock,
78        stylesheet_loader: Option<&dyn StylesheetLoader>,
79        error_reporter: Option<&dyn ParseErrorReporter>,
80        quirks_mode: QuirksMode,
81        allow_import_rules: AllowImportRules,
82        sanitization_data: Option<&mut SanitizationData>,
83    ) -> Arc<Self> {
84        let use_counters = UseCounters::default();
85        let (namespaces, rules, source_map_url, source_url) = Stylesheet::parse_rules(
86            css,
87            &url_data,
88            origin,
89            &shared_lock,
90            stylesheet_loader,
91            error_reporter,
92            quirks_mode,
93            Some(&use_counters),
94            allow_import_rules,
95            sanitization_data,
96        );
97
98        Arc::new(Self {
99            rules: CssRules::new(rules, &shared_lock),
100            origin,
101            url_data,
102            namespaces,
103            quirks_mode,
104            source_map_url,
105            source_url,
106            use_counters,
107            _forbid_construction: (),
108        })
109    }
110
111    /// Creates a new StylesheetContents with the specified pre-parsed rules,
112    /// origin, URL data, and quirks mode.
113    ///
114    /// Since the rules have already been parsed, and the intention is that
115    /// this function is used for read only User Agent style sheets, an empty
116    /// namespace map is used, and the source map and source URLs are set to
117    /// None.
118    ///
119    /// An empty namespace map should be fine, as it is only used for parsing,
120    /// not serialization of existing selectors.  Since UA sheets are read only,
121    /// we should never need the namespace map.
122    pub fn from_shared_data(
123        rules: Arc<Locked<CssRules>>,
124        origin: Origin,
125        url_data: UrlExtraData,
126        quirks_mode: QuirksMode,
127    ) -> Arc<Self> {
128        debug_assert!(rules.is_static());
129        Arc::new(Self {
130            rules,
131            origin,
132            url_data,
133            namespaces: Namespaces::default(),
134            quirks_mode,
135            source_map_url: None,
136            source_url: None,
137            use_counters: UseCounters::default(),
138            _forbid_construction: (),
139        })
140    }
141
142    /// Returns a reference to the list of rules.
143    #[inline]
144    pub fn rules<'a, 'b: 'a>(&'a self, guard: &'b SharedRwLockReadGuard) -> &'a [CssRule] {
145        &self.rules.read_with(guard).0
146    }
147
148    /// Measure heap usage.
149    #[cfg(feature = "gecko")]
150    pub fn size_of(&self, guard: &SharedRwLockReadGuard, ops: &mut MallocSizeOfOps) -> usize {
151        if self.rules.is_static() {
152            return 0;
153        }
154        // Measurement of other fields may be added later.
155        self.rules.unconditional_shallow_size_of(ops)
156            + self.rules.read_with(guard).size_of(guard, ops)
157    }
158
159    /// Return an iterator using the condition `C`.
160    #[inline]
161    pub fn iter_rules<'a, 'b, C, CMM>(
162        &'a self,
163        device: &'a Device,
164        custom_media: CMM,
165        guard: &'a SharedRwLockReadGuard<'b>,
166    ) -> RulesIterator<'a, 'b, C, CMM>
167    where
168        C: NestedRuleIterationCondition,
169        CMM: Deref<Target = CustomMediaMap>,
170    {
171        RulesIterator::new(
172            device,
173            self.quirks_mode,
174            custom_media,
175            guard,
176            self.rules(guard).iter(),
177        )
178    }
179
180    /// Return an iterator over the effective rules within the style-sheet, as
181    /// according to the supplied `Device`.
182    #[inline]
183    pub fn effective_rules<'a, 'b, CMM: Deref<Target = CustomMediaMap>>(
184        &'a self,
185        device: &'a Device,
186        custom_media: CMM,
187        guard: &'a SharedRwLockReadGuard<'b>,
188    ) -> EffectiveRulesIterator<'a, 'b, CMM> {
189        self.iter_rules::<EffectiveRules, CMM>(device, custom_media, guard)
190    }
191
192    /// Perform a deep clone, of this stylesheet, with an explicit URL data if needed.
193    pub fn deep_clone(
194        &self,
195        lock: &SharedRwLock,
196        url_data: Option<&UrlExtraData>,
197        guard: &SharedRwLockReadGuard,
198    ) -> Arc<Self> {
199        // Make a deep clone of the rules, using the new lock.
200        let rules = self
201            .rules
202            .read_with(guard)
203            .deep_clone_with_lock(lock, guard);
204
205        let url_data = url_data.cloned().unwrap_or_else(|| self.url_data.clone());
206
207        Arc::new(Self {
208            rules: Arc::new(lock.wrap(rules)),
209            quirks_mode: self.quirks_mode,
210            origin: self.origin,
211            url_data,
212            namespaces: self.namespaces.clone(),
213            source_map_url: self.source_map_url.clone(),
214            source_url: self.source_url.clone(),
215            use_counters: self.use_counters.clone(),
216            _forbid_construction: (),
217        })
218    }
219}
220
221/// The structure servo uses to represent a stylesheet.
222#[derive(Debug)]
223pub struct Stylesheet {
224    /// The contents of this stylesheet.
225    pub contents: Locked<Arc<StylesheetContents>>,
226    /// The lock used for objects inside this stylesheet
227    pub shared_lock: SharedRwLock,
228    /// List of media associated with the Stylesheet.
229    pub media: Arc<Locked<MediaList>>,
230    /// Whether this stylesheet should be disabled.
231    pub disabled: AtomicBool,
232}
233
234/// A trait to represent a given stylesheet in a document.
235pub trait StylesheetInDocument: ::std::fmt::Debug {
236    /// Get whether this stylesheet is enabled.
237    fn enabled(&self) -> bool;
238
239    /// Get the media associated with this stylesheet.
240    fn media<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> Option<&'a MediaList>;
241
242    /// Returns a reference to the contents of the stylesheet.
243    fn contents<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> &'a StylesheetContents;
244
245    /// Returns whether the style-sheet applies for the current device.
246    fn is_effective_for_device(
247        &self,
248        device: &Device,
249        custom_media: &CustomMediaMap,
250        guard: &SharedRwLockReadGuard,
251    ) -> bool {
252        let media = match self.media(guard) {
253            Some(m) => m,
254            None => return true,
255        };
256        media.evaluate(
257            device,
258            self.contents(guard).quirks_mode,
259            &mut CustomMediaEvaluator::new(custom_media, guard),
260        )
261    }
262
263    /// Return the implicit scope root for this stylesheet, if one exists.
264    fn implicit_scope_root(&self) -> Option<ImplicitScopeRoot>;
265}
266
267impl StylesheetInDocument for Stylesheet {
268    fn media<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> Option<&'a MediaList> {
269        Some(self.media.read_with(guard))
270    }
271
272    fn enabled(&self) -> bool {
273        !self.disabled()
274    }
275
276    #[inline]
277    fn contents<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> &'a StylesheetContents {
278        self.contents.read_with(guard)
279    }
280
281    fn implicit_scope_root(&self) -> Option<ImplicitScopeRoot> {
282        None
283    }
284}
285
286/// A simple wrapper over an `Arc<Stylesheet>`, with pointer comparison, and
287/// suitable for its use in a `StylesheetSet`.
288#[derive(Clone, Debug)]
289#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
290pub struct DocumentStyleSheet(
291    #[cfg_attr(feature = "servo", ignore_malloc_size_of = "Arc")] pub Arc<Stylesheet>,
292);
293
294impl PartialEq for DocumentStyleSheet {
295    fn eq(&self, other: &Self) -> bool {
296        Arc::ptr_eq(&self.0, &other.0)
297    }
298}
299
300impl StylesheetInDocument for DocumentStyleSheet {
301    fn media<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> Option<&'a MediaList> {
302        self.0.media(guard)
303    }
304
305    fn enabled(&self) -> bool {
306        self.0.enabled()
307    }
308
309    #[inline]
310    fn contents<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> &'a StylesheetContents {
311        self.0.contents(guard)
312    }
313
314    fn implicit_scope_root(&self) -> Option<ImplicitScopeRoot> {
315        None
316    }
317}
318
319/// The kind of sanitization to use when parsing a stylesheet.
320#[repr(u8)]
321#[derive(Clone, Copy, Debug, PartialEq)]
322pub enum SanitizationKind {
323    /// Perform no sanitization.
324    None,
325    /// Allow only @font-face, style rules, and @namespace.
326    Standard,
327    /// Allow everything but conditional rules.
328    NoConditionalRules,
329}
330
331/// Whether @import rules are allowed.
332#[repr(u8)]
333#[derive(Clone, Copy, Debug, PartialEq)]
334pub enum AllowImportRules {
335    /// @import rules will be parsed.
336    Yes,
337    /// @import rules will not be parsed.
338    No,
339}
340
341impl SanitizationKind {
342    fn allows(self, rule: &CssRule) -> bool {
343        debug_assert_ne!(self, SanitizationKind::None);
344        // NOTE(emilio): If this becomes more complex (not filtering just by
345        // top-level rules), we should thread all the data through nested rules
346        // and such. But this doesn't seem necessary at the moment.
347        let is_standard = matches!(self, SanitizationKind::Standard);
348        match *rule {
349            CssRule::Document(..) |
350            CssRule::Media(..) |
351            CssRule::CustomMedia(..) |
352            CssRule::Supports(..) |
353            CssRule::Import(..) |
354            CssRule::Container(..) |
355            // TODO(emilio): Perhaps Layer should not be always sanitized? But
356            // we sanitize @media and co, so this seems safer for now.
357            CssRule::LayerStatement(..) |
358            CssRule::LayerBlock(..) |
359            // TODO(dshin): Same comment as Layer applies - shouldn't give away
360            // something like display size - erring on the side of "safe" for now.
361            CssRule::Scope(..) |
362            CssRule::StartingStyle(..) => false,
363
364            CssRule::FontFace(..) |
365            CssRule::Namespace(..) |
366            CssRule::Style(..) |
367            CssRule::NestedDeclarations(..) |
368            CssRule::PositionTry(..) => true,
369
370            CssRule::Keyframes(..) |
371            CssRule::Page(..) |
372            CssRule::Margin(..) |
373            CssRule::Property(..) |
374            CssRule::FontFeatureValues(..) |
375            CssRule::FontPaletteValues(..) |
376            CssRule::CounterStyle(..) => !is_standard,
377        }
378    }
379}
380
381/// A struct to hold the data relevant to style sheet sanitization.
382#[derive(Debug)]
383pub struct SanitizationData {
384    kind: SanitizationKind,
385    output: String,
386}
387
388impl SanitizationData {
389    /// Create a new input for sanitization.
390    #[inline]
391    pub fn new(kind: SanitizationKind) -> Option<Self> {
392        if matches!(kind, SanitizationKind::None) {
393            return None;
394        }
395        Some(Self {
396            kind,
397            output: String::new(),
398        })
399    }
400
401    /// Take the sanitized output.
402    #[inline]
403    pub fn take(self) -> String {
404        self.output
405    }
406}
407
408impl Stylesheet {
409    fn parse_rules(
410        css: &str,
411        url_data: &UrlExtraData,
412        origin: Origin,
413        shared_lock: &SharedRwLock,
414        stylesheet_loader: Option<&dyn StylesheetLoader>,
415        error_reporter: Option<&dyn ParseErrorReporter>,
416        quirks_mode: QuirksMode,
417        use_counters: Option<&UseCounters>,
418        allow_import_rules: AllowImportRules,
419        mut sanitization_data: Option<&mut SanitizationData>,
420    ) -> (Namespaces, Vec<CssRule>, Option<String>, Option<String>) {
421        let mut input = ParserInput::new(css);
422        let mut input = Parser::new(&mut input);
423
424        let context = ParserContext::new(
425            origin,
426            url_data,
427            None,
428            ParsingMode::DEFAULT,
429            quirks_mode,
430            /* namespaces = */ Default::default(),
431            error_reporter,
432            use_counters,
433        );
434
435        let mut rule_parser = TopLevelRuleParser {
436            shared_lock,
437            loader: stylesheet_loader,
438            context,
439            state: State::Start,
440            dom_error: None,
441            insert_rule_context: None,
442            allow_import_rules,
443            declaration_parser_state: Default::default(),
444            first_declaration_block: Default::default(),
445            wants_first_declaration_block: false,
446            error_reporting_state: Default::default(),
447            rules: Vec::new(),
448        };
449
450        {
451            let mut iter = StyleSheetParser::new(&mut input, &mut rule_parser);
452            while let Some(result) = iter.next() {
453                match result {
454                    Ok(rule_start) => {
455                        // TODO(emilio, nesting): sanitize nested CSS rules, probably?
456                        if let Some(ref mut data) = sanitization_data {
457                            if let Some(ref rule) = iter.parser.rules.last() {
458                                if !data.kind.allows(rule) {
459                                    iter.parser.rules.pop();
460                                    continue;
461                                }
462                            }
463                            let end = iter.input.position().byte_index();
464                            data.output.push_str(&css[rule_start.byte_index()..end]);
465                        }
466                    },
467                    Err((error, slice)) => {
468                        let location = error.location;
469                        let error = ContextualParseError::InvalidRule(slice, error);
470                        iter.parser.context.log_css_error(location, error);
471                    },
472                }
473            }
474        }
475
476        let source_map_url = input.current_source_map_url().map(String::from);
477        let source_url = input.current_source_url().map(String::from);
478        (
479            rule_parser.context.namespaces.into_owned(),
480            rule_parser.rules,
481            source_map_url,
482            source_url,
483        )
484    }
485
486    /// Creates an empty stylesheet and parses it with a given base url, origin and media.
487    pub fn from_str(
488        css: &str,
489        url_data: UrlExtraData,
490        origin: Origin,
491        media: Arc<Locked<MediaList>>,
492        shared_lock: SharedRwLock,
493        stylesheet_loader: Option<&dyn StylesheetLoader>,
494        error_reporter: Option<&dyn ParseErrorReporter>,
495        quirks_mode: QuirksMode,
496        allow_import_rules: AllowImportRules,
497    ) -> Self {
498        // FIXME: Consider adding use counters to Servo?
499        let contents = StylesheetContents::from_str(
500            css,
501            url_data,
502            origin,
503            &shared_lock,
504            stylesheet_loader,
505            error_reporter,
506            quirks_mode,
507            allow_import_rules,
508            /* sanitized_output = */ None,
509        );
510
511        Stylesheet {
512            contents: shared_lock.wrap(contents),
513            shared_lock,
514            media,
515            disabled: AtomicBool::new(false),
516        }
517    }
518
519    /// Returns whether the stylesheet has been explicitly disabled through the
520    /// CSSOM.
521    pub fn disabled(&self) -> bool {
522        self.disabled.load(Ordering::SeqCst)
523    }
524
525    /// Records that the stylesheet has been explicitly disabled through the
526    /// CSSOM.
527    ///
528    /// Returns whether the the call resulted in a change in disabled state.
529    ///
530    /// Disabled stylesheets remain in the document, but their rules are not
531    /// added to the Stylist.
532    pub fn set_disabled(&self, disabled: bool) -> bool {
533        self.disabled.swap(disabled, Ordering::SeqCst) != disabled
534    }
535}
536
537#[cfg(feature = "servo")]
538impl Clone for Stylesheet {
539    fn clone(&self) -> Self {
540        // Create a new lock for our clone.
541        let lock = self.shared_lock.clone();
542        let guard = self.shared_lock.read();
543
544        // Make a deep clone of the media, using the new lock.
545        let media = self.media.read_with(&guard).clone();
546        let media = Arc::new(lock.wrap(media));
547        let contents = lock.wrap(
548            self.contents
549                .read_with(&guard)
550                .deep_clone(&lock, None, &guard),
551        );
552
553        Stylesheet {
554            contents,
555            media,
556            shared_lock: lock,
557            disabled: AtomicBool::new(self.disabled.load(Ordering::SeqCst)),
558        }
559    }
560}