From d67e817d8e684c06c3dd04b5fddef90d6f12c0ab Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Tue, 12 Apr 2022 16:44:02 +0100 Subject: [PATCH] LibWeb: Break friendship between CSS Function and Parser Again, this means deviating from the spec by creating a complete Function in one go instead of creating it empty and then poking at its internals. --- Userland/Libraries/LibWeb/CSS/Parser/Function.cpp | 5 ----- Userland/Libraries/LibWeb/CSS/Parser/Function.h | 11 +++++++---- Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp | 10 ++++++---- Userland/Libraries/LibWeb/CSS/StyleComputer.cpp | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Function.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Function.cpp index f1d872ebbaa..0c15b077159 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Function.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Function.cpp @@ -10,11 +10,6 @@ namespace Web::CSS::Parser { -Function::Function(FlyString name) - : m_name(move(name)) -{ -} - Function::Function(FlyString name, Vector&& values) : m_name(move(name)) , m_values(move(values)) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Function.h b/Userland/Libraries/LibWeb/CSS/Parser/Function.h index 5979c461f7f..e90c23661a0 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Function.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Function.h @@ -17,11 +17,12 @@ namespace Web::CSS::Parser { class Function : public RefCounted { - friend class Parser; - public: - explicit Function(FlyString name); - Function(FlyString name, Vector&& values); + static NonnullRefPtr create(FlyString name, Vector&& values) + { + return adopt_ref(*new Function(move(name), move(values))); + } + ~Function(); StringView name() const { return m_name; } @@ -30,6 +31,8 @@ public: String to_string() const; private: + Function(FlyString name, Vector&& values); + FlyString m_name; Vector m_values; }; diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 236f080897b..32a58825d22 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -1878,7 +1878,9 @@ NonnullRefPtr Parser::consume_a_function(TokenStream& tokens) // Create a function with its name equal to the value of the current input token // and with its value initially set to an empty list. - auto function = make_ref_counted(((Token)name_ident).function()); + // NOTE: We create the Function fully initialized when we return it instead. + FlyString function_name = ((Token)name_ident).function(); + Vector function_values; // Repeatedly consume the next input token and process it as follows: for (;;) { @@ -1887,14 +1889,14 @@ NonnullRefPtr Parser::consume_a_function(TokenStream& tokens) // <)-token> if (token.is(Token::Type::CloseParen)) { // Return the function. - return function; + return Function::create(move(function_name), move(function_values)); } // if (token.is(Token::Type::EndOfFile)) { // This is a parse error. Return the function. log_parse_error(); - return function; + return Function::create(move(function_name), move(function_values)); } // anything else @@ -1903,7 +1905,7 @@ NonnullRefPtr Parser::consume_a_function(TokenStream& tokens) tokens.reconsume_current_input_token(); // Consume a component value and append the returned value to the function’s value. - function->m_values.append(consume_a_component_value(tokens)); + function_values.append(consume_a_component_value(tokens)); } } } diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index 590d38032d6..61228e0b706 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -622,7 +622,7 @@ bool StyleComputer::expand_unresolved_values(DOM::Element& element, StringView p Vector function_values; if (!expand_unresolved_values(element, property_name, dependencies, source_function.values(), function_values, 0)) return false; - NonnullRefPtr function = adopt_ref(*new Parser::Function(source_function.name(), move(function_values))); + NonnullRefPtr function = Parser::Function::create(source_function.name(), move(function_values)); dest.empend(function); continue; }