From c3437bccb3db9b97fea5bf7d14d0277c763f1fc5 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 3 Dec 2021 20:00:31 +0000 Subject: [PATCH] LibWeb: Handle dependency cycles in CSS var()s :^) We now detect situations like this, where variables infinitely recur, without crashing: ```css div { --a: var(--b); --b: var(--a); background: var(--a); } p { --foo: var(--foo); background: var(--foo); } ``` --- .../Libraries/LibWeb/CSS/StyleComputer.cpp | 67 ++++++++++++++++--- Userland/Libraries/LibWeb/CSS/StyleComputer.h | 22 +++++- 2 files changed, 80 insertions(+), 9 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index e8436c87bdc..a8af89d46f3 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -7,6 +7,7 @@ */ #include +#include #include #include #include @@ -452,14 +453,12 @@ struct MatchingDeclarations { Vector author_rules; }; -bool StyleComputer::expand_unresolved_values(DOM::Element& element, Vector const& source, Vector& dest, size_t source_start_index) const +bool StyleComputer::expand_unresolved_values(DOM::Element& element, StringView property_name, HashMap>& dependencies, Vector const& source, Vector& dest, size_t source_start_index) const { // FIXME: Do this better! // We build a copy of the tree of StyleComponentValueRules, with all var()s replaced with their contents. // This is a very naive solution, and we could do better if the CSS Parser could accept tokens one at a time. - // FIXME: Handle dependency cycles. https://www.w3.org/TR/css-variables-1/#cycles - // Arbitrary large value chosen to avoid the billion-laughs attack. // https://www.w3.org/TR/css-variables-1/#long-variables const size_t MAX_VALUE_COUNT = 16384; @@ -475,6 +474,16 @@ bool StyleComputer::expand_unresolved_values(DOM::Element& element, Vector