Skip to content

Commit

Permalink
tokenizer: Look for both env() and var() functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
emilio committed Nov 5, 2018
1 parent cb39f2a commit 7f0461a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "cssparser"
version = "0.24.1"
version = "0.25.0"
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]

description = "Rust implementation of CSS Syntax Level 3"
Expand Down
16 changes: 9 additions & 7 deletions src/parser.rs
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
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
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

0 comments on commit 7f0461a

Please sign in to comment.