1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:58:11 +00:00

LibHTML: Implement compound selectors

This patch moves the Selector object model closer to the specification
objects in Selectors Level 4.

A "Selector" in LibHTML is now a { Vector<ComplexSelector> }, which is
a { Relation, CompoundSelector }. A CompoundSelector is really just
a Vector<SimpleSelector>, and SimpleSelector is "Component" renamed.

This makes a lot more selectors actually match on the Ubuntu Apache2
default homepage. :^)
This commit is contained in:
Andreas Kling 2019-11-27 20:37:36 +01:00
parent 449ebbddb6
commit d19d4da14a
5 changed files with 194 additions and 148 deletions

View file

@ -1,7 +1,7 @@
#include <LibHTML/CSS/Selector.h>
Selector::Selector(Vector<Component>&& components)
: m_components(move(components))
Selector::Selector(Vector<ComplexSelector>&& component_lists)
: m_complex_selectors(move(component_lists))
{
}
@ -15,19 +15,21 @@ Specificity Selector::specificity() const
unsigned tag_names = 0;
unsigned classes = 0;
for (auto& component : m_components) {
switch (component.type) {
case Component::Type::Id:
++ids;
break;
case Component::Type::Class:
++classes;
break;
case Component::Type::TagName:
++tag_names;
break;
default:
break;
for (auto& list : m_complex_selectors) {
for (auto& simple_selector : list.compound_selector) {
switch (simple_selector.type) {
case SimpleSelector::Type::Id:
++ids;
break;
case SimpleSelector::Type::Class:
++classes;
break;
case SimpleSelector::Type::TagName:
++tag_names;
break;
default:
break;
}
}
}

View file

@ -6,7 +6,7 @@
class Selector {
public:
struct Component {
struct SimpleSelector {
enum class Type {
Invalid,
Universal,
@ -23,15 +23,6 @@ public:
};
PseudoClass pseudo_class { PseudoClass::None };
enum class Relation {
None,
ImmediateChild,
Descendant,
AdjacentSibling,
GeneralSibling,
};
Relation relation { Relation::None };
String value;
enum class AttributeMatchType {
@ -45,13 +36,27 @@ public:
String attribute_value;
};
explicit Selector(Vector<Component>&&);
struct ComplexSelector {
enum class Relation {
None,
ImmediateChild,
Descendant,
AdjacentSibling,
GeneralSibling,
};
Relation relation { Relation::None };
using CompoundSelector = Vector<SimpleSelector>;
CompoundSelector compound_selector;
};
explicit Selector(Vector<ComplexSelector>&&);
~Selector();
const Vector<Component>& components() const { return m_components; }
const Vector<ComplexSelector>& complex_selectors() const { return m_complex_selectors; }
Specificity specificity() const;
private:
Vector<Component> m_components;
Vector<ComplexSelector> m_complex_selectors;
};

View file

@ -14,27 +14,27 @@ static bool matches_hover_pseudo_class(const Element& element)
return element.is_ancestor_of(*hovered_node);
}
bool matches(const Selector::Component& component, const Element& element)
bool matches(const Selector::SimpleSelector& component, const Element& element)
{
switch (component.pseudo_class) {
case Selector::Component::PseudoClass::None:
case Selector::SimpleSelector::PseudoClass::None:
break;
case Selector::Component::PseudoClass::Link:
case Selector::SimpleSelector::PseudoClass::Link:
if (!element.is_link())
return false;
break;
case Selector::Component::PseudoClass::Hover:
case Selector::SimpleSelector::PseudoClass::Hover:
if (!matches_hover_pseudo_class(element))
return false;
break;
}
switch (component.attribute_match_type) {
case Selector::Component::AttributeMatchType::HasAttribute:
case Selector::SimpleSelector::AttributeMatchType::HasAttribute:
if (!element.has_attribute(component.attribute_name))
return false;
break;
case Selector::Component::AttributeMatchType::ExactValueMatch:
case Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
if (element.attribute(component.attribute_name) != component.attribute_value)
return false;
break;
@ -43,50 +43,52 @@ bool matches(const Selector::Component& component, const Element& element)
}
switch (component.type) {
case Selector::Component::Type::Universal:
case Selector::SimpleSelector::Type::Universal:
return true;
case Selector::Component::Type::Id:
case Selector::SimpleSelector::Type::Id:
return component.value == element.attribute("id");
case Selector::Component::Type::Class:
case Selector::SimpleSelector::Type::Class:
return element.has_class(component.value);
case Selector::Component::Type::TagName:
case Selector::SimpleSelector::Type::TagName:
return component.value == element.tag_name();
default:
ASSERT_NOT_REACHED();
}
}
bool matches(const Selector& selector, int component_index, const Element& element)
bool matches(const Selector& selector, int component_list_index, const Element& element)
{
auto& component = selector.components()[component_index];
if (!matches(component, element))
return false;
switch (component.relation) {
case Selector::Component::Relation::None:
auto& component_list = selector.complex_selectors()[component_list_index];
for (auto& component : component_list.compound_selector) {
if (!matches(component, element))
return false;
}
switch (component_list.relation) {
case Selector::ComplexSelector::Relation::None:
return true;
case Selector::Component::Relation::Descendant:
ASSERT(component_index != 0);
case Selector::ComplexSelector::Relation::Descendant:
ASSERT(component_list_index != 0);
for (auto* ancestor = element.parent(); ancestor; ancestor = ancestor->parent()) {
if (!is<Element>(*ancestor))
continue;
if (matches(selector, component_index - 1, to<Element>(*ancestor)))
if (matches(selector, component_list_index - 1, to<Element>(*ancestor)))
return true;
}
return false;
case Selector::Component::Relation::ImmediateChild:
ASSERT(component_index != 0);
case Selector::ComplexSelector::Relation::ImmediateChild:
ASSERT(component_list_index != 0);
if (!element.parent() || !is<Element>(*element.parent()))
return false;
return matches(selector, component_index - 1, to<Element>(*element.parent()));
case Selector::Component::Relation::AdjacentSibling:
ASSERT(component_index != 0);
return matches(selector, component_list_index - 1, to<Element>(*element.parent()));
case Selector::ComplexSelector::Relation::AdjacentSibling:
ASSERT(component_list_index != 0);
if (auto* sibling = element.previous_element_sibling())
return matches(selector, component_index - 1, *sibling);
return matches(selector, component_list_index - 1, *sibling);
return false;
case Selector::Component::Relation::GeneralSibling:
ASSERT(component_index != 0);
case Selector::ComplexSelector::Relation::GeneralSibling:
ASSERT(component_list_index != 0);
for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
if (matches(selector, component_index - 1, *sibling))
if (matches(selector, component_list_index - 1, *sibling))
return true;
}
return false;
@ -96,8 +98,8 @@ bool matches(const Selector& selector, int component_index, const Element& eleme
bool matches(const Selector& selector, const Element& element)
{
ASSERT(!selector.components().is_empty());
return matches(selector, selector.components().size() - 1, element);
ASSERT(!selector.complex_selectors().is_empty());
return matches(selector, selector.complex_selectors().size() - 1, element);
}
}