Skip to content

Record whether viewport percentage dimensions are seen #99

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
Mar 19, 2016
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,7 +1,7 @@
[package]

name = "cssparser"
version = "0.5.4"
version = "0.5.5"
authors = [ "Simon Sapin <[email protected]>" ]

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

/// Start looking for viewport percentage lengths. (See the `seen_viewport_percentages`
/// method.)
#[inline]
pub fn look_for_viewport_percentages(&mut self) {
self.tokenizer.look_for_viewport_percentages()
}

/// Return whether a `vh`, `vw`, `vmin`, or `vmax` dimension has been seen by the tokenizer
/// since `look_for_viewport_percentages` was called, and stop looking.
#[inline]
pub fn seen_viewport_percentages(&mut self) -> bool {
self.tokenizer.seen_viewport_percentages()
}

/// Execute the given closure, passing it the parser.
/// If the result (returned unchanged) is `Err`,
/// the internal state of the parser (including position within the input)
Expand Down
41 changes: 32 additions & 9 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,12 @@ pub struct Tokenizer<'a> {
position: usize,
/// Cache for `source_location()`
last_known_line_break: Cell<(usize, usize)>,
var_functions: VarFunctions,
var_functions: SeenStatus,
viewport_percentages: SeenStatus,
}

#[derive(Copy, Clone, PartialEq, Eq)]
enum VarFunctions {
enum SeenStatus {
DontCare,
LookingForThem,
SeenAtLeastOne,
Expand All @@ -229,19 +230,32 @@ impl<'a> Tokenizer<'a> {
input: input,
position: 0,
last_known_line_break: Cell::new((1, 0)),
var_functions: VarFunctions::DontCare,
var_functions: SeenStatus::DontCare,
viewport_percentages: SeenStatus::DontCare,
}
}

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

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

#[inline]
pub fn look_for_viewport_percentages(&mut self) {
self.viewport_percentages = SeenStatus::LookingForThem;
}

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

Expand Down Expand Up @@ -630,9 +644,9 @@ fn consume_ident_like<'a>(tokenizer: &mut Tokenizer<'a>) -> Token<'a> {
if value.eq_ignore_ascii_case("url") {
consume_unquoted_url(tokenizer).unwrap_or(Function(value))
} else {
if tokenizer.var_functions == VarFunctions::LookingForThem &&
if tokenizer.var_functions == SeenStatus::LookingForThem &&
value.eq_ignore_ascii_case("var") {
tokenizer.var_functions = VarFunctions::SeenAtLeastOne;
tokenizer.var_functions = SeenStatus::SeenAtLeastOne;
}
Function(value)
}
Expand Down Expand Up @@ -784,7 +798,16 @@ fn consume_numeric<'a>(tokenizer: &mut Tokenizer<'a>) -> Token<'a> {
has_sign: has_sign,
};
if is_ident_start(tokenizer) {
Dimension(value, consume_name(tokenizer))
let name = consume_name(tokenizer);
if tokenizer.viewport_percentages == SeenStatus::LookingForThem {
if name.eq_ignore_ascii_case("vh") ||
name.eq_ignore_ascii_case("vw") ||
name.eq_ignore_ascii_case("vmin") ||
name.eq_ignore_ascii_case("vmax") {
tokenizer.viewport_percentages = SeenStatus::SeenAtLeastOne;
}
}
Dimension(value, name)
} else {
Number(value)
}
Expand Down