Skip to content

Commit

Permalink
LibWeb/CSS: Rewrite CSS Parser core methods according to new spec
Browse files Browse the repository at this point in the history
CSS Syntax 3 (https://drafts.csswg.org/css-syntax) has changed
significantly since we implemented it a couple of years ago. Just about
every parsing algorithm has been rewritten in terms of the new token
stream concept, and to support nested styles. As all of those
algorithms call into each other, this is an unfortunately chonky diff.

As part of this, the transitory types (Declaration, Function, AtRule...)
have been rewritten. That's both because we have new requirements of
what they should be and contain, and also because the spec asks us to
create and then gradually modify them in place, which is easier if they
are plain structs.

(cherry picked from commit e0be17e4fbf1870f35614d0cde8f63e72f78bd16;
amended to tweak test expectation due to serenity not yet having
LadybirdBrowser/ladybird#1603)
  • Loading branch information
AtkinsSJ authored and nico committed Nov 18, 2024
1 parent 059539d commit fe9ab2c
Show file tree
Hide file tree
Showing 25 changed files with 1,110 additions and 1,262 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ static Optional<RoundingStrategy> parse_rounding_strategy(Vector<ComponentValue>
OwnPtr<CalculationNode> Parser::parse_math_function(PropertyID property_id, Function const& function)
{
TokenStream stream { function.values() };
TokenStream stream { function.value };
auto arguments = parse_a_comma_separated_list_of_component_values(stream);
)~~~");

Expand All @@ -129,7 +129,7 @@ OwnPtr<CalculationNode> Parser::parse_math_function(PropertyID property_id, Func
auto function_generator = generator.fork();
function_generator.set("name:lowercase", name);
function_generator.set("name:titlecase", title_casify(name));
function_generator.appendln(" if (function.name().equals_ignoring_ascii_case(\"@name:lowercase@\"sv)) {");
function_generator.appendln(" if (function.name.equals_ignoring_ascii_case(\"@name:lowercase@\"sv)) {");
if (function_data.get_bool("is-variadic"sv).value_or(false)) {
// Variadic function
function_generator.append(R"~~~(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ source_set("Parser") {
configs += [ "//Userland/Libraries/LibWeb:configs" ]
deps = [ "//Userland/Libraries/LibWeb:all_generated" ]
sources = [
"Block.cpp",
"ComponentValue.cpp",
"Declaration.cpp",
"DeclarationOrAtRule.cpp",
"Function.cpp",
"GradientParsing.cpp",
"Helpers.cpp",
"MediaParsing.cpp",
"Parser.cpp",
"ParsingContext.cpp",
"Rule.cpp",
"SelectorParsing.cpp",
"Token.cpp",
"Tokenizer.cpp",
"Types.cpp",
]
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Color is: "rgb(0, 128, 0)", should be "rgb(0, 128, 0)"
This text should look green Color is: "rgb(0, 128, 0)", should be "rgb(0, 128, 0)"
6 changes: 1 addition & 5 deletions Userland/Libraries/LibWeb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,16 @@ set(SOURCES
CSS/MediaQuery.cpp
CSS/MediaQueryList.cpp
CSS/MediaQueryListEvent.cpp
CSS/Parser/Block.cpp
CSS/Parser/ComponentValue.cpp
CSS/Parser/Declaration.cpp
CSS/Parser/DeclarationOrAtRule.cpp
CSS/Parser/Function.cpp
CSS/Parser/GradientParsing.cpp
CSS/Parser/Helpers.cpp
CSS/Parser/MediaParsing.cpp
CSS/Parser/Parser.cpp
CSS/Parser/ParsingContext.cpp
CSS/Parser/Rule.cpp
CSS/Parser/SelectorParsing.cpp
CSS/Parser/Token.cpp
CSS/Parser/Tokenizer.cpp
CSS/Parser/Types.cpp
CSS/ParsedFontFace.cpp
CSS/PercentageOr.cpp
CSS/PreferredColorScheme.cpp
Expand Down
31 changes: 0 additions & 31 deletions Userland/Libraries/LibWeb/CSS/Parser/Block.cpp

This file was deleted.

42 changes: 0 additions & 42 deletions Userland/Libraries/LibWeb/CSS/Parser/Block.h

This file was deleted.

20 changes: 9 additions & 11 deletions Userland/Libraries/LibWeb/CSS/Parser/ComponentValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <LibWeb/CSS/Parser/Block.h>
#include <LibWeb/CSS/Parser/ComponentValue.h>
#include <LibWeb/CSS/Parser/Function.h>

namespace Web::CSS::Parser {

ComponentValue::ComponentValue(Token token)
: m_value(token)
{
}
ComponentValue::ComponentValue(NonnullRefPtr<Function> function)
ComponentValue::ComponentValue(Function&& function)
: m_value(function)
{
}
ComponentValue::ComponentValue(NonnullRefPtr<Block> block)
ComponentValue::ComponentValue(SimpleBlock&& block)
: m_value(block)
{
}
Expand All @@ -28,7 +26,7 @@ ComponentValue::~ComponentValue() = default;

bool ComponentValue::is_function(StringView name) const
{
return is_function() && function().name().equals_ignoring_ascii_case(name);
return is_function() && function().name.equals_ignoring_ascii_case(name);
}

bool ComponentValue::is_ident(StringView ident) const
Expand All @@ -40,8 +38,8 @@ String ComponentValue::to_string() const
{
return m_value.visit(
[](Token const& token) { return token.to_string(); },
[](NonnullRefPtr<Block> const& block) { return block->to_string(); },
[](NonnullRefPtr<Function> const& function) { return function->to_string(); });
[](SimpleBlock const& block) { return block.to_string(); },
[](Function const& function) { return function.to_string(); });
}

String ComponentValue::to_debug_string() const
Expand All @@ -50,11 +48,11 @@ String ComponentValue::to_debug_string() const
[](Token const& token) {
return MUST(String::formatted("Token: {}", token.to_debug_string()));
},
[](NonnullRefPtr<Block> const& block) {
return MUST(String::formatted("Block: {}", block->to_string()));
[](SimpleBlock const& block) {
return MUST(String::formatted("Block: {}", block.to_string()));
},
[](NonnullRefPtr<Function> const& function) {
return MUST(String::formatted("Function: {}", function->to_string()));
[](Function const& function) {
return MUST(String::formatted("Function: {}", function.to_string()));
});
}

Expand Down
20 changes: 11 additions & 9 deletions Userland/Libraries/LibWeb/CSS/Parser/ComponentValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,26 @@
#include <AK/NonnullRefPtr.h>
#include <AK/RefPtr.h>
#include <LibWeb/CSS/Parser/Token.h>
#include <LibWeb/Forward.h>
#include <LibWeb/CSS/Parser/Types.h>

namespace Web::CSS::Parser {

// https://www.w3.org/TR/css-syntax-3/#component-value
// https://drafts.csswg.org/css-syntax/#component-value
class ComponentValue {
public:
ComponentValue(Token);
explicit ComponentValue(NonnullRefPtr<Function>);
explicit ComponentValue(NonnullRefPtr<Block>);
explicit ComponentValue(Function&&);
explicit ComponentValue(SimpleBlock&&);
~ComponentValue();

bool is_block() const { return m_value.has<NonnullRefPtr<Block>>(); }
Block& block() const { return m_value.get<NonnullRefPtr<Block>>(); }
bool is_block() const { return m_value.has<SimpleBlock>(); }
SimpleBlock& block() { return m_value.get<SimpleBlock>(); }
SimpleBlock const& block() const { return m_value.get<SimpleBlock>(); }

bool is_function() const { return m_value.has<NonnullRefPtr<Function>>(); }
bool is_function() const { return m_value.has<Function>(); }
bool is_function(StringView name) const;
Function& function() const { return m_value.get<NonnullRefPtr<Function>>(); }
Function& function() { return m_value.get<Function>(); }
Function const& function() const { return m_value.get<Function>(); }

bool is_token() const { return m_value.has<Token>(); }
bool is(Token::Type type) const { return is_token() && token().is(type); }
Expand All @@ -41,7 +43,7 @@ class ComponentValue {
String to_debug_string() const;

private:
Variant<Token, NonnullRefPtr<Function>, NonnullRefPtr<Block>> m_value;
Variant<Token, Function, SimpleBlock> m_value;
};
}

Expand Down
36 changes: 0 additions & 36 deletions Userland/Libraries/LibWeb/CSS/Parser/Declaration.cpp

This file was deleted.

34 changes: 0 additions & 34 deletions Userland/Libraries/LibWeb/CSS/Parser/Declaration.h

This file was deleted.

27 changes: 0 additions & 27 deletions Userland/Libraries/LibWeb/CSS/Parser/DeclarationOrAtRule.cpp

This file was deleted.

47 changes: 0 additions & 47 deletions Userland/Libraries/LibWeb/CSS/Parser/DeclarationOrAtRule.h

This file was deleted.

Loading

0 comments on commit fe9ab2c

Please sign in to comment.