mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 01:12:44 +00:00 
			
		
		
		
	LibHTML: Add Selector::specificity(), which returns a Specificity object.
This commit is contained in:
		
							parent
							
								
									b729b5fc64
								
							
						
					
					
						commit
						9a7dc06567
					
				
					 3 changed files with 68 additions and 1 deletions
				
			
		|  | @ -8,3 +8,28 @@ Selector::Selector(Vector<Component>&& components) | ||||||
| Selector::~Selector() | 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 }; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -2,11 +2,17 @@ | ||||||
| 
 | 
 | ||||||
| #include <AK/AKString.h> | #include <AK/AKString.h> | ||||||
| #include <AK/Vector.h> | #include <AK/Vector.h> | ||||||
|  | #include <LibHTML/CSS/Specificity.h> | ||||||
| 
 | 
 | ||||||
| class Selector { | class Selector { | ||||||
| public: | public: | ||||||
|     struct Component { |     struct Component { | ||||||
|         enum class Type { Invalid, TagName, Id, Class }; |         enum class Type { | ||||||
|  |             Invalid, | ||||||
|  |             TagName, | ||||||
|  |             Id, | ||||||
|  |             Class | ||||||
|  |         }; | ||||||
|         Type type { Type::Invalid }; |         Type type { Type::Invalid }; | ||||||
|         String value; |         String value; | ||||||
|     }; |     }; | ||||||
|  | @ -16,6 +22,8 @@ public: | ||||||
| 
 | 
 | ||||||
|     const Vector<Component>& components() const { return m_components; } |     const Vector<Component>& components() const { return m_components; } | ||||||
| 
 | 
 | ||||||
|  |     Specificity specificity() const; | ||||||
|  | 
 | ||||||
| private: | private: | ||||||
|     Vector<Component> m_components; |     Vector<Component> m_components; | ||||||
| }; | }; | ||||||
|  |  | ||||||
							
								
								
									
										34
									
								
								LibHTML/CSS/Specificity.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								LibHTML/CSS/Specificity.h
									
										
									
									
									
										Normal 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 }; | ||||||
|  | }; | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling