Skip to content

Commit 49d1d30

Browse files
committed
Record whether viewport percentage dimensions are seen
Used to tell Servo to recalulate styles on window resize (servo/servo#8754).
1 parent d5c93cf commit 49d1d30

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

src/parser.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,20 @@ impl<'i, 't> Parser<'i, 't> {
236236
self.tokenizer.seen_var_functions()
237237
}
238238

239+
/// Start looking for viewport percentage lengths. (See the `seen_viewport_percentages`
240+
/// method.)
241+
#[inline]
242+
pub fn look_for_viewport_percentages(&mut self) {
243+
self.tokenizer.look_for_viewport_percentages()
244+
}
245+
246+
/// Return whether a `vh`, `vw`, `vmin`, or `vmax` dimension has been seen by the tokenizer
247+
/// since `look_for_viewport_percentages` was called, and stop looking.
248+
#[inline]
249+
pub fn seen_viewport_percentages(&mut self) -> bool {
250+
self.tokenizer.seen_viewport_percentages()
251+
}
252+
239253
/// Execute the given closure, passing it the parser.
240254
/// If the result (returned unchanged) is `Err`,
241255
/// the internal state of the parser (including position within the input)

src/tokenizer.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,12 @@ pub struct Tokenizer<'a> {
211211
position: usize,
212212
/// Cache for `source_location()`
213213
last_known_line_break: Cell<(usize, usize)>,
214-
var_functions: VarFunctions,
214+
var_functions: SeenStatus,
215+
viewport_percentages: SeenStatus,
215216
}
216217

217218
#[derive(Copy, Clone, PartialEq, Eq)]
218-
enum VarFunctions {
219+
enum SeenStatus {
219220
DontCare,
220221
LookingForThem,
221222
SeenAtLeastOne,
@@ -229,19 +230,32 @@ impl<'a> Tokenizer<'a> {
229230
input: input,
230231
position: 0,
231232
last_known_line_break: Cell::new((1, 0)),
232-
var_functions: VarFunctions::DontCare,
233+
var_functions: SeenStatus::DontCare,
234+
viewport_percentages: SeenStatus::DontCare,
233235
}
234236
}
235237

236238
#[inline]
237239
pub fn look_for_var_functions(&mut self) {
238-
self.var_functions = VarFunctions::LookingForThem;
240+
self.var_functions = SeenStatus::LookingForThem;
239241
}
240242

241243
#[inline]
242244
pub fn seen_var_functions(&mut self) -> bool {
243-
let seen = self.var_functions == VarFunctions::SeenAtLeastOne;
244-
self.var_functions = VarFunctions::DontCare;
245+
let seen = self.var_functions == SeenStatus::SeenAtLeastOne;
246+
self.var_functions = SeenStatus::DontCare;
247+
seen
248+
}
249+
250+
#[inline]
251+
pub fn look_for_viewport_percentages(&mut self) {
252+
self.viewport_percentages = SeenStatus::LookingForThem;
253+
}
254+
255+
#[inline]
256+
pub fn seen_viewport_percentages(&mut self) -> bool {
257+
let seen = self.viewport_percentages == SeenStatus::SeenAtLeastOne;
258+
self.viewport_percentages = SeenStatus::DontCare;
245259
seen
246260
}
247261

@@ -630,9 +644,9 @@ fn consume_ident_like<'a>(tokenizer: &mut Tokenizer<'a>) -> Token<'a> {
630644
if value.eq_ignore_ascii_case("url") {
631645
consume_unquoted_url(tokenizer).unwrap_or(Function(value))
632646
} else {
633-
if tokenizer.var_functions == VarFunctions::LookingForThem &&
647+
if tokenizer.var_functions == SeenStatus::LookingForThem &&
634648
value.eq_ignore_ascii_case("var") {
635-
tokenizer.var_functions = VarFunctions::SeenAtLeastOne;
649+
tokenizer.var_functions = SeenStatus::SeenAtLeastOne;
636650
}
637651
Function(value)
638652
}
@@ -784,7 +798,16 @@ fn consume_numeric<'a>(tokenizer: &mut Tokenizer<'a>) -> Token<'a> {
784798
has_sign: has_sign,
785799
};
786800
if is_ident_start(tokenizer) {
787-
Dimension(value, consume_name(tokenizer))
801+
let name = consume_name(tokenizer);
802+
if tokenizer.viewport_percentages == SeenStatus::LookingForThem {
803+
if name.eq_ignore_ascii_case("vh") ||
804+
name.eq_ignore_ascii_case("vw") ||
805+
name.eq_ignore_ascii_case("vmin") ||
806+
name.eq_ignore_ascii_case("vmax") {
807+
tokenizer.viewport_percentages = SeenStatus::SeenAtLeastOne;
808+
}
809+
}
810+
Dimension(value, name)
788811
} else {
789812
Number(value)
790813
}

0 commit comments

Comments
 (0)