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    )
37}
38
39/// Like [`parser_context_for_document`], but with a custom error reporter.
40pub(crate) fn parser_context_for_document_with_reporter<'a>(
41    document: &'a Document,
42    rule_type: CssRuleType,
43    parsing_mode: ParsingMode,
44    url_data: &'a UrlExtraData,
45    error_reporter: &'a dyn ParseErrorReporter,
46) -> ParserContext<'a> {
47    let quirks_mode = document.quirks_mode();
48
49    ParserContext::new(
50        Origin::Author,
51        url_data,
52        Some(rule_type),
53        parsing_mode,
54        quirks_mode,
55        /* namespaces = */ Default::default(),
56        Some(error_reporter),
57        None,
58    )
59}
60
61/// Creates a `ParserContext` without a document, using no quirks mode
62/// and no error reporter.
63pub(crate) fn parser_context_for_anonymous_content<'a>(
64    rule_type: CssRuleType,
65    parsing_mode: ParsingMode,
66    url_data: &'a UrlExtraData,
67) -> ParserContext<'a> {
68    ParserContext::new(
69        Origin::Author,
70        url_data,
71        Some(rule_type),
72        parsing_mode,
73        QuirksMode::NoQuirks,
74        /* namespaces = */ Default::default(),
75        None,
76        None,
77    )
78}