Skip to content

Commit

Permalink
Pull rule_type into ParserContext
Browse files Browse the repository at this point in the history
Absorb `rule_type` into the `ParserContext` so that it's easier to pass down to
deeper levels of the parser.

MozReview-Commit-ID: DjBNytLxGKX
  • Loading branch information
jryans committed Apr 12, 2017
1 parent a093b0a commit 4574cd8
Show file tree
Hide file tree
Showing 35 changed files with 125 additions and 97 deletions.
5 changes: 3 additions & 2 deletions components/script/dom/css.rs
Expand Up @@ -10,6 +10,7 @@ use dom::bindings::str::DOMString;
use dom::window::Window;
use dom_struct::dom_struct;
use style::parser::ParserContext;
use style::stylesheets::CssRuleType;
use style::supports::{Declaration, parse_condition_or_declaration};

#[dom_struct]
Expand All @@ -29,7 +30,7 @@ impl CSS {
pub fn Supports(win: &Window, property: DOMString, value: DOMString) -> bool {
let decl = Declaration { prop: property.into(), val: value.into() };
let url = win.Document().url();
let context = ParserContext::new_for_cssom(&url, win.css_error_reporter());
let context = ParserContext::new_for_cssom(&url, win.css_error_reporter(), Some(CssRuleType::Supports));
decl.eval(&context)
}

Expand All @@ -39,7 +40,7 @@ impl CSS {
let cond = parse_condition_or_declaration(&mut input);
if let Ok(cond) = cond {
let url = win.Document().url();
let context = ParserContext::new_for_cssom(&url, win.css_error_reporter());
let context = ParserContext::new_for_cssom(&url, win.css_error_reporter(), Some(CssRuleType::Supports));
cond.eval(&context)
} else {
false
Expand Down
5 changes: 4 additions & 1 deletion components/script/dom/cssmediarule.rs
Expand Up @@ -17,7 +17,7 @@ use dom_struct::dom_struct;
use std::sync::Arc;
use style::media_queries::parse_media_query_list;
use style::shared_lock::{Locked, ToCssWithGuard};
use style::stylesheets::MediaRule;
use style::stylesheets::{CssRuleType, MediaRule};
use style_traits::ToCss;

#[dom_struct]
Expand Down Expand Up @@ -68,6 +68,9 @@ impl CSSMediaRule {
/// https://drafts.csswg.org/css-conditional-3/#the-cssmediarule-interface
pub fn set_condition_text(&self, text: DOMString) {
let mut input = Parser::new(&text);
let win = self.global().as_window();
let url = win.Document().url();
let context = ParserContext::new_for_cssom(&url, win.css_error_reporter(), Some(CssRuleType::Media));
let new_medialist = parse_media_query_list(&mut input);
let mut guard = self.cssconditionrule.shared_lock().write();

Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/csssupportsrule.rs
Expand Up @@ -16,7 +16,7 @@ use dom_struct::dom_struct;
use std::sync::Arc;
use style::parser::ParserContext;
use style::shared_lock::{Locked, ToCssWithGuard};
use style::stylesheets::SupportsRule;
use style::stylesheets::{CssRuleType, SupportsRule};
use style::supports::SupportsCondition;
use style_traits::ToCss;

Expand Down Expand Up @@ -61,7 +61,7 @@ impl CSSSupportsRule {
let global = self.global();
let win = global.as_window();
let url = win.Document().url();
let context = ParserContext::new_for_cssom(&url, win.css_error_reporter());
let context = ParserContext::new_for_cssom(&url, win.css_error_reporter(), Some(CssRuleType::Supports));
let enabled = cond.eval(&context);
let mut guard = self.cssconditionrule.shared_lock().write();
let rule = self.supportsrule.write_with(&mut guard);
Expand Down
10 changes: 6 additions & 4 deletions components/style/keyframes.rs
Expand Up @@ -128,7 +128,8 @@ impl Keyframe {
let error_reporter = MemoryHoleReporter;
let context = ParserContext::new(parent_stylesheet.origin,
&parent_stylesheet.url_data,
&error_reporter);
&error_reporter,
Some(CssRuleType::Keyframe));
let mut input = Parser::new(css);

let mut rule_parser = KeyframeListParser {
Expand Down Expand Up @@ -364,8 +365,9 @@ impl<'a> QualifiedRuleParser for KeyframeListParser<'a> {

fn parse_block(&mut self, prelude: Self::Prelude, input: &mut Parser)
-> Result<Self::QualifiedRule, ()> {
let context = ParserContext::new_with_rule_type(self.context, Some(CssRuleType::Keyframe));
let parser = KeyframeDeclarationParser {
context: self.context,
context: &context,
};
let mut iter = DeclarationListParser::new(input, parser);
let mut block = PropertyDeclarationBlock::new();
Expand All @@ -376,7 +378,7 @@ impl<'a> QualifiedRuleParser for KeyframeListParser<'a> {
let pos = range.start;
let message = format!("Unsupported keyframe property declaration: '{}'",
iter.input.slice(range));
log_css_error(iter.input, pos, &*message, self.context);
log_css_error(iter.input, pos, &*message, &context);
}
}
// `parse_important` is not called here, `!important` is not allowed in keyframe blocks.
Expand All @@ -403,7 +405,7 @@ impl<'a, 'b> DeclarationParser for KeyframeDeclarationParser<'a, 'b> {

fn parse_value(&mut self, name: &str, input: &mut Parser) -> Result<ParsedDeclaration, ()> {
let id = try!(PropertyId::parse(name.into()));
match ParsedDeclaration::parse(id, self.context, input, true, CssRuleType::Keyframe) {
match ParsedDeclaration::parse(id, self.context, input, true) {
Ok(parsed) => {
// In case there is still unparsed text in the declaration, we should roll back.
if !input.is_exhausted() {
Expand Down
28 changes: 24 additions & 4 deletions components/style/parser.rs
Expand Up @@ -9,7 +9,7 @@
use cssparser::{Parser, SourcePosition, UnicodeRange};
use error_reporting::ParseErrorReporter;
use style_traits::OneOrMoreCommaSeparated;
use stylesheets::{Origin, UrlExtraData};
use stylesheets::{CssRuleType, Origin, UrlExtraData};

/// The data that the parser needs from outside in order to parse a stylesheet.
pub struct ParserContext<'a> {
Expand All @@ -20,26 +20,46 @@ pub struct ParserContext<'a> {
pub url_data: &'a UrlExtraData,
/// An error reporter to report syntax errors.
pub error_reporter: &'a ParseErrorReporter,
/// The current rule type, if any.
pub rule_type: Option<CssRuleType>,
}

impl<'a> ParserContext<'a> {
/// Create a parser context.
pub fn new(stylesheet_origin: Origin,
url_data: &'a UrlExtraData,
error_reporter: &'a ParseErrorReporter)
error_reporter: &'a ParseErrorReporter,
rule_type: Option<CssRuleType>)
-> ParserContext<'a> {
ParserContext {
stylesheet_origin: stylesheet_origin,
url_data: url_data,
error_reporter: error_reporter,
rule_type: rule_type,
}
}

/// Create a parser context for on-the-fly parsing in CSSOM
pub fn new_for_cssom(url_data: &'a UrlExtraData,
error_reporter: &'a ParseErrorReporter)
error_reporter: &'a ParseErrorReporter,
rule_type: Option<CssRuleType>)
-> ParserContext<'a> {
Self::new(Origin::Author, url_data, error_reporter)
Self::new(Origin::Author, url_data, error_reporter, rule_type)
}

/// Create a parser context based on a previous context, but with a modified rule type.
pub fn new_with_rule_type(context: &'a ParserContext,
rule_type: Option<CssRuleType>)
-> ParserContext<'a> {
Self::new(context.stylesheet_origin,
context.url_data,
context.error_reporter,
rule_type)
}

/// Get the rule type, which assumes that one is available.
pub fn rule_type(&self) -> CssRuleType {
self.rule_type.expect("Rule type expected, but none was found.")
}
}

Expand Down
15 changes: 6 additions & 9 deletions components/style/properties/declaration_block.rs
Expand Up @@ -612,8 +612,8 @@ pub fn parse_style_attribute(input: &str,
url_data: &UrlExtraData,
error_reporter: &ParseErrorReporter)
-> PropertyDeclarationBlock {
let context = ParserContext::new(Origin::Author, url_data, error_reporter);
parse_property_declaration_list(&context, &mut Parser::new(input), CssRuleType::Style)
let context = ParserContext::new(Origin::Author, url_data, error_reporter, Some(CssRuleType::Style));
parse_property_declaration_list(&context, &mut Parser::new(input))
}

/// Parse a given property declaration. Can result in multiple
Expand All @@ -626,17 +626,16 @@ pub fn parse_one_declaration(id: PropertyId,
url_data: &UrlExtraData,
error_reporter: &ParseErrorReporter)
-> Result<ParsedDeclaration, ()> {
let context = ParserContext::new(Origin::Author, url_data, error_reporter);
let context = ParserContext::new(Origin::Author, url_data, error_reporter, Some(CssRuleType::Style));
Parser::new(input).parse_entirely(|parser| {
ParsedDeclaration::parse(id, &context, parser, false, CssRuleType::Style)
ParsedDeclaration::parse(id, &context, parser, false)
.map_err(|_| ())
})
}

/// A struct to parse property declarations.
struct PropertyDeclarationParser<'a, 'b: 'a> {
context: &'a ParserContext<'b>,
rule_type: CssRuleType,
}


Expand All @@ -654,7 +653,7 @@ impl<'a, 'b> DeclarationParser for PropertyDeclarationParser<'a, 'b> {
-> Result<(ParsedDeclaration, Importance), ()> {
let id = try!(PropertyId::parse(name.into()));
let parsed = input.parse_until_before(Delimiter::Bang, |input| {
ParsedDeclaration::parse(id, self.context, input, false, self.rule_type)
ParsedDeclaration::parse(id, self.context, input, false)
.map_err(|_| ())
})?;
let importance = match input.try(parse_important) {
Expand All @@ -673,13 +672,11 @@ impl<'a, 'b> DeclarationParser for PropertyDeclarationParser<'a, 'b> {
/// Parse a list of property declarations and return a property declaration
/// block.
pub fn parse_property_declaration_list(context: &ParserContext,
input: &mut Parser,
rule_type: CssRuleType)
input: &mut Parser)
-> PropertyDeclarationBlock {
let mut block = PropertyDeclarationBlock::new();
let parser = PropertyDeclarationParser {
context: context,
rule_type: rule_type,
};
let mut iter = DeclarationListParser::new(input, parser);
while let Some(declaration) = iter.next() {
Expand Down
5 changes: 3 additions & 2 deletions components/style/properties/properties.mako.rs
Expand Up @@ -330,7 +330,7 @@ impl PropertyDeclarationIdSet {
//
// FIXME(pcwalton): Cloning the error reporter is slow! But so are custom
// properties, so whatever...
let context = ParserContext::new(Origin::Author, url_data, error_reporter);
let context = ParserContext::new(Origin::Author, url_data, error_reporter, None);
Parser::new(&css).parse_entirely(|input| {
match from_shorthand {
None => {
Expand Down Expand Up @@ -977,8 +977,9 @@ impl ParsedDeclaration {
/// to Importance::Normal. Parsing Importance values is the job of PropertyDeclarationParser,
/// we only set them here so that we don't have to reallocate
pub fn parse(id: PropertyId, context: &ParserContext, input: &mut Parser,
in_keyframe_block: bool, rule_type: CssRuleType)
in_keyframe_block: bool)
-> Result<ParsedDeclaration, PropertyDeclarationParseError> {
let rule_type = context.rule_type();
debug_assert!(rule_type == CssRuleType::Keyframe ||
rule_type == CssRuleType::Page ||
rule_type == CssRuleType::Style,
Expand Down
11 changes: 7 additions & 4 deletions components/style/stylesheets.rs
Expand Up @@ -387,7 +387,8 @@ impl CssRule {
let mut namespaces = parent_stylesheet.namespaces.write();
let context = ParserContext::new(parent_stylesheet.origin,
&parent_stylesheet.url_data,
&error_reporter);
&error_reporter,
None);
let mut input = Parser::new(css);

// nested rules are in the body state
Expand Down Expand Up @@ -658,7 +659,7 @@ impl Stylesheet {
namespaces: namespaces,
shared_lock: shared_lock,
loader: stylesheet_loader,
context: ParserContext::new(origin, url_data, error_reporter),
context: ParserContext::new(origin, url_data, error_reporter, None),
state: Cell::new(State::Start),
};

Expand Down Expand Up @@ -1115,7 +1116,8 @@ impl<'a, 'b> AtRuleParser for NestedRuleParser<'a, 'b> {
}))))
}
AtRulePrelude::Page => {
let declarations = parse_property_declaration_list(self.context, input, CssRuleType::Page);
let context = ParserContext::new_with_rule_type(self.context, Some(CssRuleType::Page));
let declarations = parse_property_declaration_list(&context, input);
Ok(CssRule::Page(Arc::new(self.shared_lock.wrap(PageRule(
Arc::new(self.shared_lock.wrap(declarations))
)))))
Expand All @@ -1138,7 +1140,8 @@ impl<'a, 'b> QualifiedRuleParser for NestedRuleParser<'a, 'b> {

fn parse_block(&mut self, prelude: SelectorList<SelectorImpl>, input: &mut Parser)
-> Result<CssRule, ()> {
let declarations = parse_property_declaration_list(self.context, input, CssRuleType::Style);
let context = ParserContext::new_with_rule_type(self.context, Some(CssRuleType::Style));
let declarations = parse_property_declaration_list(&context, input);
Ok(CssRule::Style(Arc::new(self.shared_lock.wrap(StyleRule {
selectors: prelude,
block: Arc::new(self.shared_lock.wrap(declarations))
Expand Down
3 changes: 2 additions & 1 deletion components/style/supports.rs
Expand Up @@ -212,7 +212,8 @@ impl Declaration {
return false
};
let mut input = Parser::new(&self.val);
let res = ParsedDeclaration::parse(id, cx, &mut input, /* in_keyframe */ false, CssRuleType::Style);
let context = ParserContext::new_with_rule_type(cx, Some(CssRuleType::Style));
let res = ParsedDeclaration::parse(id, &context, &mut input, /* in_keyframe */ false);
let _ = input.try(parse_important);
res.is_ok() && input.is_exhausted()
}
Expand Down
8 changes: 4 additions & 4 deletions ports/geckolib/glue.rs
Expand Up @@ -950,9 +950,9 @@ pub extern "C" fn Servo_ParseProperty(property: *const nsACString, value: *const

let url_data = unsafe { RefPtr::from_ptr_ref(&data) };
let reporter = StdoutErrorReporter;
let context = ParserContext::new(Origin::Author, url_data, &reporter);
let context = ParserContext::new(Origin::Author, url_data, &reporter, Some(CssRuleType::Style));

match ParsedDeclaration::parse(id, &context, &mut Parser::new(value), false, CssRuleType::Style) {
match ParsedDeclaration::parse(id, &context, &mut Parser::new(value), false) {
Ok(parsed) => {
let global_style_data = &*GLOBAL_STYLE_DATA;
let mut block = PropertyDeclarationBlock::new();
Expand All @@ -972,7 +972,7 @@ pub extern "C" fn Servo_ParseEasing(easing: *const nsAString,

let url_data = unsafe { RefPtr::from_ptr_ref(&data) };
let reporter = StdoutErrorReporter;
let context = ParserContext::new(Origin::Author, url_data, &reporter);
let context = ParserContext::new(Origin::Author, url_data, &reporter, Some(CssRuleType::Style));
let easing = unsafe { (*easing).to_string() };
match transition_timing_function::single_value::parse(&context, &mut Parser::new(&easing)) {
Ok(parsed_easing) => {
Expand Down Expand Up @@ -1511,7 +1511,7 @@ pub extern "C" fn Servo_CSSSupports(cond: *const nsACString) -> bool {
if let Ok(cond) = cond {
let url_data = unsafe { dummy_url_data() };
let reporter = StdoutErrorReporter;
let context = ParserContext::new_for_cssom(url_data, &reporter);
let context = ParserContext::new_for_cssom(url_data, &reporter, Some(CssRuleType::Style));
cond.eval(&context)
} else {
false
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/style/parsing/animation.rs
Expand Up @@ -9,7 +9,7 @@ use servo_atoms::Atom;
use style::parser::{Parse, ParserContext};
use style::properties::longhands::animation_iteration_count::single_value::computed_value::T as AnimationIterationCount;
use style::properties::longhands::animation_name;
use style::stylesheets::Origin;
use style::stylesheets::{CssRuleType, Origin};
use style_traits::ToCss;

#[test]
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/style/parsing/background.rs
Expand Up @@ -10,13 +10,13 @@ use style::properties::longhands::{background_attachment, background_clip, backg
use style::properties::longhands::{background_origin, background_position_x, background_position_y, background_repeat};
use style::properties::longhands::background_size;
use style::properties::shorthands::background;
use style::stylesheets::Origin;
use style::stylesheets::{CssRuleType, Origin};

#[test]
fn background_shorthand_should_parse_all_available_properties_when_specified() {
let url = ServoUrl::parse("http://localhost").unwrap();
let reporter = CSSErrorReporterTest;
let context = ParserContext::new(Origin::Author, &url, &reporter);
let context = ParserContext::new(Origin::Author, &url, &reporter, Some(CssRuleType::Style));
let mut parser = Parser::new("url(\"http://servo/test.png\") top center / 200px 200px repeat-x fixed padding-box \
content-box red");
let result = background::parse_value(&context, &mut parser).unwrap();
Expand All @@ -36,7 +36,7 @@ fn background_shorthand_should_parse_all_available_properties_when_specified() {
fn background_shorthand_should_parse_when_some_fields_set() {
let url = ServoUrl::parse("http://localhost").unwrap();
let reporter = CSSErrorReporterTest;
let context = ParserContext::new(Origin::Author, &url, &reporter);
let context = ParserContext::new(Origin::Author, &url, &reporter, Some(CssRuleType::Style));
let mut parser = Parser::new("14px 40px repeat-y");
let result = background::parse_value(&context, &mut parser).unwrap();

Expand Down Expand Up @@ -67,7 +67,7 @@ fn background_shorthand_should_parse_when_some_fields_set() {
fn background_shorthand_should_parse_comma_separated_declarations() {
let url = ServoUrl::parse("http://localhost").unwrap();
let reporter = CSSErrorReporterTest;
let context = ParserContext::new(Origin::Author, &url, &reporter);
let context = ParserContext::new(Origin::Author, &url, &reporter, Some(CssRuleType::Style));
let mut parser = Parser::new("url(\"http://servo/test.png\") top left no-repeat, url(\"http://servo/test.png\") \
center / 100% 100% no-repeat, white");
let result = background::parse_value(&context, &mut parser).unwrap();
Expand All @@ -89,7 +89,7 @@ fn background_shorthand_should_parse_comma_separated_declarations() {
fn background_shorthand_should_parse_position_and_size_correctly() {
let url = ServoUrl::parse("http://localhost").unwrap();
let reporter = CSSErrorReporterTest;
let context = ParserContext::new(Origin::Author, &url, &reporter);
let context = ParserContext::new(Origin::Author, &url, &reporter, Some(CssRuleType::Style));
let mut parser = Parser::new("7px 4px");
let result = background::parse_value(&context, &mut parser).unwrap();

Expand All @@ -114,7 +114,7 @@ fn background_shorthand_should_parse_position_and_size_correctly() {
fn background_shorthand_should_parse_origin_and_clip_correctly() {
let url = ServoUrl::parse("http://localhost").unwrap();
let reporter = CSSErrorReporterTest;
let context = ParserContext::new(Origin::Author, &url, &reporter);
let context = ParserContext::new(Origin::Author, &url, &reporter, Some(CssRuleType::Style));
let mut parser = Parser::new("padding-box content-box");
let result = background::parse_value(&context, &mut parser).unwrap();

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/style/parsing/basic_shape.rs
Expand Up @@ -6,7 +6,7 @@ use cssparser::Parser;
use media_queries::CSSErrorReporterTest;
use parsing::parse;
use style::parser::{Parse, ParserContext};
use style::stylesheets::Origin;
use style::stylesheets::{CssRuleType, Origin};
use style::values::specified::basic_shape::*;
use style_traits::ToCss;

Expand Down

0 comments on commit 4574cd8

Please sign in to comment.