diff --git a/LibHTML/CSS/Selector.cpp b/LibHTML/CSS/Selector.cpp
index bef4d1f9a5e..8e7d30c506a 100644
--- a/LibHTML/CSS/Selector.cpp
+++ b/LibHTML/CSS/Selector.cpp
@@ -8,3 +8,28 @@ Selector::Selector(Vector&& 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 };
+}
diff --git a/LibHTML/CSS/Selector.h b/LibHTML/CSS/Selector.h
index fac4b5681a6..b1d9a53ff12 100644
--- a/LibHTML/CSS/Selector.h
+++ b/LibHTML/CSS/Selector.h
@@ -2,11 +2,17 @@
#include
#include
+#include
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& components() const { return m_components; }
+ Specificity specificity() const;
+
private:
Vector m_components;
};
diff --git a/LibHTML/CSS/Specificity.h b/LibHTML/CSS/Specificity.h
new file mode 100644
index 00000000000..9ea471a96da
--- /dev/null
+++ b/LibHTML/CSS/Specificity.h
@@ -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 };
+};