Skip to content

Truly independent web browser

LibWeb: Parse substituted property values from component values

Callum Law9646d9a06080
diff --git a/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs b/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs
index 44af370d00c..621147f07e2 100644
--- a/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs
+++ b/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs
@@ -20,8 +20,9 @@ use std::hash::Hasher;
use std::sync::Arc;
use crate::css::custom_properties::CustomPropertyStore;
+use crate::css::parser::component_value::ComponentValue;
use crate::css::parser::value_parser::{
- FfiValueParsingContext, FfiValueParsingContextKind, ParseContext, ParseOutcome, parse_css_value_from_source,
+ FfiValueParsingContext, FfiValueParsingContextKind, ParseContext, ParseOutcome, parse_css_value_with_source,
};
use crate::css::property_metadata::LAST_LONGHAND_PROPERTY_ID;
use crate::css::retained_fly_string::RetainedUtf16FlyString;
@@ -345,6 +346,7 @@ pub(crate) fn parse_substituted_value(
base_context: &ParseContext,
property_id: u16,
source: &[u16],
+ values: &[ComponentValue],
contains_attr_tainted_values: bool,
) -> RetainedStyleValueData {
crate::css::ffi_stats::bump(crate::css::ffi_stats::FfiOp::SubstitutionParse);
@@ -360,7 +362,7 @@ pub(crate) fn parse_substituted_value(
context.contains_attr_tainted_values = contains_attr_tainted_values;
context.value_contexts = ak::Span::new(std::slice::from_ref(&value_context));
context.random_function_index = Some(&random_function_index);
- match parse_css_value_from_source(&context, property_id, source) {
+ match parse_css_value_with_source(&context, property_id, values, source) {
ParseOutcome::Parsed(value) => RetainedStyleValueData::from_arc(value),
ParseOutcome::Invalid | ParseOutcome::NotHandled => {
RetainedStyleValueData::from_owned(StyleValueData::GuaranteedInvalid)
diff --git a/Libraries/LibWeb/Rust/src/css/custom_properties.rs b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
index c85b959d477..094e497a3ed 100644
--- a/Libraries/LibWeb/Rust/src/css/custom_properties.rs
+++ b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
@@ -1292,6 +1292,7 @@ fn compute_element_custom_property(
property_context.parse_context.ok_or(())?,
crate::css::property_metadata::property_id::CUSTOM,
&serialize_tokens(&tokens),
+ &tokens,
property_context.contains_attr_tainted_values,
),
TokenResolution::Invalid | TokenResolution::Cyclic => {
diff --git a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
index a4bac571c26..8eef36cf07d 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
@@ -6257,20 +6257,12 @@ pub(crate) fn parse_css_value(context: &ParseContext, property_id: u16, values:
parse_css_value_with_source(context, property_id, values, &source)
}
-fn component_values_from_source<'a>(
+pub(crate) fn component_values_from_source<'a>(
source: impl Into<TokenizerInput<'a>>,
) -> Result<smallvec::SmallVec<[ComponentValue; 8]>, ()> {
consume_a_small_list_of_component_values(tokenize_for_parser_without_source(source))
}
-/// Parses a UTF-16 property value whose component-value source is already serialized.
-pub(crate) fn parse_css_value_from_source(context: &ParseContext, property_id: u16, source: &[u16]) -> ParseOutcome {
- match component_values_from_source(source) {
- Ok(values) => parse_css_value_with_source(context, property_id, &values, source),
- Err(()) => ParseOutcome::NotHandled,
- }
-}
-
/// The result category returned through the value-parser FFI.
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -6574,14 +6566,12 @@ mod tests {
fn parses_a_serialized_substituted_value() {
let mut parse_context = context();
parse_context.is_substituted_value = true;
- let valid = utf16("rgb(1 2 3)");
- let trailing = utf16("rgb(1 2 3) trailing");
assert!(matches!(
- parse_css_value_from_source(&parse_context, property_id::COLOR, &valid),
+ parse_with_context(&parse_context, property_id::COLOR, "rgb(1 2 3)"),
ParseOutcome::Parsed(_)
));
assert!(matches!(
- parse_css_value_from_source(&parse_context, property_id::COLOR, &trailing),
+ parse_with_context(&parse_context, property_id::COLOR, "rgb(1 2 3) trailing"),
ParseOutcome::Invalid
));
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs b/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
index d3b600d6585..c399b9c2d68 100644
--- a/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
@@ -28,6 +28,7 @@ use crate::css::custom_properties::{
VarResolutionInputs, prepare_var_resolution_environment,
};
use crate::css::parser::component_value::{ComponentKind, ComponentValue};
+use crate::css::parser::value_parser::component_values_from_source;
use crate::css::rule::CompiledFunction;
use crate::css::style_value::{
RetainedStyleValueData, StyleValueData, custom_property_references, utf16_equals_ascii_case_insensitive,
@@ -2310,7 +2311,14 @@ fn substitute_against_stores(
NativeVarResolution::Resolved {
source,
contains_attr_tainted_values,
- } => parse_substituted_value(&parse_context, property, &source, contains_attr_tainted_values),
+ } => {
+ // FIXME: Preserve the resolved components rather than retokenizing their normalized serialization.
+ let Ok(values) = component_values_from_source(source.as_slice()) else {
+ crate::css::ffi_stats::bump(crate::css::ffi_stats::FfiOp::SubstitutionParse);
+ return invalid();
+ };
+ parse_substituted_value(&parse_context, property, &source, &values, contains_attr_tainted_values)
+ }
NativeVarResolution::Invalid | NativeVarResolution::NotHandled => invalid(),
}
}
diff --git a/Tests/LibWeb/Text/expected/css/custom-property-substitution-numeric-token-type.txt b/Tests/LibWeb/Text/expected/css/custom-property-substitution-numeric-token-type.txt
new file mode 100644
index 00000000000..7f8f011eb73
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/custom-property-substitution-numeric-token-type.txt
@@ -0,0 +1 @@
+7
diff --git a/Tests/LibWeb/Text/input/css/custom-property-substitution-numeric-token-type.html b/Tests/LibWeb/Text/input/css/custom-property-substitution-numeric-token-type.html
new file mode 100644
index 00000000000..b11ba47ec4e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/custom-property-substitution-numeric-token-type.html
@@ -0,0 +1,10 @@
+<!doctype html>
+<style>
+ @function --integer(--value <integer>: 7) { result: var(--value); }
+ #target { --raw: var(--missing, 1.0); z-index: --integer(var(--raw)); }
+</style>
+<div id="target"></div>
+<script src="../include.js"></script>
+<script>
+ test(() => println(getComputedStyle(target).zIndex));
+</script>