Use cssparser's new_with_line_number_offset

cssparser provides a way to set the initial line number on a
ParserInput.  This patch changes servo to use this facility, rather than
reimplement the same functionality itself.
This commit is contained in:
Tom Tromey 2017-08-24 07:37:02 -06:00
parent a266e96d28
commit 546ecaeee9
11 changed files with 26 additions and 60 deletions

View file

@ -37,7 +37,7 @@ bitflags = "0.7"
bit-vec = "0.4.3"
byteorder = "1.0"
cfg-if = "0.1.0"
cssparser = "0.19.3"
cssparser = "0.19.5"
encoding = {version = "0.2", optional = true}
euclid = "0.15"
fnv = "1.0"

View file

@ -72,7 +72,7 @@ impl Stylesheet {
stylesheet_loader,
error_reporter,
quirks_mode,
0u64)
0)
}
/// Updates an empty stylesheet with a set of bytes that reached over the

View file

@ -53,8 +53,6 @@ pub struct ParserContext<'a> {
pub url_data: &'a UrlExtraData,
/// The current rule type, if any.
pub rule_type: Option<CssRuleType>,
/// Line number offsets for inline stylesheets
pub line_number_offset: u64,
/// The mode to use when parsing.
pub parsing_mode: ParsingMode,
/// The quirks mode of this stylesheet.
@ -76,7 +74,6 @@ impl<'a> ParserContext<'a> {
stylesheet_origin: stylesheet_origin,
url_data: url_data,
rule_type: rule_type,
line_number_offset: 0u64,
parsing_mode: parsing_mode,
quirks_mode: quirks_mode,
namespaces: None,
@ -109,32 +106,12 @@ impl<'a> ParserContext<'a> {
stylesheet_origin: context.stylesheet_origin,
url_data: context.url_data,
rule_type: Some(rule_type),
line_number_offset: context.line_number_offset,
parsing_mode: context.parsing_mode,
quirks_mode: context.quirks_mode,
namespaces: Some(namespaces),
}
}
/// Create a parser context for inline CSS which accepts additional line offset argument.
pub fn new_with_line_number_offset(
stylesheet_origin: Origin,
url_data: &'a UrlExtraData,
line_number_offset: u64,
parsing_mode: ParsingMode,
quirks_mode: QuirksMode,
) -> ParserContext<'a> {
ParserContext {
stylesheet_origin: stylesheet_origin,
url_data: url_data,
rule_type: None,
line_number_offset: line_number_offset,
parsing_mode: parsing_mode,
quirks_mode: quirks_mode,
namespaces: None,
}
}
/// 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.")
@ -148,7 +125,7 @@ impl<'a> ParserContext<'a> {
where R: ParseErrorReporter
{
let location = SourceLocation {
line: location.line + self.line_number_offset as u32,
line: location.line,
column: location.column,
};
context.error_reporter.report_error(self.url_data, location, error)

View file

@ -160,10 +160,7 @@ impl<'a, 'i, R: ParseErrorReporter> AtRuleParser<'i> for TopLevelRuleParser<'a,
name: CowRcStr<'i>,
input: &mut Parser<'i, 't>
) -> Result<AtRuleType<AtRulePrelude, CssRule>, ParseError<'i>> {
let location = get_location_with_offset(
input.current_source_location(),
self.context.line_number_offset,
);
let location = get_location_with_offset(input.current_source_location());
match_ignore_ascii_case! { &*name,
"import" => {
if self.state > State::Imports {
@ -334,11 +331,7 @@ impl<'a, 'b, 'i, R: ParseErrorReporter> AtRuleParser<'i> for NestedRuleParser<'a
name: CowRcStr<'i>,
input: &mut Parser<'i, 't>
) -> Result<AtRuleType<AtRulePrelude, CssRule>, ParseError<'i>> {
let location =
get_location_with_offset(
input.current_source_location(),
self.context.line_number_offset
);
let location = get_location_with_offset(input.current_source_location());
match_ignore_ascii_case! { &*name,
"media" => {
@ -545,8 +538,7 @@ impl<'a, 'b, 'i, R: ParseErrorReporter> QualifiedRuleParser<'i> for NestedRulePa
url_data: Some(self.context.url_data),
};
let location = get_location_with_offset(input.current_source_location(),
self.context.line_number_offset);
let location = get_location_with_offset(input.current_source_location());
let selectors = SelectorList::parse(&selector_parser, input)?;
Ok(QualifiedRuleParserPrelude {
@ -575,13 +567,10 @@ impl<'a, 'b, 'i, R: ParseErrorReporter> QualifiedRuleParser<'i> for NestedRulePa
}
}
/// Calculates the location of a rule's source given an offset.
fn get_location_with_offset(
location: SourceLocation,
offset: u64
) -> SourceLocation {
/// Adjust a location's column to accommodate DevTools.
fn get_location_with_offset(location: SourceLocation) -> SourceLocation {
SourceLocation {
line: location.line + offset as u32,
line: location.line,
// Column offsets are not yet supported, but Gecko devtools expect 1-based columns.
column: location.column + 1,
}

View file

@ -73,7 +73,7 @@ impl StylesheetContents {
stylesheet_loader: Option<&StylesheetLoader>,
error_reporter: &R,
quirks_mode: QuirksMode,
line_number_offset: u64
line_number_offset: u32
) -> Self {
let namespaces = RwLock::new(Namespaces::default());
let (rules, source_map_url) = Stylesheet::parse_rules(
@ -311,7 +311,7 @@ impl Stylesheet {
url_data: UrlExtraData,
stylesheet_loader: Option<&StylesheetLoader>,
error_reporter: &R,
line_number_offset: u64)
line_number_offset: u32)
where R: ParseErrorReporter
{
let namespaces = RwLock::new(Namespaces::default());
@ -349,17 +349,17 @@ impl Stylesheet {
stylesheet_loader: Option<&StylesheetLoader>,
error_reporter: &R,
quirks_mode: QuirksMode,
line_number_offset: u64
line_number_offset: u32
) -> (Vec<CssRule>, Option<String>) {
let mut rules = Vec::new();
let mut input = ParserInput::new(css);
let mut input = ParserInput::new_with_line_number_offset(css, line_number_offset);
let mut input = Parser::new(&mut input);
let context =
ParserContext::new_with_line_number_offset(
ParserContext::new(
origin,
url_data,
line_number_offset,
None,
PARSING_MODE_DEFAULT,
quirks_mode
);
@ -410,7 +410,7 @@ impl Stylesheet {
stylesheet_loader: Option<&StylesheetLoader>,
error_reporter: &R,
quirks_mode: QuirksMode,
line_number_offset: u64)
line_number_offset: u32)
-> Stylesheet
{
let contents = StylesheetContents::from_str(