Skip to content

Commit a17f99d

Browse files
committed
Bug 2024601 - patch 1 - Parsing support for substitution functions as style query expression values. r=firefox-style-system-reviewers,emilio
This is sufficient to make the existing parsing test pass, but the functions don't actually work at evaluation time. The following patch will add some query evaluation tests to expose this. Differential Revision: https://phabricator.services.mozilla.com/D290901
1 parent 9b39113 commit a17f99d

3 files changed

Lines changed: 33 additions & 8 deletions

File tree

servo/components/style/queries/feature_expression.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use super::feature::{Evaluator, QueryFeatureDescription};
99
use super::feature::{FeatureFlags, KeywordDiscriminant};
1010
use crate::context::QuirksMode;
11+
use crate::custom_properties::VariableValue as CustomVariableValue;
1112
use crate::derives::*;
1213
use crate::parser::{Parse, ParserContext};
1314
use crate::properties::CSSWideKeyword;
@@ -679,8 +680,12 @@ pub enum QueryExpressionValue {
679680
/// A custom property name.
680681
Custom(DashedIdent),
681682
/// A simple var(...) reference without a default (equivalent to a bare Custom ident,
682-
/// but will serialize with the `var()` wrapper)
683+
/// but will serialize with the `var()` wrapper).
683684
Var(DashedIdent),
685+
/// An arbitrary substitution function (var(), attr(), env()), stored as a string
686+
/// for later evaluation. We store this as a custom-property value to make it easy
687+
/// to resolve later.
688+
Function(Box<CustomVariableValue>),
684689
}
685690

686691
impl QueryExpressionValue {
@@ -709,6 +714,7 @@ impl QueryExpressionValue {
709714
v.to_css(dest)?;
710715
dest.write_char(')')
711716
},
717+
QueryExpressionValue::Function(ref f) => f.to_css(dest),
712718
QueryExpressionValue::Enumerated(value) => match for_expr
713719
.expect("caller should have passed for_expr")
714720
.feature()
@@ -793,17 +799,38 @@ impl QueryExpressionValue {
793799
if let Ok(keyword) = input.try_parse(|i| CSSWideKeyword::parse(i)) {
794800
return Ok(Self::Keyword(keyword));
795801
}
802+
input.skip_whitespace();
803+
let start = input.position();
796804
if let Ok(Token::Function(ref name)) = input.next() {
797-
// Here, we only handle simple `var(--foo)` references when used as individual
798-
// query expression values. More complex usages such as `var(...)` with default,
799-
// or `var(...)` used within `calc(...)` expressions, will be substituted and
800-
// resolved at query evaluation time.
805+
// Helper to parse the function arg and store the complete expression (function
806+
// name and parenthesized argument) into a CustomVariableValue.
807+
let parse_func =
808+
|input: &mut Parser<'i, 't>| -> Result<CustomVariableValue, ParseError<'i>> {
809+
input.parse_nested_block(|i| i.expect_no_error_token().map_err(Into::into))?;
810+
let mut input = ParserInput::new(input.slice_from(start));
811+
CustomVariableValue::parse(
812+
&mut Parser::new(&mut input),
813+
Some(&context.namespaces.prefixes),
814+
context.url_data,
815+
)
816+
};
817+
801818
if name.eq_ignore_ascii_case("var") {
819+
// For simple `var(--foo)` references used as individual query-expression values,
820+
// we can store as the Var() variant and just look up the custom property at
821+
// evaluation time.
802822
if let Ok(ident) =
803823
input.try_parse(|i| i.parse_nested_block(|i| DashedIdent::parse(context, i)))
804824
{
805825
return Ok(Self::Var(ident));
806826
}
827+
// Otherwise, we store the entire function to be resolved later via
828+
// custom_properties::substitute(), which will also handle fallbacks
829+
// if necessary.
830+
return Ok(Self::Function(Box::new(parse_func(input)?)));
831+
}
832+
if static_prefs::pref!("layout.css.attr.enabled") && name.eq_ignore_ascii_case("attr") {
833+
return Ok(Self::Function(Box::new(parse_func(input)?)));
807834
}
808835
}
809836
Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
prefs: [layout.css.attr.enabled:true]

testing/web-platform/meta/css/css-conditional/container-queries/at-container-style-parsing.html.ini

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)