diff --git a/Libraries/LibWeb/Layout/Node.cpp b/Libraries/LibWeb/Layout/Node.cpp index 275fcb37d5a..d9f98d80853 100644 --- a/Libraries/LibWeb/Layout/Node.cpp +++ b/Libraries/LibWeb/Layout/Node.cpp @@ -1155,13 +1155,13 @@ DOM::Node* Node::dom_node() DOM::Element const* Node::pseudo_element_generator() const { - VERIFY(m_generated_for != GeneratedFor::NotGenerated); + VERIFY(m_generated_for.has_value()); return m_pseudo_element_generator.ptr(); } DOM::Element* Node::pseudo_element_generator() { - VERIFY(m_generated_for != GeneratedFor::NotGenerated); + VERIFY(m_generated_for.has_value()); return m_pseudo_element_generator.ptr(); } diff --git a/Libraries/LibWeb/Layout/Node.h b/Libraries/LibWeb/Layout/Node.h index 74cf35d1656..fe535e2f87f 100644 --- a/Libraries/LibWeb/Layout/Node.h +++ b/Libraries/LibWeb/Layout/Node.h @@ -45,15 +45,10 @@ public: DOM::Element const* pseudo_element_generator() const; DOM::Element* pseudo_element_generator(); - enum class GeneratedFor { - NotGenerated, - PseudoBefore, - PseudoAfter - }; - bool is_generated() const { return m_generated_for != GeneratedFor::NotGenerated; } - bool is_generated_for_before_pseudo_element() const { return m_generated_for == GeneratedFor::PseudoBefore; } - bool is_generated_for_after_pseudo_element() const { return m_generated_for == GeneratedFor::PseudoAfter; } - void set_generated_for(GeneratedFor type, DOM::Element& element) + bool is_generated() const { return m_generated_for.has_value(); } + bool is_generated_for_before_pseudo_element() const { return m_generated_for == CSS::GeneratedPseudoElement::Before; } + bool is_generated_for_after_pseudo_element() const { return m_generated_for == CSS::GeneratedPseudoElement::After; } + void set_generated_for(CSS::GeneratedPseudoElement type, DOM::Element& element) { m_generated_for = type; m_pseudo_element_generator = &element; @@ -213,7 +208,7 @@ private: bool m_has_been_wrapped_in_table_wrapper { false }; - GeneratedFor m_generated_for { GeneratedFor::NotGenerated }; + Optional m_generated_for {}; u32 m_initial_quote_nesting_level { 0 }; }; diff --git a/Libraries/LibWeb/Layout/TreeBuilder.cpp b/Libraries/LibWeb/Layout/TreeBuilder.cpp index 3b238c86e1f..5a78538f36c 100644 --- a/Libraries/LibWeb/Layout/TreeBuilder.cpp +++ b/Libraries/LibWeb/Layout/TreeBuilder.cpp @@ -225,15 +225,7 @@ void TreeBuilder::create_pseudo_element_if_needed(DOM::Element& element, CSS::Ps pseudo_element_node->append_child(*list_item_marker); } - auto generated_for = Node::GeneratedFor::NotGenerated; - if (pseudo_element == CSS::PseudoElement::Before) { - generated_for = Node::GeneratedFor::PseudoBefore; - } else if (pseudo_element == CSS::PseudoElement::After) { - generated_for = Node::GeneratedFor::PseudoAfter; - } else { - VERIFY_NOT_REACHED(); - } - + auto generated_for = CSS::to_generated_pseudo_element(pseudo_element).release_value(); pseudo_element_node->set_generated_for(generated_for, element); pseudo_element_node->set_initial_quote_nesting_level(initial_quote_nesting_level); diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPseudoElement.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPseudoElement.cpp index cbcb7e1923d..0ad57b7caf1 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPseudoElement.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPseudoElement.cpp @@ -43,8 +43,15 @@ ErrorOr generate_header_file(JsonObject& pseudo_elements_data, Core::File& SourceGenerator generator { builder }; auto pseudo_element_count = 0u; - pseudo_elements_data.for_each_member([&pseudo_element_count](auto const&, auto const&) { ++pseudo_element_count; }); + auto generated_pseudo_element_count = 0u; + pseudo_elements_data.for_each_member([&](auto const&, JsonValue const& value) { + auto& pseudo_element = value.as_object(); + ++pseudo_element_count; + if (pseudo_element.get_bool("is-generated"sv).value_or(false)) + ++generated_pseudo_element_count; + }); generator.set("pseudo_element_underlying_type", underlying_type_for_enum(pseudo_element_count)); + generator.set("generated_pseudo_element_underlying_type", underlying_type_for_enum(generated_pseudo_element_count)); generator.append(R"~~~( #pragma once @@ -74,6 +81,23 @@ StringView pseudo_element_name(PseudoElement); bool is_has_allowed_pseudo_element(PseudoElement); +enum class GeneratedPseudoElement : @generated_pseudo_element_underlying_type@ { +)~~~"); + pseudo_elements_data.for_each_member([&](auto& name, JsonValue const& value) { + auto& pseudo_element = value.as_object(); + if (!pseudo_element.get_bool("is-generated"sv).value_or(false)) + return; + + auto member_generator = generator.fork(); + member_generator.set("name:titlecase", title_casify(name)); + member_generator.appendln(" @name:titlecase@,"); + }); + generator.append(R"~~~( +}; + +Optional to_generated_pseudo_element(PseudoElement); +PseudoElement to_pseudo_element(GeneratedPseudoElement); + } )~~~"); @@ -160,6 +184,53 @@ bool is_has_allowed_pseudo_element(PseudoElement pseudo_element) } } +Optional to_generated_pseudo_element(PseudoElement pseudo_element) +{ + switch (pseudo_element) { +)~~~"); + + pseudo_elements_data.for_each_member([&](auto& name, JsonValue const& value) { + auto& pseudo_element = value.as_object(); + if (!pseudo_element.get_bool("is-generated"sv).value_or(false)) + return; + + auto member_generator = generator.fork(); + member_generator.set("name:titlecase", title_casify(name)); + member_generator.append(R"~~~( + case PseudoElement::@name:titlecase@: + return GeneratedPseudoElement::@name:titlecase@; +)~~~"); + }); + + generator.append(R"~~~( + default: + return {}; + } +} + +PseudoElement to_pseudo_element(GeneratedPseudoElement generated_pseudo_element) +{ + switch (generated_pseudo_element) { +)~~~"); + + pseudo_elements_data.for_each_member([&](auto& name, JsonValue const& value) { + auto& pseudo_element = value.as_object(); + if (!pseudo_element.get_bool("is-generated"sv).value_or(false)) + return; + + auto member_generator = generator.fork(); + member_generator.set("name:titlecase", title_casify(name)); + member_generator.append(R"~~~( + case GeneratedPseudoElement::@name:titlecase@: + return PseudoElement::@name:titlecase@; +)~~~"); + }); + + generator.append(R"~~~( + } + VERIFY_NOT_REACHED(); +} + } )~~~");