1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 18:28:12 +00:00

LibHTML: Start building the style tree.

Walk the DOM and construct a parallel style tree that points back to the DOM
and has the relevant CSS property values hanging off of them.

The values are picked based on naive selector matching. There's no cascade
or specificity taken into account yet.
This commit is contained in:
Andreas Kling 2019-06-28 21:17:34 +02:00
parent 3af59dfed1
commit ffcbe8f0de
6 changed files with 86 additions and 16 deletions

View file

@ -1,5 +1,7 @@
#include <LibHTML/CSS/StyleResolver.h>
#include <LibHTML/CSS/StyleSheet.h>
#include <LibHTML/CSS/StyledNode.h>
#include <LibHTML/DOM/Document.h>
#include <LibHTML/DOM/Element.h>
#include <LibHTML/Dump.h>
#include <stdio.h>
@ -51,15 +53,19 @@ NonnullRefPtrVector<StyleRule> StyleResolver::collect_matching_rules(const Eleme
return matching_rules;
}
OwnPtr<LayoutStyle> StyleResolver::resolve_document_style(const Document& document)
NonnullRefPtr<StyledNode> StyleResolver::create_styled_node(const Document& document)
{
UNUSED_PARAM(document);
return make<LayoutStyle>();
return StyledNode::create(document);
}
OwnPtr<LayoutStyle> StyleResolver::resolve_element_style(const Element& element)
NonnullRefPtr<StyledNode> StyleResolver::create_styled_node(const Element& element)
{
auto style = make<LayoutStyle>();
auto style = StyledNode::create(element);
auto matching_rules = collect_matching_rules(element);
for (auto& rule : matching_rules) {
for (auto& declaration : rule.declarations()) {
style->set_property(declaration.property_name(), declaration.value());
}
}
return style;
}

View file

@ -2,12 +2,13 @@
#include <AK/OwnPtr.h>
#include <AK/NonnullRefPtrVector.h>
#include <LibHTML/Layout/LayoutStyle.h>
class Document;
class Element;
class ParentNode;
class StyleRule;
class StyleSheet;
class StyledNode;
class StyleResolver {
public:
@ -19,8 +20,8 @@ public:
void add_sheet(const StyleSheet& sheet) { m_sheets.append(sheet); }
OwnPtr<LayoutStyle> resolve_element_style(const Element&);
OwnPtr<LayoutStyle> resolve_document_style(const Document&);
NonnullRefPtr<StyledNode> create_styled_node(const Element&);
NonnullRefPtr<StyledNode> create_styled_node(const Document&);
NonnullRefPtrVector<StyleRule> collect_matching_rules(const Element&) const;

View file

@ -10,6 +10,10 @@ class Node;
class StyledNode : public TreeNode<StyledNode> {
public:
static NonnullRefPtr<StyledNode> create(const Node& node)
{
return adopt(*new StyledNode(&node));
}
~StyledNode();
const Node* node() const { return m_node; }
@ -28,6 +32,18 @@ public:
callback(*node);
}
template<typename Callback>
inline void for_each_property(Callback callback) const
{
for (auto& it : m_property_values)
callback(it.key, *it.value);
}
void set_property(const String& name, NonnullRefPtr<StyleValue> value)
{
m_property_values.set(name, move(value));
}
protected:
explicit StyledNode(const Node*);