Skip to content

Truly independent web browser

LibWeb: Read less to note selector facts and source positions

Andreas Kling85d6eb0afd8c
diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/structure.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/structure.rs
index c58e7c4f9a0..cb5cc92e380 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/structure.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/structure.rs
@@ -500,26 +500,30 @@ struct LineCursor {
impl LineCursor {
fn position<U: Unit>(&mut self, units: &[U], offset: usize) -> SourcePosition {
- fn code_points<U: Unit>(units: &[U]) -> usize {
- if !U::WIDE {
- return units.len();
- }
- // NB: The source has no lone surrogate, so each low surrogate is the second unit of a code point.
- units.iter().filter(|unit| unit.code() & 0xFC00 != 0xDC00).count()
- }
if offset < self.offset {
*self = Self::default();
}
+ // NB: The places are near each other, so this reads the units between them once.
let units = &units[self.offset..offset];
- let is_newline = |unit: &U| unit.code() == 0x0A;
- let lines = units.iter().filter(|unit| is_newline(unit)).count();
- if lines == 0 {
- self.column += code_points(units);
- } else {
- let last = units.iter().rposition(is_newline).unwrap();
+ let mut lines = 0;
+ let mut line_start = 0;
+ // NB: The source has no lone surrogate, so each low surrogate is the second unit of a code point.
+ let mut low_surrogates = 0;
+ for (index, unit) in units.iter().enumerate() {
+ let code = unit.code();
+ if code == 0x0A {
+ lines += 1;
+ line_start = index + 1;
+ low_surrogates = 0;
+ } else if U::WIDE && code & 0xFC00 == 0xDC00 {
+ low_surrogates += 1;
+ }
+ }
+ if lines != 0 {
self.line += lines;
- self.column = code_points(&units[last + 1..]);
+ self.column = 0;
}
+ self.column += units.len() - line_start - low_surrogates;
self.offset = offset;
SourcePosition {
line: self.line,
diff --git a/Libraries/LibWeb/Rust/src/css/selector_arena.rs b/Libraries/LibWeb/Rust/src/css/selector_arena.rs
index cb9f79c7abb..8dd1555ea38 100644
--- a/Libraries/LibWeb/Rust/src/css/selector_arena.rs
+++ b/Libraries/LibWeb/Rust/src/css/selector_arena.rs
@@ -2292,8 +2292,18 @@ impl SelectorArena {
.unwrap_or(index);
let describes_its_subject =
!(start..=subject_end).any(|at| self.components[at].bits.kind() == ComponentKind::PseudoElement);
- let flags = (u32::from(names_the_scope(selector)) * NAMES_THE_SCOPE)
- | (u32::from(escapes_its_namespace(selector)) * ESCAPES_ITS_NAMESPACE)
+ // NB: Only a pseudo-class or a `&` can name the scope, and only a pseudo-element can escape the namespace.
+ // Most selectors have none of them, and are then not read as selectors here.
+ let (mut may_name_the_scope, mut may_escape_its_namespace) = (false, false);
+ for component in &self.components[start..=index] {
+ match component.bits.kind() {
+ ComponentKind::PseudoClass | ComponentKind::Nesting => may_name_the_scope = true,
+ ComponentKind::PseudoElement => may_escape_its_namespace = true,
+ _ => {}
+ }
+ }
+ let flags = (u32::from(may_name_the_scope && names_the_scope(selector)) * NAMES_THE_SCOPE)
+ | (u32::from(may_escape_its_namespace && escapes_its_namespace(selector)) * ESCAPES_ITS_NAMESPACE)
| (u32::from(describes_its_subject) * DESCRIBES_ITS_SUBJECT);
if flags != 0 {
facts.push((start, flags));