Skip to content

Commit

Permalink
Add pseudo classes to the style sheet index
Browse files Browse the repository at this point in the history
  • Loading branch information
mikke89 committed Apr 10, 2022
1 parent 30d7f60 commit e14e923
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Include/RmlUi/Core/StyleSheetTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct StyleSheetIndex {
using NodeIndex = UnorderedMap<std::size_t, NodeList>;

// The following objects are given in prioritized order. Any nodes in the first object will not be contained in the next one and so on.
NodeIndex ids, classes, tags;
NodeIndex ids, classes, pseudo_classes, tags;
NodeList other;
};
} // namespace Rml
Expand Down
4 changes: 4 additions & 0 deletions Source/Core/StyleSheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ SharedPtr<const ElementDefinition> StyleSheet::GetElementDefinition(const Elemen
const String& tag = element->GetTagName();
const String& id = element->GetId();
const StringList& class_names = element->GetStyle()->GetClassNameList();
const PseudoClassMap& pseudo_classes = element->GetStyle()->GetActivePseudoClasses();

// First, look up the indexed requirements.
if (!id.empty())
Expand All @@ -202,6 +203,9 @@ SharedPtr<const ElementDefinition> StyleSheet::GetElementDefinition(const Elemen
for (const String& name : class_names)
AddApplicableNodes(styled_node_index.classes, name);

for (const auto& pseudo : pseudo_classes)
AddApplicableNodes(styled_node_index.pseudo_classes, pseudo.first);

AddApplicableNodes(styled_node_index.tags, tag);

// Also check all remaining nodes that don't contain any indexed requirements.
Expand Down
12 changes: 8 additions & 4 deletions Source/Core/StyleSheetNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ void StyleSheetNode::BuildIndex(StyleSheetIndex& styled_node_index) const
// class with the most unique name. For example by adding the class from this node's list that has the fewest existing matches.
IndexInsertNode(styled_node_index.classes, class_names.front(), this);
}
else if (!pseudo_class_names.empty())
{
IndexInsertNode(styled_node_index.pseudo_classes, pseudo_class_names.front(), this);
}
else if (!tag.empty())
{
IndexInsertNode(styled_node_index.tags, tag, this);
Expand Down Expand Up @@ -266,22 +270,22 @@ bool StyleSheetNode::IsApplicable(const Element* const in_element) const
// Determine whether the element matches the current node and its entire lineage. The entire hierarchy of
// the element's document will be considered during the match as necessary.

if (!tag.empty() && tag != in_element->GetTagName())
return false;

for (const String& name : pseudo_class_names)
{
if (!in_element->IsPseudoClassSet(name))
return false;
}

if (!id.empty() && id != in_element->GetId())
return false;

for (const String& name : class_names)
{
if (!in_element->IsClassSet(name))
return false;
}

if (!tag.empty() && tag != in_element->GetTagName())
if (!id.empty() && id != in_element->GetId())
return false;

const Element* element = in_element;
Expand Down

0 comments on commit e14e923

Please sign in to comment.