Skip to main content

script/
css.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
5//! Helpers for CSS value parsing.
6
7use style::context::QuirksMode;
8use style::error_reporting::ParseErrorReporter;
9use style::parser::ParserContext;
10use style::stylesheets::{CssRuleType, Origin, UrlExtraData};
11use style_traits::ParsingMode;
12
13use crate::dom::document::Document;
14
15/// Creates a `ParserContext` from the given document.
16///
17/// Automatically configures quirks mode and error reporter from the document.
18pub(crate) fn parser_context_for_document<'a>(
19    document: &'a Document,
20    rule_type: CssRuleType,
21    parsing_mode: ParsingMode,
22    url_data: &'a UrlExtraData,
23) -> ParserContext<'a> {
24    let quirks_mode = document.quirks_mode();
25    let error_reporter = document.window().css_error_reporter();
26
27    ParserContext::new(
28        Origin::Author,
29        url_data,
30        Some(rule_type),
31        parsing_mode,
32        quirks_mode,
33        /* namespaces = */ Default::default(),
34        Some(error_reporter),
35        None,
36        /* attr_taint = */ Default::default(),
37    )
38}
39
40/// Like [`parser_context_for_document`], but with a custom error reporter.
41pub(crate) fn parser_context_for_document_with_reporter<'a>(
42    document: &'a Document,
43    rule_type: CssRuleType,
44    parsing_mode: ParsingMode,
45    url_data: &'a UrlExtraData,
46    error_reporter: &'a dyn ParseErrorReporter,
47) -> ParserContext<'a> {
48    let quirks_mode = document.quirks_mode();
49
50    ParserContext::new(
51        Origin::Author,
52        url_data,
53        Some(rule_type),
54        parsing_mode,
55        quirks_mode,
56        /* namespaces = */ Default::default(),
57        Some(error_reporter),
58        None,
59        /* attr_taint = */ Default::default(),
60    )
61}
62
63/// Creates a `ParserContext` without a document, using no quirks mode
64/// and no error reporter.
65pub(crate) fn parser_context_for_anonymous_content<'a>(
66    rule_type: CssRuleType,
67    parsing_mode: ParsingMode,
68    url_data: &'a UrlExtraData,
69) -> ParserContext<'a> {
70    ParserContext::new(
71        Origin::Author,
72        url_data,
73        Some(rule_type),
74        parsing_mode,
75        QuirksMode::NoQuirks,
76        /* namespaces = */ Default::default(),
77        None,
78        None,
79        /* attr_taint = */ Default::default(),
80    )
81}