Defined new trait ParseErrorReporter and added error_reporter member to ParserContext

This commit is contained in:
GauriGNaik 2015-10-26 11:31:28 -04:00 committed by Josh Matthews
parent 8efc954531
commit 996e9e06b2
24 changed files with 194 additions and 69 deletions

View file

@ -6,6 +6,7 @@
use cssparser::{Parser, SourcePosition};
use log;
use selectors::parser::ParserContext as SelectorParserContext;
use style_traits::ParseErrorReporter;
use stylesheets::Origin;
use url::{Url, UrlParser};
@ -13,16 +14,19 @@ pub struct ParserContext<'a> {
pub stylesheet_origin: Origin,
pub base_url: &'a Url,
pub selector_context: SelectorParserContext,
pub error_reporter: Box<ParseErrorReporter + Send>
}
impl<'a> ParserContext<'a> {
pub fn new(stylesheet_origin: Origin, base_url: &'a Url) -> ParserContext<'a> {
pub fn new(stylesheet_origin: Origin, base_url: &'a Url, error_reporter: Box<ParseErrorReporter + Send>)
-> 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,
}
}
}

View file

@ -22,7 +22,7 @@ use euclid::SideOffsets2D;
use euclid::size::Size2D;
use fnv::FnvHasher;
use string_cache::Atom;
use style_traits::ParseErrorReporter;
use computed_values;
use parser::{ParserContext, log_css_error};
use selectors::matching::DeclarationBlock;
@ -135,6 +135,7 @@ pub mod longhands {
use properties::{ComputedValues, PropertyDeclaration};
use std::collections::HashMap;
use std::sync::Arc;
use style_traits::ParseErrorReporter;
use values::computed::ToComputedValue;
use values::{computed, specified};
use string_cache::Atom;
@ -145,7 +146,8 @@ pub mod longhands {
inherited_style: &ComputedValues,
context: &computed::Context,
seen: &mut PropertyBitField,
cacheable: &mut bool) {
cacheable: &mut bool,
error_reporter: Box<ParseErrorReporter + Send>) {
let declared_value = match *declaration {
PropertyDeclaration::${property.camel_case}(ref declared_value) => {
declared_value
@ -174,7 +176,7 @@ pub mod longhands {
.${property.ident}
.clone()
}
}
}, error_reporter.clone()
);
Arc::make_mut(&mut style.${THIS_STYLE_STRUCT.ident}).${property.ident} =
computed_value;
@ -186,7 +188,8 @@ pub mod longhands {
inherited_style,
context,
seen,
cacheable);
cacheable,
error_reporter);
% endif
% else:
// Do not allow stylesheets to set derived properties.
@ -533,7 +536,8 @@ pub mod longhands {
_inherited_style: &ComputedValues,
context: &computed::Context,
_seen: &mut PropertyBitField,
_cacheable: &mut bool) {
_cacheable: &mut bool,
_error_reporter: Box<ParseErrorReporter + Send>) {
Arc::make_mut(&mut style.box_)._servo_display_for_hypothetical_box =
longhands::_servo_display_for_hypothetical_box::derive_from_display(
*computed_value,
@ -2234,7 +2238,8 @@ pub mod longhands {
_inherited_style: &ComputedValues,
context: &computed::Context,
_seen: &mut PropertyBitField,
_cacheable: &mut bool) {
_cacheable: &mut bool,
_error_reporter: Box<ParseErrorReporter + Send>) {
Arc::make_mut(&mut style.inheritedtext)._servo_text_decorations_in_effect =
longhands::_servo_text_decorations_in_effect::derive_from_text_decoration(
*computed_value,
@ -5613,7 +5618,7 @@ mod property_bit_field {
fn substitute_variables_${property.ident}<F, R>(
value: &DeclaredValue<longhands::${property.ident}::SpecifiedValue>,
custom_properties: &Option<Arc<::custom_properties::ComputedValuesMap>>,
f: F)
f: F, error_reporter: Box<ParseErrorReporter + Send>)
-> R
where F: FnOnce(&DeclaredValue<longhands::${property.ident}::SpecifiedValue>) -> R
{
@ -5625,7 +5630,7 @@ mod property_bit_field {
.and_then(|css| {
// As of this writing, only the base URL is used for property values:
let context = ParserContext::new(
::stylesheets::Origin::Author, base_url);
::stylesheets::Origin::Author, base_url, error_reporter);
Parser::new(&css).parse_entirely(|input| {
match from_shorthand {
None => {
@ -5668,15 +5673,15 @@ pub struct PropertyDeclarationBlock {
pub normal: Arc<Vec<PropertyDeclaration>>,
}
pub fn parse_style_attribute(input: &str, base_url: &Url) -> PropertyDeclarationBlock {
let context = ParserContext::new(Origin::Author, base_url);
pub fn parse_style_attribute(input: &str, base_url: &Url, error_reporter: Box<ParseErrorReporter + Send>)
-> PropertyDeclarationBlock {
let context = ParserContext::new(Origin::Author, base_url, error_reporter);
parse_property_declaration_list(&context, &mut Parser::new(input))
}
pub fn parse_one_declaration(name: &str, input: &str, base_url: &Url)
pub fn parse_one_declaration(name: &str, input: &str, base_url: &Url, error_reporter: Box<ParseErrorReporter + Send>)
-> Result<Vec<PropertyDeclaration>, ()> {
let context = ParserContext::new(Origin::Author, base_url);
let context = ParserContext::new(Origin::Author, base_url, error_reporter);
let mut results = vec![];
match PropertyDeclaration::parse(name, &context, &mut Parser::new(input), &mut results) {
PropertyDeclarationParseResult::ValidOrIgnoredDeclaration => Ok(results),
@ -6389,7 +6394,8 @@ fn cascade_with_cached_declarations(
parent_style: &ComputedValues,
cached_style: &ComputedValues,
custom_properties: Option<Arc<::custom_properties::ComputedValuesMap>>,
context: &computed::Context)
context: &computed::Context,
error_reporter: Box<ParseErrorReporter + Send>)
-> ComputedValues {
% for style_struct in STYLE_STRUCTS:
% if style_struct.inherited:
@ -6433,7 +6439,7 @@ fn cascade_with_cached_declarations(
.clone()
}
DeclaredValue::WithVariables { .. } => unreachable!()
}
}, error_reporter.clone()
);
Arc::make_mut(&mut style_${style_struct.ident})
.${property.ident} = computed_value;
@ -6488,7 +6494,8 @@ type CascadePropertyFn = extern "Rust" fn(declaration: &PropertyDeclaration,
inherited_style: &ComputedValues,
context: &computed::Context,
seen: &mut PropertyBitField,
cacheable: &mut bool);
cacheable: &mut bool,
error_reporter: Box<ParseErrorReporter + Send>);
// This is a thread-local rather than a lazy static to avoid atomic operations when cascading
// properties.
@ -6533,7 +6540,8 @@ pub fn cascade(viewport_size: Size2D<Au>,
applicable_declarations: &[DeclarationBlock<Vec<PropertyDeclaration>>],
shareable: bool,
parent_style: Option< &ComputedValues >,
cached_style: Option< &ComputedValues >)
cached_style: Option< &ComputedValues >,
error_reporter: Box<ParseErrorReporter + Send>)
-> (ComputedValues, bool) {
let initial_values = &*INITIAL_VALUES;
let (is_root_element, inherited_style) = match parent_style {
@ -6588,7 +6596,7 @@ pub fn cascade(viewport_size: Size2D<Au>,
// This assumes that the computed and specified values have the same Rust type.
macro_rules! get_specified(
($style_struct_getter: ident, $property: ident, $declared_value: expr) => {
($style_struct_getter: ident, $property: ident, $declared_value: expr, $error_reporter: expr) => {
concat_idents!(substitute_variables_, $property)(
$declared_value, &custom_properties, |value| match *value {
DeclaredValue::Value(specified_value) => specified_value,
@ -6597,7 +6605,7 @@ pub fn cascade(viewport_size: Size2D<Au>,
inherited_style.$style_struct_getter().$property.clone()
}
DeclaredValue::WithVariables { .. } => unreachable!()
}
}, $error_reporter.clone()
)
};
);
@ -6636,7 +6644,7 @@ pub fn cascade(viewport_size: Size2D<Au>,
DeclaredValue::Initial => longhands::font_size::get_initial_value(),
DeclaredValue::Inherit => context.inherited_font_size,
DeclaredValue::WithVariables { .. } => unreachable!(),
}
}, error_reporter.clone()
);
}
PropertyDeclaration::Color(ref value) => {
@ -6648,35 +6656,35 @@ pub fn cascade(viewport_size: Size2D<Au>,
DeclaredValue::Initial => longhands::color::get_initial_value(),
DeclaredValue::Inherit => inherited_style.get_color().color.clone(),
DeclaredValue::WithVariables { .. } => unreachable!(),
}
}, error_reporter.clone()
);
}
PropertyDeclaration::Display(ref value) => {
context.display = get_specified!(get_box, display, value);
context.display = get_specified!(get_box, display, value, error_reporter.clone());
}
PropertyDeclaration::Position(ref value) => {
context.positioned = match get_specified!(get_box, position, value) {
context.positioned = match get_specified!(get_box, position, value, error_reporter.clone()) {
longhands::position::SpecifiedValue::absolute |
longhands::position::SpecifiedValue::fixed => true,
_ => false,
}
}
PropertyDeclaration::OverflowX(ref value) => {
context.overflow_x = get_specified!(get_box, overflow_x, value);
context.overflow_x = get_specified!(get_box, overflow_x, value, error_reporter.clone());
}
PropertyDeclaration::OverflowY(ref value) => {
context.overflow_y = get_specified!(get_box, overflow_y, value);
context.overflow_y = get_specified!(get_box, overflow_y, value, error_reporter.clone());
}
PropertyDeclaration::Float(ref value) => {
context.floated = get_specified!(get_box, float, value)
context.floated = get_specified!(get_box, float, value, error_reporter.clone())
!= longhands::float::SpecifiedValue::none;
}
PropertyDeclaration::TextDecoration(ref value) => {
context.text_decoration = get_specified!(get_text, text_decoration, value);
context.text_decoration = get_specified!(get_text, text_decoration, value, error_reporter.clone());
}
PropertyDeclaration::OutlineStyle(ref value) => {
context.outline_style_present =
match get_specified!(get_outline, outline_style, value) {
match get_specified!(get_outline, outline_style, value, error_reporter.clone()) {
BorderStyle::none => false,
_ => true,
};
@ -6684,7 +6692,7 @@ pub fn cascade(viewport_size: Size2D<Au>,
% for side in ["top", "right", "bottom", "left"]:
PropertyDeclaration::Border${side.capitalize()}Style(ref value) => {
context.border_${side}_present =
match get_specified!(get_border, border_${side}_style, value) {
match get_specified!(get_border, border_${side}_style, value, error_reporter.clone()) {
BorderStyle::none | BorderStyle::hidden => false,
_ => true,
};
@ -6702,7 +6710,8 @@ pub fn cascade(viewport_size: Size2D<Au>,
parent_style,
cached_style,
custom_properties,
&context), false)
&context,
error_reporter), false)
}
(_, _) => {}
}
@ -6746,7 +6755,8 @@ pub fn cascade(viewport_size: Size2D<Au>,
inherited_style,
&context,
&mut seen,
&mut cacheable);
&mut cacheable,
error_reporter.clone());
}
}
});

View file

@ -2,6 +2,8 @@
* 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/. */
use cssparser::{Parser, SourcePosition};
use log;
use media_queries::{Device, MediaType};
use node::TElementAttributes;
use properties::{PropertyDeclaration, PropertyDeclarationBlock};
@ -14,6 +16,7 @@ use selectors::parser::PseudoElement;
use selectors::states::*;
use smallvec::VecLike;
use std::process;
use style_traits::ParseErrorReporter;
use style_traits::viewport::ViewportConstraints;
use stylesheets::{CSSRuleIteratorExt, Origin, Stylesheet};
use url::Url;
@ -24,6 +27,20 @@ use viewport::{MaybeNew, ViewportRuleCascade};
pub type DeclarationBlock = GenericDeclarationBlock<Vec<PropertyDeclaration>>;
struct StdoutErrorReporter;
impl ParseErrorReporter for StdoutErrorReporter {
fn report_error(&self, input: &mut Parser, position: SourcePosition, message: &str) {
if log_enabled!(log::LogLevel::Info) {
let location = input.source_location(position);
info!("{}:{} {}", location.line, location.column, message)
}
}
fn clone(&self) -> Box<ParseErrorReporter + Send + Sync> {
box StdoutErrorReporter
}
}
lazy_static! {
pub static ref USER_OR_USER_AGENT_STYLESHEETS: Vec<Stylesheet> = {
@ -38,7 +55,8 @@ lazy_static! {
Url::parse(&format!("chrome:///{:?}", filename)).unwrap(),
None,
None,
Origin::UserAgent);
Origin::UserAgent,
box StdoutErrorReporter);
stylesheets.push(ua_stylesheet);
}
Err(..) => {
@ -49,7 +67,7 @@ lazy_static! {
}
for &(ref contents, ref url) in &opts::get().user_stylesheets {
stylesheets.push(Stylesheet::from_bytes(
&contents, url.clone(), None, None, Origin::User));
&contents, url.clone(), None, None, Origin::User, box StdoutErrorReporter));
}
stylesheets
};
@ -64,7 +82,8 @@ lazy_static! {
url!("chrome:///quirks-mode.css"),
None,
None,
Origin::UserAgent)
Origin::UserAgent,
box StdoutErrorReporter)
},
Err(..) => {
error!("Stylist failed to load 'quirks-mode.css'!");

View file

@ -16,10 +16,12 @@ use std::cell::Cell;
use std::iter::Iterator;
use std::slice;
use string_cache::{Atom, Namespace};
use style_traits::ParseErrorReporter;
use url::Url;
use util::mem::HeapSizeOf;
use viewport::ViewportRule;
/// Each style rule has an origin, which determines where it enters the cascade.
///
/// http://dev.w3.org/csswg/css-cascade/#cascading-origins
@ -80,31 +82,33 @@ pub struct StyleRule {
impl Stylesheet {
pub fn from_bytes_iter<I: Iterator<Item=Vec<u8>>>(
input: I, base_url: Url, protocol_encoding_label: Option<&str>,
environment_encoding: Option<EncodingRef>, origin: Origin) -> Stylesheet {
environment_encoding: Option<EncodingRef>, origin: Origin,
error_reporter: Box<ParseErrorReporter + Send>) -> Stylesheet {
let mut bytes = vec![];
// TODO: incremental decoding and tokenization/parsing
for chunk in input {
bytes.push_all(&chunk)
}
Stylesheet::from_bytes(&bytes, base_url, protocol_encoding_label,
environment_encoding, origin)
environment_encoding, origin, error_reporter)
}
pub fn from_bytes(bytes: &[u8],
base_url: Url,
protocol_encoding_label: Option<&str>,
environment_encoding: Option<EncodingRef>,
origin: Origin)
origin: Origin, error_reporter: Box<ParseErrorReporter + Send>)
-> Stylesheet {
// TODO: bytes.as_slice could be bytes.container_as_bytes()
let (string, _) = decode_stylesheet_bytes(
bytes, protocol_encoding_label, environment_encoding);
Stylesheet::from_str(&string, base_url, origin)
Stylesheet::from_str(&string, base_url, origin, error_reporter)
}
pub fn from_str(css: &str, base_url: Url, origin: Origin) -> Stylesheet {
pub fn from_str(css: &str, base_url: Url, origin: Origin,
error_reporter: Box<ParseErrorReporter + Send>) -> Stylesheet {
let rule_parser = TopLevelRuleParser {
context: ParserContext::new(origin, &base_url),
context: ParserContext::new(origin, &base_url, error_reporter),
state: Cell::new(State::Start),
};
let mut input = Parser::new(css);