Skip to content

Truly independent web browser

LibWeb: Detach kept media, supports and container queries from sheets

Andreas Klingffc5addc037b
diff --git a/Libraries/LibWeb/Rust/src/css/css_tokenizer.rs b/Libraries/LibWeb/Rust/src/css/css_tokenizer.rs
index bb73011139d..f847a0a551f 100644
--- a/Libraries/LibWeb/Rust/src/css/css_tokenizer.rs
+++ b/Libraries/LibWeb/Rust/src/css/css_tokenizer.rs
@@ -357,6 +357,13 @@ pub(crate) enum ParserString {
}
impl ParserString {
+ /// Moves the string out of the storage it shares with the other strings of its tokens (see `TextDetacher`).
+ pub(crate) fn detach(&mut self, detacher: &mut TextDetacher) {
+ if let Self::Shared { storage, range } = self {
+ detacher.string(storage, range);
+ }
+ }
+
fn finish_shared(&mut self, storage: Arc<[u16]>, offset: usize) {
let Self::Pending(range) = self else {
return;
@@ -442,8 +449,21 @@ pub(crate) enum ParserTokenKind {
}
impl ParserTokenKind {
+ /// Detaches the token's string, if it has one (see `ParserString::detach`).
+ pub(crate) fn detach(&mut self, detacher: &mut TextDetacher) {
+ if let Some(string) = self.string_mut() {
+ string.detach(detacher);
+ }
+ }
+
fn finish_shared_strings(&mut self, storage: Arc<[u16]>, offset: usize) {
- let string = match self {
+ if let Some(string) = self.string_mut() {
+ string.finish_shared(storage, offset);
+ }
+ }
+
+ fn string_mut(&mut self) -> Option<&mut ParserString> {
+ match self {
Self::Ident(value)
| Self::Function(value)
| Self::AtKeyword(value)
@@ -452,9 +472,6 @@ impl ParserTokenKind {
Self::Hash { value, .. } => Some(value),
Self::Dimension { unit, .. } => Some(unit),
_ => None,
- };
- if let Some(string) = string {
- string.finish_shared(storage, offset);
}
}
}
@@ -499,6 +516,61 @@ impl SourceStorage {
}
}
+/// Moves the text that values outliving their parse keep out of the storage they share with the rest of the sheet, so
+/// that keeping them does not keep the whole sheet's text alive. The values are walked twice: once to find the span of
+/// each storage they keep, and once to point them into one copy of that span.
+#[derive(Default)]
+pub(crate) struct TextDetacher {
+ sources: DetachedSpans<SourceStorage>,
+ strings: DetachedSpans<[u16]>,
+}
+
+/// Each storage values keep text of, the span of it they keep, and its copy once it is made.
+type DetachedSpans<T> = Vec<(Arc<T>, Range<usize>, Option<Arc<T>>)>;
+
+impl TextDetacher {
+ /// Detaches the text of the values `walk` hands to the detacher, by calling it twice.
+ pub(crate) fn detach(mut walk: impl FnMut(&mut Self)) {
+ let mut detacher = Self::default();
+ walk(&mut detacher);
+ for (storage, span, copy) in &mut detacher.sources {
+ *copy = Some(Arc::new(match storage.as_ref() {
+ SourceStorage::Ascii(units) => SourceStorage::Ascii(units[span.clone()].into()),
+ SourceStorage::Utf16(units) => SourceStorage::Utf16(units[span.clone()].into()),
+ }));
+ }
+ for (storage, span, copy) in &mut detacher.strings {
+ *copy = Some(storage[span.clone()].into());
+ }
+ walk(&mut detacher);
+ }
+
+ fn source(&mut self, storage: &mut Arc<SourceStorage>, range: &mut Range<usize>) {
+ Self::visit(&mut self.sources, storage, range);
+ }
+
+ fn string(&mut self, storage: &mut Arc<[u16]>, range: &mut Range<usize>) {
+ Self::visit(&mut self.strings, storage, range);
+ }
+
+ fn visit<T: ?Sized>(spans: &mut DetachedSpans<T>, storage: &mut Arc<T>, range: &mut Range<usize>) {
+ let Some((_, span, copy)) = spans.iter_mut().find(|(candidate, ..)| Arc::ptr_eq(candidate, storage)) else {
+ spans.push((Arc::clone(storage), range.clone(), None));
+ return;
+ };
+ match copy {
+ None => {
+ span.start = span.start.min(range.start);
+ span.end = span.end.max(range.end);
+ }
+ Some(copy) => {
+ *storage = Arc::clone(copy);
+ *range = range.start - span.start..range.end - span.start;
+ }
+ }
+ }
+}
+
pub(crate) enum ParserSourceIter<'a> {
Empty,
Owned(std::slice::Iter<'a, u16>),
@@ -527,6 +599,13 @@ impl ParserSource {
Self::Shared { storage, range }
}
+ /// Moves the source text out of the whole source it shares with every other token (see `TextDetacher`).
+ pub(crate) fn detach(&mut self, detacher: &mut TextDetacher) {
+ if let Self::Shared { storage, range } = self {
+ detacher.source(storage, range);
+ }
+ }
+
pub(crate) fn covering(first: &Self, last: &Self) -> Option<Self> {
let Self::Shared {
storage: first_storage,
diff --git a/Libraries/LibWeb/Rust/src/css/parser/component_value.rs b/Libraries/LibWeb/Rust/src/css/parser/component_value.rs
index 15c65ba2e4c..19472fa09ab 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/component_value.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/component_value.rs
@@ -5,7 +5,7 @@
*/
use crate::css::css_tokenizer::{
- ParserSource, ParserString, ParserToken, ParserTokenKind, SmallParserTokenList, SourcePosition,
+ ParserSource, ParserString, ParserToken, ParserTokenKind, SmallParserTokenList, SourcePosition, TextDetacher,
};
use crate::css::serialize::{StringUnits, TextSink, serialize_a_string, serialize_an_identifier};
use smallvec::SmallVec;
@@ -36,6 +36,23 @@ pub(crate) struct ComponentValue {
}
impl ComponentValue {
+ /// Copies the value's text out of the sheet's source and token storage, for a value that outlives its parse.
+ /// Otherwise the value keeps the text of the whole sheet alive.
+ pub(crate) fn detach(&mut self, detacher: &mut TextDetacher) {
+ self.original_source_text.detach(detacher);
+ match &mut self.kind {
+ ComponentKind::Token(token) => token.detach(detacher),
+ ComponentKind::Function { name, values } => {
+ name.detach(detacher);
+ values.iter_mut().for_each(|value| value.detach(detacher));
+ }
+ ComponentKind::SimpleBlock { opening, values } => {
+ opening.detach(detacher);
+ values.iter_mut().for_each(|value| value.detach(detacher));
+ }
+ }
+ }
+
pub(crate) fn end_of_file() -> Self {
Self {
kind: ComponentKind::Token(ParserTokenKind::EndOfFile),
diff --git a/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs
index 7c61899f462..5fc05a4ba99 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs
@@ -6,7 +6,7 @@
use crate::css::css_enums::keyword_from_ascii_case_insensitive;
use crate::css::css_pixels::CssPixels;
-use crate::css::css_tokenizer::{ParserString, ParserTokenKind, TokenizerInput, tokenize_for_parser};
+use crate::css::css_tokenizer::{ParserString, ParserTokenKind, TextDetacher, TokenizerInput, tokenize_for_parser};
use crate::css::custom_properties::environment_variable_is_known;
use crate::css::ffi_support::FfiUtf16View;
use crate::css::parser::component_value::{
@@ -1524,13 +1524,117 @@ impl FfiQueryHandle {
}
}
-pub(crate) fn media_query_handle(query: MediaQuery) -> Arc<FfiQueryHandle> {
+impl MediaQuery {
+ fn detach(&mut self, detacher: &mut TextDetacher) {
+ if let Some(media_type) = &mut self.media_type {
+ media_type.detach(detacher);
+ }
+ if let Some(condition) = &mut self.condition {
+ condition.detach(detacher);
+ }
+ }
+}
+
+impl Expression {
+ /// Detaches every component value and string the expression keeps (see `ComponentValue::detach`).
+ fn detach(&mut self, detacher: &mut TextDetacher) {
+ match self {
+ Self::Not(expression)
+ | Self::InParens(expression)
+ | Self::StyleFunction(expression)
+ | Self::ScrollStateFunction(expression) => expression.detach(detacher),
+ Self::And(expressions) | Self::Or(expressions) => expressions
+ .iter_mut()
+ .for_each(|expression| expression.detach(detacher)),
+ Self::GeneralEnclosed { component, .. } => component.detach(detacher),
+ Self::GeneralEnclosedValues { components, .. } => {
+ components.iter_mut().for_each(|value| value.detach(detacher));
+ }
+ Self::QueryFeature(feature) => feature.detach(detacher),
+ Self::SupportsFeature(feature) => feature.detach(detacher),
+ Self::StyleFeature(feature) => feature.detach(detacher),
+ }
+ }
+}
+
+impl QueryFeature {
+ fn detach(&mut self, detacher: &mut TextDetacher) {
+ match self {
+ Self::Boolean { .. } => {}
+ Self::Plain { value, .. } => value.components.iter_mut().for_each(|value| value.detach(detacher)),
+ Self::Range { left, right, .. } => {
+ if let Some((value, _)) = left {
+ value.components.iter_mut().for_each(|value| value.detach(detacher));
+ }
+ if let Some((_, value)) = right {
+ value.components.iter_mut().for_each(|value| value.detach(detacher));
+ }
+ }
+ }
+ }
+}
+
+impl SupportsFeature {
+ fn detach(&mut self, detacher: &mut TextDetacher) {
+ match self {
+ Self::Declaration { components, .. } | Self::Selector { components, .. } => {
+ components.iter_mut().for_each(|value| value.detach(detacher));
+ }
+ Self::FontTech { name, .. }
+ | Self::FontFormat { name, .. }
+ | Self::AtRule { name, .. }
+ | Self::Env { name, .. } => name.detach(detacher),
+ }
+ }
+}
+
+impl StyleRangeValue {
+ fn detach(&mut self, detacher: &mut TextDetacher) {
+ match self {
+ Self::Property(name) => name.detach(detacher),
+ Self::Components(components) => components.iter_mut().for_each(|value| value.detach(detacher)),
+ }
+ }
+}
+
+impl StyleFeature {
+ fn detach(&mut self, detacher: &mut TextDetacher) {
+ match self {
+ Self::Boolean(name) => name.detach(detacher),
+ Self::Plain {
+ name,
+ value,
+ original_source,
+ original_value_source,
+ } => {
+ name.detach(detacher);
+ value.iter_mut().for_each(|value| value.detach(detacher));
+ original_source.detach(detacher);
+ original_value_source.detach(detacher);
+ }
+ Self::Range {
+ left, middle, right, ..
+ } => {
+ left.detach(detacher);
+ middle.detach(detacher);
+ if let Some((_, right)) = right {
+ right.detach(detacher);
+ }
+ }
+ }
+ }
+}
+
+// NB: A query outlives the parse of its sheet, so its handle detaches it from the sheet's text.
+pub(crate) fn media_query_handle(mut query: MediaQuery) -> Arc<FfiQueryHandle> {
+ TextDetacher::detach(|detacher| query.detach(detacher));
Arc::new(FfiQueryHandle {
tree: QueryTree::MediaQuery(query),
})
}
-pub(crate) fn expression_query_handle(expression: Expression, kind: QueryKind) -> Arc<FfiQueryHandle> {
+pub(crate) fn expression_query_handle(mut expression: Expression, kind: QueryKind) -> Arc<FfiQueryHandle> {
+ TextDetacher::detach(|detacher| expression.detach(detacher));
Arc::new(FfiQueryHandle {
tree: QueryTree::Expression { expression, kind },
})