1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-16 20:15:07 +00:00

LibHTML: Add adjacent (+) and general (~) sibling combinators

This patch implements two more selector features:

- "div + p" matches the <p> sibling immediately after a <div>.
- "div ~ p" matches all <p> siblings after a <div>.
This commit is contained in:
Andreas Kling 2019-10-06 19:59:07 +02:00
parent 5a6c36dc91
commit bedb00603c
5 changed files with 53 additions and 2 deletions

View file

@ -84,6 +84,11 @@ public:
return isalnum(ch) || ch == '-' || ch == '_' || ch == '(' || ch == ')' || ch == '@';
}
bool is_combinator(char ch) const
{
return ch == '~' || ch == '>' || ch == '+';
}
Optional<Selector::Component> parse_selector_component()
{
consume_whitespace();
@ -93,8 +98,18 @@ public:
if (peek() == '{')
return {};
if (peek() == '>') {
relation = Selector::Component::Relation::ImmediateChild;
if (is_combinator(peek())) {
switch (peek()) {
case '>':
relation = Selector::Component::Relation::ImmediateChild;
break;
case '+':
relation = Selector::Component::Relation::AdjacentSibling;
break;
case '~':
relation = Selector::Component::Relation::GeneralSibling;
break;
}
consume_one();
consume_whitespace();
}