diff --git a/Cargo.toml b/Cargo.toml index 958b7a40..da3444ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cssparser" -version = "0.19.4" +version = "0.19.5" authors = [ "Simon Sapin " ] description = "Rust implementation of CSS Syntax Level 3" diff --git a/src/parser.rs b/src/parser.rs index 534eefc5..66b26e1a 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -101,6 +101,15 @@ impl<'i> ParserInput<'i> { } } + /// Create a new input for a parser. Line numbers in locations + /// are offset by the given value. + pub fn new_with_line_number_offset(input: &'i str, first_line_number: u32) -> ParserInput<'i> { + ParserInput { + tokenizer: Tokenizer::with_first_line_number(input, first_line_number), + cached_token: None, + } + } + #[inline] fn cached_token_ref(&self) -> &Token<'i> { &self.cached_token.as_ref().unwrap().token diff --git a/src/tests.rs b/src/tests.rs index 3a0439e7..a80c891b 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -970,6 +970,20 @@ fn parser_maintains_current_line() { assert_eq!(parser.current_line(), "ident"); } +#[test] +fn parser_with_line_number_offset() { + let mut input = ParserInput::new_with_line_number_offset("ident\nident", 72); + let mut parser = Parser::new(&mut input); + assert_eq!(parser.current_source_location(), SourceLocation { line: 72, column: 0 }); + assert_eq!(parser.next_including_whitespace_and_comments(), Ok(&Token::Ident("ident".into()))); + assert_eq!(parser.current_source_location(), SourceLocation { line: 72, column: 5 }); + assert_eq!(parser.next_including_whitespace_and_comments(), + Ok(&Token::WhiteSpace("\n".into()))); + assert_eq!(parser.current_source_location(), SourceLocation { line: 73, column: 0 }); + assert_eq!(parser.next_including_whitespace_and_comments(), Ok(&Token::Ident("ident".into()))); + assert_eq!(parser.current_source_location(), SourceLocation { line: 73, column: 5 }); +} + #[test] fn cdc_regression_test() { let mut input = ParserInput::new("-->x");