Skip to content

Extract source map URL from directive comments #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cssparser"
version = "0.19.0"
version = "0.19.1"
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]

description = "Rust implementation of CSS Syntax Level 3"
Expand Down
9 changes: 9 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,15 @@ impl<'i: 't, 't> Parser<'i, 't> {
self.input.tokenizer.current_source_location()
}

/// The source map URL, if known.
///
/// The source map URL is extracted from a specially formatted
/// comment. The last such comment is used, so this value may
/// change as parsing proceeds.
pub fn current_source_map_url(&self) -> Option<&str> {
self.input.tokenizer.current_source_map_url()
}

/// Return the current internal state of the parser (including position within the input).
///
/// This state can later be restored with the `Parser::reset` method.
Expand Down
4 changes: 2 additions & 2 deletions src/size_of_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ size_of_test!(token, Token, 32);
size_of_test!(std_cow_str, Cow<'static, str>, 32);
size_of_test!(cow_rc_str, CowRcStr, 16);

size_of_test!(tokenizer, ::tokenizer::Tokenizer, 40);
size_of_test!(parser_input, ::parser::ParserInput, 112);
size_of_test!(tokenizer, ::tokenizer::Tokenizer, 56);
size_of_test!(parser_input, ::parser::ParserInput, 128);
size_of_test!(parser, ::parser::Parser, 16);
size_of_test!(source_position, ::SourcePosition, 8);
size_of_test!(parser_state, ::ParserState, 24);
Expand Down
25 changes: 25 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -979,3 +979,28 @@ fn parse_entirely_reports_first_error() {
let result: Result<(), _> = parser.parse_entirely(|_| Err(ParseError::Custom(E::Foo)));
assert_eq!(result, Err(ParseError::Custom(E::Foo)));
}

#[test]
fn parse_comments() {
let tests = vec![
("/*# sourceMappingURL=here*/", Some("here")),
("/*# sourceMappingURL=here */", Some("here")),
("/*@ sourceMappingURL=here*/", Some("here")),
("/*@ sourceMappingURL=there*/ /*# sourceMappingURL=here*/", Some("here")),
("/*# sourceMappingURL=here there */", Some("here")),
("/*# sourceMappingURL= here */", Some("")),
("/*# sourceMappingURL=*/", Some("")),
("/*# sourceMappingUR=here */", None),
("/*! sourceMappingURL=here */", None),
("/*# sourceMappingURL = here */", None),
("/* # sourceMappingURL=here */", None)
];

for test in tests {
let mut input = ParserInput::new(test.0);
let mut parser = Parser::new(&mut input);
while let Ok(_) = parser.next_including_whitespace() {
}
assert_eq!(parser.current_source_map_url(), test.1);
}
}
25 changes: 24 additions & 1 deletion src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ pub struct Tokenizer<'a> {
current_line_number: u32,
var_functions: SeenStatus,
viewport_percentages: SeenStatus,
source_map_url: Option<&'a str>,
}

#[derive(Copy, Clone, PartialEq, Eq)]
Expand All @@ -234,6 +235,7 @@ impl<'a> Tokenizer<'a> {
current_line_number: first_line_number,
var_functions: SeenStatus::DontCare,
viewport_percentages: SeenStatus::DontCare,
source_map_url: None,
}
}

Expand Down Expand Up @@ -300,6 +302,11 @@ impl<'a> Tokenizer<'a> {
}
}

#[inline]
pub fn current_source_map_url(&self) -> Option<&'a str> {
self.source_map_url
}

#[inline]
pub fn state(&self) -> ParserState {
ParserState {
Expand Down Expand Up @@ -507,7 +514,9 @@ fn next_token<'a>(tokenizer: &mut Tokenizer<'a>) -> Result<Token<'a>, ()> {
}
b'/' => {
if tokenizer.starts_with(b"/*") {
Comment(consume_comment(tokenizer))
let contents = consume_comment(tokenizer);
check_for_source_map(tokenizer, contents);
Comment(contents)
} else {
tokenizer.advance(1);
Delim('/')
Expand Down Expand Up @@ -594,6 +603,20 @@ fn consume_whitespace<'a>(tokenizer: &mut Tokenizer<'a>, newline: bool, is_cr: b
}


// Check for a sourceMappingURL comment and update the tokenizer appropriately.
fn check_for_source_map<'a>(tokenizer: &mut Tokenizer<'a>, contents: &'a str) {
let directive = "# sourceMappingURL=";
let directive_old = "@ sourceMappingURL=";

// If there is a source map directive, extract the URL.
if contents.starts_with(directive) || contents.starts_with(directive_old) {
let contents = &contents[directive.len()..];
tokenizer.source_map_url = contents.split(|c| {
c == ' ' || c == '\t' || c == '\x0C' || c == '\r' || c == '\n'
}).next()
}
}

fn consume_comment<'a>(tokenizer: &mut Tokenizer<'a>) -> &'a str {
tokenizer.advance(2); // consume "/*"
let start_position = tokenizer.position();
Expand Down