From a744ae79ff6db5dee5287127e5ac15471f3e955d Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Wed, 29 Mar 2023 23:48:40 +0100 Subject: LibWeb: Implement the `:defined` pseudo class This selects an element if it is either a built-in element, or an upgraded custom element. --- Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp | 2 ++ Userland/Libraries/LibWeb/CSS/Selector.cpp | 1 + Userland/Libraries/LibWeb/CSS/Selector.h | 3 +++ Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp | 2 ++ Userland/Libraries/LibWeb/Dump.cpp | 3 +++ 5 files changed, 11 insertions(+) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index d513f66b17..10923271be 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -494,6 +494,8 @@ Parser::ParseErrorOr Parser::parse_pseudo_simple_selec return make_pseudo_class_selector(Selector::SimpleSelector::PseudoClass::Type::Checked); if (pseudo_name.equals_ignoring_ascii_case("indeterminate"sv)) return make_pseudo_class_selector(Selector::SimpleSelector::PseudoClass::Type::Indeterminate); + if (pseudo_name.equals_ignoring_ascii_case("defined"sv)) + return make_pseudo_class_selector(Selector::SimpleSelector::PseudoClass::Type::Defined); if (pseudo_name.equals_ignoring_ascii_case("disabled"sv)) return make_pseudo_class_selector(Selector::SimpleSelector::PseudoClass::Type::Disabled); if (pseudo_name.equals_ignoring_ascii_case("empty"sv)) diff --git a/Userland/Libraries/LibWeb/CSS/Selector.cpp b/Userland/Libraries/LibWeb/CSS/Selector.cpp index d0c6636218..9fb5d880c7 100644 --- a/Userland/Libraries/LibWeb/CSS/Selector.cpp +++ b/Userland/Libraries/LibWeb/CSS/Selector.cpp @@ -228,6 +228,7 @@ ErrorOr Selector::SimpleSelector::serialize() const case Selector::SimpleSelector::PseudoClass::Type::Checked: case Selector::SimpleSelector::PseudoClass::Type::Active: case Selector::SimpleSelector::PseudoClass::Type::Scope: + case Selector::SimpleSelector::PseudoClass::Type::Defined: // If the pseudo-class does not accept arguments append ":" (U+003A), followed by the name of the pseudo-class, to s. TRY(s.try_append(':')); TRY(s.try_append(pseudo_class_name(pseudo_class.type))); diff --git a/Userland/Libraries/LibWeb/CSS/Selector.h b/Userland/Libraries/LibWeb/CSS/Selector.h index eef67017ee..bd20ad111f 100644 --- a/Userland/Libraries/LibWeb/CSS/Selector.h +++ b/Userland/Libraries/LibWeb/CSS/Selector.h @@ -112,6 +112,7 @@ public: Active, Lang, Scope, + Defined, }; Type type; @@ -295,6 +296,8 @@ constexpr StringView pseudo_class_name(Selector::SimpleSelector::PseudoClass::Ty return "lang"sv; case Selector::SimpleSelector::PseudoClass::Type::Scope: return "scope"sv; + case Selector::SimpleSelector::PseudoClass::Type::Defined: + return "defined"sv; } VERIFY_NOT_REACHED(); } diff --git a/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp b/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp index 8c8505c2c9..6881a403d9 100644 --- a/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp +++ b/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp @@ -268,6 +268,8 @@ static inline bool matches_pseudo_class(CSS::Selector::SimpleSelector::PseudoCla return matches_checked_pseudo_class(element); case CSS::Selector::SimpleSelector::PseudoClass::Type::Indeterminate: return matches_indeterminate_pseudo_class(element); + case CSS::Selector::SimpleSelector::PseudoClass::Type::Defined: + return element.is_defined(); case CSS::Selector::SimpleSelector::PseudoClass::Type::Is: case CSS::Selector::SimpleSelector::PseudoClass::Type::Where: for (auto& selector : pseudo_class.argument_selector_list) { diff --git a/Userland/Libraries/LibWeb/Dump.cpp b/Userland/Libraries/LibWeb/Dump.cpp index 147e0491c1..56b00f4546 100644 --- a/Userland/Libraries/LibWeb/Dump.cpp +++ b/Userland/Libraries/LibWeb/Dump.cpp @@ -459,6 +459,9 @@ void dump_selector(StringBuilder& builder, CSS::Selector const& selector) case CSS::Selector::SimpleSelector::PseudoClass::Type::Scope: pseudo_class_description = "Scope"; break; + case CSS::Selector::SimpleSelector::PseudoClass::Type::Defined: + pseudo_class_description = "Defined"; + break; } builder.appendff(" pseudo_class={}", pseudo_class_description); -- cgit v1.2.3