Skip to content

Truly independent web browser

LibWeb: Do not copy rule names and unresolved values in the CSS parser

Andreas Kling3a9591fc5ae7
diff --git a/Libraries/LibWeb/Rust/src/css/parser/descriptor_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/descriptor_parser.rs
index 7c5b80edd05..8407b181ee3 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/descriptor_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/descriptor_parser.rs
@@ -459,7 +459,7 @@ fn parse_value_type(
}
Some(unresolved_value(
source,
- values.to_vec(),
+ values,
SubstitutionFunctionsPresence::default(),
))
}
@@ -524,7 +524,7 @@ pub(crate) fn parse_descriptor(
if !metadata.allow_arbitrary_substitution_functions {
return None;
}
- unresolved_value(source, values.to_vec(), presence)
+ unresolved_value(source, values, presence)
} else {
parse_with_metadata(&descriptor_context, &metadata, values, source)?
};
diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax.rs
index ad2a64f4e60..78532ff1235 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/syntax.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax.rs
@@ -418,7 +418,7 @@ pub(crate) fn parse_with_syntax_component_values(
collect_arbitrary_substitution_function_presence(values, &mut presence).ok()?;
return Some(match original_source {
- Some(source) => unresolved_value(source, values.to_vec(), presence),
+ Some(source) => unresolved_value(source, values, presence),
None => unresolved_value_from_component_values(values.to_vec(), presence),
});
}
diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
index aa60602d7cb..708c5750b42 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
@@ -141,6 +141,23 @@ pub(crate) enum Rule {
Qualified(QualifiedRule),
}
+/// What the rule around a rule is, for the diagnostics that name it.
+#[derive(Clone, Debug, PartialEq)]
+pub(crate) enum OuterRuleName {
+ Context(RuleContext),
+ /// The name of an at-rule, after its `@`.
+ At(Arc<[u16]>),
+}
+
+impl OuterRuleName {
+ fn name(&self) -> Option<ParserString> {
+ match self {
+ Self::Context(context) => context_rule_name(*context),
+ Self::At(name) => Some(at_rule_name(name)),
+ }
+ }
+}
+
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct AtRule {
pub name: ParserString,
@@ -148,7 +165,7 @@ pub(crate) struct AtRule {
pub has_block: bool,
nesting_parent: StyleNestingParent,
valid_in_context: bool,
- outer_rule_name: Option<ParserString>,
+ outer_rule_name: Option<OuterRuleName>,
}
#[derive(Clone, Debug, PartialEq)]
@@ -157,7 +174,7 @@ pub(crate) struct QualifiedRule {
pub prelude_is_selector: bool,
nesting_parent: StyleNestingParent,
valid_in_context: bool,
- outer_rule_name: Option<ParserString>,
+ outer_rule_name: Option<OuterRuleName>,
pub source_position: Option<SourcePosition>,
}
@@ -2359,12 +2376,7 @@ fn structural_validation_code(
|| !matches!(parsed_prelude, ParsedRulePrelude::Import(_)))
.then_some(FfiSyntaxDiagnosticCode::ImportInvalid),
(Rule::At(rule), ParsedRuleKind::Layer) => {
- if rule
- .outer_rule_name
- .as_deref()
- .is_some_and(|name| equals_ascii_case_insensitive(name, b"style"))
- && !rule.has_block
- {
+ if matches!(rule.outer_rule_name, Some(OuterRuleName::Context(RuleContext::Style))) && !rule.has_block {
Some(FfiSyntaxDiagnosticCode::InvalidRuleContext)
} else if rule.has_block && !matches!(parsed_prelude, ParsedRulePrelude::Name(_)) {
Some(FfiSyntaxDiagnosticCode::LayerInvalidName)
@@ -2495,11 +2507,12 @@ fn structural_validation_code(
}
}
-fn rule_outer_name(rule: &Rule) -> Option<&ParserString> {
+fn rule_outer_name(rule: &Rule) -> Option<ParserString> {
match rule {
Rule::At(rule) => rule.outer_rule_name.as_ref(),
Rule::Qualified(rule) => rule.outer_rule_name.as_ref(),
}
+ .and_then(OuterRuleName::name)
}
fn rule_diagnostic_name(rule: &Rule) -> ParserString {
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 944cf9a6e75..70067aa6f64 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/structure.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/structure.rs
@@ -15,8 +15,8 @@
//! https://drafts.csswg.org/css-syntax-3/#parsing
use super::{
- AtRule, Declaration, FfiSyntaxDiagnostic, FfiSyntaxDiagnosticCode, Nested, QualifiedRule, QualifiedRuleResult,
- Rule, RuleContext, at_rule_name, context_for_at_rule, context_rule_name, equals_ascii_case_insensitive,
+ AtRule, Declaration, FfiSyntaxDiagnostic, FfiSyntaxDiagnosticCode, Nested, OuterRuleName, QualifiedRule,
+ QualifiedRuleResult, Rule, RuleContext, context_for_at_rule, equals_ascii_case_insensitive,
is_accepted_position_try_property, is_font_feature_value_rule_name, is_margin_rule_name, starts_with_ascii,
};
use crate::css::css_tokenizer::{
@@ -585,7 +585,7 @@ impl Sink for NoSink {
pub(super) struct Core<'a, U: Unit> {
stream: Stream<'a, U>,
rule_context: Vec<RuleContext>,
- rule_names: Vec<Option<ParserString>>,
+ rule_names: Vec<OuterRuleName>,
lines: LineCursor,
/// The tokens that end the blocks and functions that are open, while a component value is skipped.
closers: Vec<Kind>,
@@ -593,7 +593,7 @@ pub(super) struct Core<'a, U: Unit> {
impl<'a, U: Unit> Core<'a, U> {
fn new(units: &'a [U], storage: &'a Arc<SourceStorage>, rule_context: Vec<RuleContext>) -> Self {
- let rule_names = rule_context.iter().copied().map(context_rule_name).collect();
+ let rule_names = rule_context.iter().copied().map(OuterRuleName::Context).collect();
Self {
stream: Stream::new(units, storage, 0..usize::MAX),
rule_context,
@@ -701,7 +701,7 @@ impl<'a, U: Unit> Core<'a, U> {
has_block: false,
nesting_parent: self.style_nesting_parent(),
valid_in_context: false,
- outer_rule_name: self.rule_names.last().cloned().flatten(),
+ outer_rule_name: self.rule_names.last().cloned(),
};
loop {
self.stream.push_gap(&mut rule.prelude);
@@ -717,7 +717,7 @@ impl<'a, U: Unit> Core<'a, U> {
rule.has_block = true;
rule.valid_in_context = self.at_rule_is_valid(&rule);
let context = context_for_at_rule(rule.name.as_ref());
- let rule_name = Some(at_rule_name(rule.name.as_ref()));
+ let rule_name = OuterRuleName::At(rule.name.as_ref().into());
sink.begin_rule(Rule::At(rule));
self.rule_context.push(context);
self.rule_names.push(rule_name);
@@ -789,11 +789,11 @@ impl<'a, U: Unit> Core<'a, U> {
prelude_is_selector: qualified_context == RuleContext::Style,
nesting_parent: self.style_nesting_parent(),
valid_in_context: self.qualified_rule_is_valid(),
- outer_rule_name: self.rule_names.last().cloned().flatten(),
+ outer_rule_name: self.rule_names.last().cloned(),
source_position,
}));
self.rule_context.push(qualified_context);
- self.rule_names.push(context_rule_name(qualified_context));
+ self.rule_names.push(OuterRuleName::Context(qualified_context));
self.consume_block(sink);
self.rule_names.pop();
self.rule_context.pop();
diff --git a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
index 455f90bcac3..522d1b78344 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
@@ -28,8 +28,8 @@ use crate::css::parser::arbitrary_substitution::{
use crate::css::parser::calc_parser::{CalcParseError, parse_a_calc_function_node};
use crate::css::parser::color_parser::{is_color_function_name, parse_color_value};
use crate::css::parser::component_value::{
- ComponentKind, ComponentValue, ComponentValueList, consume_a_counted_list_of_component_values,
- consume_a_list_of_component_values, consume_a_small_list_of_component_values,
+ ComponentKind, ComponentValue, consume_a_counted_list_of_component_values, consume_a_list_of_component_values,
+ consume_a_small_list_of_component_values,
};
use crate::css::parser::fonts_parser::parse_font_property;
use crate::css::parser::grid_parser::parse_grid_property;
@@ -5922,18 +5922,17 @@ pub(crate) fn unresolved_value_from_string(
presence: SubstitutionFunctionsPresence,
) -> StyleValueData {
let values = consume_a_counted_list_of_component_values(tokenize_for_parser(unresolved_source)).unwrap_or_default();
- unresolved_value(unresolved_source, values, presence)
+ unresolved_value(unresolved_source, &values, presence)
}
/// An unresolved value of `values`, whose source text `unresolved_source` is, and which it tokenizes again when its
/// components are read.
pub(crate) fn unresolved_value(
unresolved_source: &[u16],
- values: impl Into<ComponentValueList>,
+ values: &[ComponentValue],
presence: SubstitutionFunctionsPresence,
) -> StyleValueData {
- let values = values.into();
- unresolved_value_with_components(unresolved_source, UnresolvedValueComponents::OfText(&values), presence)
+ unresolved_value_with_components(unresolved_source, UnresolvedValueComponents::OfText(values), presence)
}
/// An unresolved value whose source text is `unresolved_source`, made of `components`.
@@ -6017,7 +6016,7 @@ fn parse_css_value_after_substitution_scan(
)
} else {
serialization = Vec::new();
- unresolved_value(unresolved_source, values.to_vec(), substitution_presence)
+ unresolved_value(unresolved_source, values, substitution_presence)
};
if let StyleValueData::Unresolved(unresolved_payload) = &mut unresolved {
unresolved_payload.payload_mut().contains_attr_tainted_values = context.contains_attr_tainted_values;
diff --git a/Libraries/LibWeb/Rust/src/css/style_value.rs b/Libraries/LibWeb/Rust/src/css/style_value.rs
index a17c2591d96..436d8aea9ef 100644
--- a/Libraries/LibWeb/Rust/src/css/style_value.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_value.rs
@@ -4663,7 +4663,7 @@ mod equality_tests {
fn unresolved(source: &str) -> StyleValueData {
let values = consume_a_list_of_component_values(tokenize_for_parser(source.as_bytes())).unwrap();
let serialized = serialize_component_values_to_utf16(&values, ComponentSerializationMode::Retokenize);
- unresolved_value(&serialized, values, Default::default())
+ unresolved_value(&serialized, &values, Default::default())
}
#[test]