LibWeb/CSS: Add quick-and-dirty parsing for pseudo-element selectors

As noted, this is hacky because the parser wasn't written to allow
parsing an individual component of a selector. (Fox example, the
convenient-sounding `parse_pseudo_simple_selector()` assumes the first
colon has already been consumed...) So until that changes, this parses
the input as an entire selector-list, and then throws it away if it's
not a single pseudo-element selector.

It's only temporary though, I promise. 😅
This commit is contained in:
Sam Atkins 2024-08-06 12:44:43 +01:00 committed by Andreas Kling
commit dae9c9be40
Notes: github-actions[bot] 2024-08-07 14:15:55 +00:00
3 changed files with 37 additions and 1 deletions

View file

@ -32,6 +32,34 @@ Optional<SelectorList> Parser::parse_as_relative_selector(SelectorParsingMode pa
return {};
}
Optional<Selector::PseudoElement> Parser::parse_as_pseudo_element_selector()
{
// FIXME: This is quite janky. Selector parsing is not at all designed to allow parsing just a single part of a selector.
// So, this code parses a whole selector, then rejects it if it's not a single pseudo-element simple selector.
// Come back and fix this, future Sam!
auto maybe_selector_list = parse_a_selector_list(m_token_stream, SelectorType::Standalone, SelectorParsingMode::Standard);
if (maybe_selector_list.is_error())
return {};
auto& selector_list = maybe_selector_list.value();
if (selector_list.size() != 1)
return {};
auto& selector = selector_list.first();
if (selector->compound_selectors().size() != 1)
return {};
auto& first_compound_selector = selector->compound_selectors().first();
if (first_compound_selector.simple_selectors.size() != 1)
return {};
auto& simple_selector = first_compound_selector.simple_selectors.first();
if (simple_selector.type != Selector::SimpleSelector::Type::PseudoElement)
return {};
return simple_selector.pseudo_element();
}
template<typename T>
Parser::ParseErrorOr<SelectorList> Parser::parse_a_selector_list(TokenStream<T>& tokens, SelectorType mode, SelectorParsingMode parsing_mode)
{