Skip to content

tokenizer: Look for both env() and var() functions. #230

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 1 commit into from
Nov 5, 2018
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.24.1"
version = "0.25.0"
authors = [ "Simon Sapin <[email protected]>" ]

description = "Rust implementation of CSS Syntax Level 3"
Expand Down
16 changes: 9 additions & 7 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,17 +470,19 @@ impl<'i: 't, 't> Parser<'i, 't> {
self.at_start_of = state.at_start_of;
}

/// Start looking for `var()` functions. (See the `.seen_var_functions()` method.)
/// Start looking for `var()` / `env()` functions. (See the
/// `.seen_var_or_env_functions()` method.)
#[inline]
pub fn look_for_var_functions(&mut self) {
self.input.tokenizer.look_for_var_functions()
pub fn look_for_var_or_env_functions(&mut self) {
self.input.tokenizer.look_for_var_or_env_functions()
}

/// Return whether a `var()` function has been seen by the tokenizer since
/// either `look_for_var_functions` was called, and stop looking.
/// Return whether a `var()` or `env()` function has been seen by the
/// tokenizer since either `look_for_var_or_env_functions` was called, and
/// stop looking.
#[inline]
pub fn seen_var_functions(&mut self) -> bool {
self.input.tokenizer.seen_var_functions()
pub fn seen_var_or_env_functions(&mut self) -> bool {
self.input.tokenizer.seen_var_or_env_functions()
}

/// Execute the given closure, passing it the parser.
Expand Down
6 changes: 3 additions & 3 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,14 +685,14 @@ fn unquoted_url(b: &mut Bencher) {
b.iter(|| {
let mut input = ParserInput::new(BACKGROUND_IMAGE);
let mut input = Parser::new(&mut input);
input.look_for_var_functions();
input.look_for_var_or_env_functions();

let result = input.try(|input| input.expect_url());

assert!(result.is_ok());

input.seen_var_functions();
(result.is_ok(), input.seen_var_functions())
input.seen_var_or_env_functions();
(result.is_ok(), input.seen_var_or_env_functions())
})
}

Expand Down
22 changes: 12 additions & 10 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ pub struct Tokenizer<'a> {
/// of UTF-16 characters.
current_line_start_position: usize,
current_line_number: u32,
var_functions: SeenStatus,
var_or_env_functions: SeenStatus,
source_map_url: Option<&'a str>,
source_url: Option<&'a str>,
}
Expand All @@ -234,29 +234,31 @@ impl<'a> Tokenizer<'a> {
position: 0,
current_line_start_position: 0,
current_line_number: first_line_number,
var_functions: SeenStatus::DontCare,
var_or_env_functions: SeenStatus::DontCare,
source_map_url: None,
source_url: None,
}
}

#[inline]
pub fn look_for_var_functions(&mut self) {
self.var_functions = SeenStatus::LookingForThem;
pub fn look_for_var_or_env_functions(&mut self) {
self.var_or_env_functions = SeenStatus::LookingForThem;
}

#[inline]
pub fn seen_var_functions(&mut self) -> bool {
let seen = self.var_functions == SeenStatus::SeenAtLeastOne;
self.var_functions = SeenStatus::DontCare;
pub fn seen_var_or_env_functions(&mut self) -> bool {
let seen = self.var_or_env_functions == SeenStatus::SeenAtLeastOne;
self.var_or_env_functions = SeenStatus::DontCare;
seen
}

#[inline]
pub fn see_function(&mut self, name: &str) {
if self.var_functions == SeenStatus::LookingForThem {
if name.eq_ignore_ascii_case("var") {
self.var_functions = SeenStatus::SeenAtLeastOne;
if self.var_or_env_functions == SeenStatus::LookingForThem {
if name.eq_ignore_ascii_case("var") ||
name.eq_ignore_ascii_case("env")
{
self.var_or_env_functions = SeenStatus::SeenAtLeastOne;
}
}
}
Expand Down