mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:48:10 +00:00
LibHTML: Implement some very simple selector matching.
We walk the entire DOM and check all selectors against all elements. Only id, class and tag name are checked right now. There's no ancestor stack or compound selectors. All in good time :^)
This commit is contained in:
parent
2b4eea5a50
commit
e971f5604c
7 changed files with 109 additions and 29 deletions
|
@ -1,5 +1,8 @@
|
|||
#include <LibHTML/CSS/StyleResolver.h>
|
||||
#include <LibHTML/CSS/StyleSheet.h>
|
||||
#include <LibHTML/DOM/Element.h>
|
||||
#include <LibHTML/Dump.h>
|
||||
#include <stdio.h>
|
||||
|
||||
StyleResolver::StyleResolver(Document& document)
|
||||
: m_document(document)
|
||||
|
@ -10,14 +13,53 @@ StyleResolver::~StyleResolver()
|
|||
{
|
||||
}
|
||||
|
||||
static bool matches(const Selector& selector, const Element& element)
|
||||
{
|
||||
// FIXME: Support compound selectors.
|
||||
ASSERT(selector.components().size() == 1);
|
||||
|
||||
auto& component = selector.components().first();
|
||||
switch (component.type) {
|
||||
case Selector::Component::Type::Id:
|
||||
return component.value == element.attribute("id");
|
||||
case Selector::Component::Type::Class:
|
||||
return element.has_class(component.value);
|
||||
case Selector::Component::Type::TagName:
|
||||
return component.value == element.tag_name();
|
||||
default:
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
NonnullRefPtrVector<StyleRule> StyleResolver::collect_matching_rules(const Element& element) const
|
||||
{
|
||||
NonnullRefPtrVector<StyleRule> matching_rules;
|
||||
for (auto& sheet : m_sheets) {
|
||||
for (auto& rule : sheet.rules()) {
|
||||
for (auto& selector : rule.selectors()) {
|
||||
if (matches(selector, element)) {
|
||||
matching_rules.append(rule);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("Rules matching Element{%p}\n", &element);
|
||||
for (auto& rule : matching_rules) {
|
||||
dump_rule(rule);
|
||||
}
|
||||
return matching_rules;
|
||||
}
|
||||
|
||||
OwnPtr<LayoutStyle> StyleResolver::resolve_document_style(const Document& document)
|
||||
{
|
||||
UNUSED_PARAM(document);
|
||||
return nullptr;
|
||||
return make<LayoutStyle>();
|
||||
}
|
||||
|
||||
OwnPtr<LayoutStyle> StyleResolver::resolve_element_style(const Element& element)
|
||||
{
|
||||
UNUSED_PARAM(element);
|
||||
return nullptr;
|
||||
auto style = make<LayoutStyle>();
|
||||
auto matching_rules = collect_matching_rules(element);
|
||||
return style;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue