Skip to content

Truly independent web browser

LibWeb: Isolate guards when computing inherited custom properties

Callum Lawc88a3e9d2ec7
diff --git a/Libraries/LibWeb/Rust/src/css/custom_properties.rs b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
index cbeaae5833a..3145632aa92 100644
--- a/Libraries/LibWeb/Rust/src/css/custom_properties.rs
+++ b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
@@ -1300,8 +1300,21 @@ fn compute_element_custom_property(
let (tokens, includes_substitution, tainted) = cached_tokens_for_custom_property_value(entry.value.data(), context);
let (value, is_cyclic) = if includes_substitution {
+ let guarded_contexts = GuardedSubstitutionContexts {
+ contexts: Rc::new(RefCell::new(
+ context
+ .guarded_contexts
+ .contexts
+ .borrow()
+ .iter()
+ .take_while(|context| !matches!(&context.dependency, SubstitutionContextDependency::Function(_)))
+ .cloned()
+ .collect(),
+ )),
+ };
+
let mut property_context = ASFResolutionContext {
- guarded_contexts: context.guarded_contexts.clone(),
+ guarded_contexts,
contains_attr_tainted_values: tainted,
in_url_function: false,
uses_attr_tainted_url: false,
diff --git a/Tests/LibWeb/Text/expected/css/style-engine/custom-property-function-inherited-guards.txt b/Tests/LibWeb/Text/expected/css/style-engine/custom-property-function-inherited-guards.txt
new file mode 100644
index 00000000000..3c549c66f91
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/style-engine/custom-property-function-inherited-guards.txt
@@ -0,0 +1,10 @@
+property-cycle: GIV
+inherited-property: RESOLVED
+inherited-property-reversed: RESOLVED
+inherited-function-local-property: RESOLVED
+inherited-function-local-property-reversed: RESOLVED
+self-reference: GIV
+inherited-attribute: RESOLVED
+inherited-attribute-reversed: RESOLVED
+attribute-property-cycle: GIV
+attribute-recursion: GIV
diff --git a/Tests/LibWeb/Text/input/css/style-engine/custom-property-function-inherited-guards.html b/Tests/LibWeb/Text/input/css/style-engine/custom-property-function-inherited-guards.html
new file mode 100644
index 00000000000..338a1629871
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/style-engine/custom-property-function-inherited-guards.html
@@ -0,0 +1,129 @@
+<!doctype html>
+<html>
+ <style>
+ @function --read-first() {
+ result: var(--first-value);
+ }
+
+ #property-cycle {
+ --first-value: var(--second-value);
+ --second-value: --read-first();
+
+ /* This should resolve to GIV since there is a cycle of --first-value and --second-value */
+ --value: var(--second-value, GIV);
+ }
+
+ @function --maybe-read-first(--condition: <integer>) {
+ result: if(style(--condition: 1): var(--first-value); else: RESOLVED);
+ }
+
+ #inherited-property {
+ --first-value: --maybe-read-first(0);
+ --second-value: --maybe-read-first(1);
+
+ /* This should NOT resolve to GIV since there is no cycle, the --first-value read within --maybe-read-first(1) reads the already computed value */
+ --value: var(--second-value, GIV);
+ }
+
+ #inherited-property-reversed {
+ --second-value: --maybe-read-first(1);
+ --first-value: --maybe-read-first(0);
+
+ /* This should NOT resolve to GIV for the same reason as above - regardless of actual computation order */
+ --value: var(--second-value, GIV);
+ }
+
+ @function --outer() {
+ --first-value: --maybe-read-first(0);
+ --second-value: --maybe-read-first(1);
+
+ /* This should also NOT resolve to GIV for the same reason as above */
+ result: var(--second-value, GIV);
+ }
+
+ #inherited-function-local-property {
+ --value: --outer();
+ }
+
+ @function --outer-reversed() {
+ --second-value: --maybe-read-first(1);
+ --first-value: --maybe-read-first(0);
+
+ /* This should also NOT resolve to GIV for the same reason as above */
+ result: var(--second-value, GIV);
+ }
+
+ #inherited-function-local-property-reversed {
+ --value: --outer-reversed();
+ }
+
+ #self-reference {
+ --first-value: --maybe-read-first(1);
+
+ /* This should resolve to GIV due to --first-value referring to itself */
+ --value: var(--first-value, GIV);
+ }
+
+ @function --read-attribute(--condition) {
+ result: attr(data-value type(*));
+ }
+
+ #inherited-attribute {
+ --first-value: --read-attribute(no);
+ --second-value: --read-attribute(yes);
+ --value: var(--second-value, GIV);
+ }
+
+ #inherited-attribute-reversed {
+ --second-value: --read-attribute(yes);
+ --first-value: --read-attribute(no);
+ --value: var(--second-value, GIV);
+ }
+
+ #attribute-property-cycle {
+ --first-value: attr(data-value type(*));
+ --second-value: var(--first-value);
+ --value: var(--second-value, GIV);
+ }
+
+ #attribute-recursion {
+ --first-value: attr(data-value type(*));
+ --value: var(--first-value, GIV);
+ }
+ </style>
+ <div id="property-cycle"></div>
+ <div id="inherited-property"></div>
+ <div id="inherited-property-reversed"></div>
+ <div id="inherited-function-local-property"></div>
+ <div id="inherited-function-local-property-reversed"></div>
+ <div id="self-reference"></div>
+ <!-- Computing --first-value must not retain the attr() guard from --read-attribute(yes). -->
+ <div id="inherited-attribute" data-value="if(style(--condition: no): RESOLVED; else: var(--first-value))"></div>
+ <div
+ id="inherited-attribute-reversed"
+ data-value="if(style(--condition: no): RESOLVED; else: var(--first-value))"
+ ></div>
+ <div id="attribute-property-cycle" data-value="var(--second-value)"></div>
+ <div id="attribute-recursion" data-value="attr(data-value type(*))"></div>
+ <script src="../../include.js"></script>
+ <script>
+ test(() => {
+ let cases = [
+ "property-cycle",
+ "inherited-property",
+ "inherited-property-reversed",
+ "inherited-function-local-property",
+ "inherited-function-local-property-reversed",
+ "self-reference",
+ "inherited-attribute",
+ "inherited-attribute-reversed",
+ "attribute-property-cycle",
+ "attribute-recursion",
+ ];
+
+ for (let id of cases) {
+ println(id + ": " + getComputedStyle(document.getElementById(id)).getPropertyValue("--value"));
+ }
+ });
+ </script>
+</html>