LibHTML: Add Selector::specificity(), which returns a Specificity object.

This commit is contained in:
Andreas Kling 2019-06-29 17:32:32 +02:00
parent b729b5fc64
commit 9a7dc06567
Notes: sideshowbarker 2024-07-19 13:26:58 +09:00
3 changed files with 68 additions and 1 deletions

View file

@ -8,3 +8,28 @@ Selector::Selector(Vector<Component>&& components)
Selector::~Selector()
{
}
Specificity Selector::specificity() const
{
unsigned ids = 0;
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;
}
}
return { ids, classes, tag_names };
}

View file

@ -2,11 +2,17 @@
#include <AK/AKString.h>
#include <AK/Vector.h>
#include <LibHTML/CSS/Specificity.h>
class Selector {
public:
struct Component {
enum class Type { Invalid, TagName, Id, Class };
enum class Type {
Invalid,
TagName,
Id,
Class
};
Type type { Type::Invalid };
String value;
};
@ -16,6 +22,8 @@ public:
const Vector<Component>& components() const { return m_components; }
Specificity specificity() const;
private:
Vector<Component> m_components;
};

34
LibHTML/CSS/Specificity.h Normal file
View file

@ -0,0 +1,34 @@
#pragma once
class Specificity {
public:
Specificity(unsigned ids, unsigned classes, unsigned tag_names)
: m_ids(ids)
, m_classes(classes)
, m_tag_names(tag_names)
{
}
unsigned ids() const { return m_ids; }
unsigned classes() const { return m_classes; }
unsigned tag_names() const { return m_tag_names; }
bool operator<(const Specificity& other) const
{
return m_ids < other.m_ids
|| m_classes < other.m_classes
|| m_tag_names < other.m_tag_names;
}
bool operator==(const Specificity& other) const
{
return m_ids == other.m_ids
|| m_classes < other.m_classes
|| m_tag_names < other.m_tag_names;
}
private:
unsigned m_ids { 0 };
unsigned m_classes { 0 };
unsigned m_tag_names { 0 };
};