1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:27:35 +00:00

LibHTML: Implement some attribute selector support

This patch adds a[foo] and a[foo=bar] attribute selectors.

Note that an attribute selector is an optional part of a selector
component, and not a component on its own.
This commit is contained in:
Andreas Kling 2019-11-21 20:07:43 +01:00
parent 54a6ae9201
commit 8946e50986
6 changed files with 115 additions and 8 deletions

View file

@ -33,6 +33,16 @@ public:
Relation relation { Relation::None };
String value;
enum class AttributeMatchType {
None,
HasAttribute,
ExactValueMatch,
};
AttributeMatchType attribute_match_type { AttributeMatchType::None };
String attribute_name;
String attribute_value;
};
explicit Selector(Vector<Component>&&);

View file

@ -29,6 +29,19 @@ bool matches(const Selector::Component& component, const Element& element)
break;
}
switch (component.attribute_match_type) {
case Selector::Component::AttributeMatchType::HasAttribute:
if (!element.has_attribute(component.attribute_name))
return false;
break;
case Selector::Component::AttributeMatchType::ExactValueMatch:
if (element.attribute(component.attribute_name) != component.attribute_value)
return false;
break;
default:
break;
}
switch (component.type) {
case Selector::Component::Type::Universal:
return true;