1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:48:10 +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

@ -51,6 +51,18 @@ static bool matches(const Selector& selector, int component_index, const Element
if (!element.parent() || !element.parent()->is_element())
return false;
return matches(selector, component_index - 1, static_cast<const Element&>(*element.parent()));
case Selector::Component::Relation::AdjacentSibling:
ASSERT(component_index != 0);
if (auto* sibling = element.previous_element_sibling())
return matches(selector, component_index - 1, *sibling);
return false;
case Selector::Component::Relation::GeneralSibling:
ASSERT(component_index != 0);
for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
if (matches(selector, component_index - 1, *sibling))
return true;
}
return false;
}
ASSERT_NOT_REACHED();
}