aboutsummaryrefslogtreecommitdiffstats
path: root/components/style/parser.rs
blob: 5b29834df1a5b0ff85d2308effa71216e4274f7f (plain) (blame)
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
/* 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 http://mozilla.org/MPL/2.0/. */

//! The context within which CSS code is parsed.

use cssparser::{Parser, SourcePosition};
use error_reporting::ParseErrorReporter;
#[cfg(feature = "gecko")]
use gecko_bindings::sugar::refptr::{GeckoArcPrincipal, GeckoArcURI};
use selector_impl::TheSelectorImpl;
use selectors::parser::ParserContext as SelectorParserContext;
use servo_url::ServoUrl;
use stylesheets::Origin;

#[cfg(not(feature = "gecko"))]
pub struct ParserContextExtraData;

#[cfg(feature = "gecko")]
pub struct ParserContextExtraData {
    pub base: Option<GeckoArcURI>,
    pub referrer: Option<GeckoArcURI>,
    pub principal: Option<GeckoArcPrincipal>,
}

impl ParserContextExtraData {
    #[cfg(not(feature = "gecko"))]
    pub fn default() -> ParserContextExtraData {
        ParserContextExtraData
    }

    #[cfg(feature = "gecko")]
    pub fn default() -> ParserContextExtraData {
        ParserContextExtraData { base: None, referrer: None, principal: None }
    }
}

pub struct ParserContext<'a> {
    pub stylesheet_origin: Origin,
    pub base_url: &'a ServoUrl,
    pub selector_context: SelectorParserContext<TheSelectorImpl>,
    pub error_reporter: Box<ParseErrorReporter + Send>,
    pub extra_data: ParserContextExtraData,
}

impl<'a> ParserContext<'a> {
    pub fn new_with_extra_data(stylesheet_origin: Origin, base_url: &'a ServoUrl,
                               error_reporter: Box<ParseErrorReporter + Send>,
                               extra_data: ParserContextExtraData)
                               -> ParserContext<'a> {
        let mut selector_context = SelectorParserContext::new();
        selector_context.in_user_agent_stylesheet = stylesheet_origin == Origin::UserAgent;
        ParserContext {
            stylesheet_origin: stylesheet_origin,
            base_url: base_url,
            selector_context: selector_context,
            error_reporter: error_reporter,
            extra_data: extra_data,
        }
    }

    pub fn new(stylesheet_origin: Origin, base_url: &'a ServoUrl, error_reporter: Box<ParseErrorReporter + Send>)
               -> ParserContext<'a> {
        let extra_data = ParserContextExtraData::default();
        ParserContext::new_with_extra_data(stylesheet_origin, base_url, error_reporter, extra_data)
    }
}

/// Defaults to a no-op.
/// Set a `RUST_LOG=style::errors` environment variable
/// to log CSS parse errors to stderr.
pub fn log_css_error(input: &mut Parser, position: SourcePosition, message: &str, parsercontext: &ParserContext) {
    parsercontext.error_reporter.report_error(input, position, message);
}

// XXXManishearth Replace all specified value parse impls with impls of this
// trait. This will make it easy to write more generic values in the future.
// There may need to be two traits -- one for parsing with context, and one
// for parsing without
pub trait Parse {
    fn parse(input: &mut Parser) -> Result<Self, ()> where Self: Sized;
}