mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:47:44 +00:00
LibHTML: Get rid of the style tree
We now create a layout tree directly from the DOM tree. This way we don't actually lose text nodes ^)
This commit is contained in:
parent
a9ebd676e5
commit
fd0aa5dd43
22 changed files with 126 additions and 261 deletions
22
Libraries/LibHTML/CSS/StyleProperties.cpp
Normal file
22
Libraries/LibHTML/CSS/StyleProperties.cpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
#include <LibHTML/CSS/StyleProperties.h>
|
||||
|
||||
void StyleProperties::set_property(const String& name, NonnullRefPtr<StyleValue> value)
|
||||
{
|
||||
m_property_values.set(name, move(value));
|
||||
}
|
||||
|
||||
Optional<NonnullRefPtr<StyleValue>> StyleProperties::property(const String& name) const
|
||||
{
|
||||
auto it = m_property_values.find(name);
|
||||
if (it == m_property_values.end())
|
||||
return {};
|
||||
return it->value;
|
||||
}
|
||||
|
||||
Length StyleProperties::length_or_fallback(const StringView& property_name, const Length& fallback) const
|
||||
{
|
||||
auto value = property(property_name);
|
||||
if (!value.has_value())
|
||||
return fallback;
|
||||
return value.value()->to_length();
|
||||
}
|
23
Libraries/LibHTML/CSS/StyleProperties.h
Normal file
23
Libraries/LibHTML/CSS/StyleProperties.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <LibHTML/CSS/StyleValue.h>
|
||||
|
||||
class StyleProperties {
|
||||
public:
|
||||
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);
|
||||
Optional<NonnullRefPtr<StyleValue>> property(const String& name) const;
|
||||
|
||||
Length length_or_fallback(const StringView& property_name, const Length& fallback) const;
|
||||
|
||||
private:
|
||||
HashMap<String, NonnullRefPtr<StyleValue>> m_property_values;
|
||||
};
|
|
@ -1,6 +1,5 @@
|
|||
#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>
|
||||
|
@ -53,19 +52,14 @@ NonnullRefPtrVector<StyleRule> StyleResolver::collect_matching_rules(const Eleme
|
|||
return matching_rules;
|
||||
}
|
||||
|
||||
NonnullRefPtr<StyledNode> StyleResolver::create_styled_node(const Document& document)
|
||||
StyleProperties StyleResolver::resolve_style(const Element& element)
|
||||
{
|
||||
return StyledNode::create(document);
|
||||
}
|
||||
|
||||
NonnullRefPtr<StyledNode> StyleResolver::create_styled_node(const Element& element)
|
||||
{
|
||||
auto style = StyledNode::create(element);
|
||||
StyleProperties style_properties;
|
||||
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());
|
||||
style_properties.set_property(declaration.property_name(), declaration.value());
|
||||
}
|
||||
}
|
||||
return style;
|
||||
return style_properties;
|
||||
}
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/NonnullRefPtrVector.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <LibHTML/CSS/StyleProperties.h>
|
||||
|
||||
class Document;
|
||||
class Element;
|
||||
class ParentNode;
|
||||
class StyleRule;
|
||||
class StyleSheet;
|
||||
class StyledNode;
|
||||
|
||||
class StyleResolver {
|
||||
public:
|
||||
|
@ -18,12 +18,10 @@ public:
|
|||
Document& document() { return m_document; }
|
||||
const Document& document() const { return m_document; }
|
||||
|
||||
NonnullRefPtr<StyledNode> create_styled_node(const Element&);
|
||||
NonnullRefPtr<StyledNode> create_styled_node(const Document&);
|
||||
StyleProperties resolve_style(const Element&);
|
||||
|
||||
NonnullRefPtrVector<StyleRule> collect_matching_rules(const Element&) const;
|
||||
|
||||
|
||||
private:
|
||||
Document& m_document;
|
||||
};
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
#include <LibHTML/CSS/StyledNode.h>
|
||||
|
||||
StyledNode::StyledNode(const Node* node)
|
||||
: m_node(node)
|
||||
{
|
||||
}
|
||||
|
||||
StyledNode::~StyledNode()
|
||||
{
|
||||
}
|
||||
|
||||
Display StyledNode::display() const
|
||||
{
|
||||
auto it = m_property_values.find("display");
|
||||
if (it == m_property_values.end())
|
||||
return Display::Inline;
|
||||
auto value = it->value->to_string();
|
||||
if (value == "none")
|
||||
return Display::None;
|
||||
if (value == "block")
|
||||
return Display::Block;
|
||||
if (value == "inline")
|
||||
return Display::Inline;
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <AK/Optional.h>
|
||||
#include <LibHTML/CSS/StyleValue.h>
|
||||
#include <LibHTML/TreeNode.h>
|
||||
|
||||
class Node;
|
||||
|
||||
enum class Display {
|
||||
None,
|
||||
Block,
|
||||
Inline,
|
||||
};
|
||||
|
||||
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; }
|
||||
|
||||
template<typename Callback>
|
||||
inline void for_each_child(Callback callback) const
|
||||
{
|
||||
for (auto* node = first_child(); node; node = node->next_sibling())
|
||||
callback(*node);
|
||||
}
|
||||
|
||||
template<typename Callback>
|
||||
inline void for_each_child(Callback callback)
|
||||
{
|
||||
for (auto* node = first_child(); node; node = node->next_sibling())
|
||||
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));
|
||||
}
|
||||
|
||||
Optional<NonnullRefPtr<StyleValue>> property(const String& name) const
|
||||
{
|
||||
auto it = m_property_values.find(name);
|
||||
if (it == m_property_values.end())
|
||||
return {};
|
||||
return it->value;
|
||||
}
|
||||
|
||||
Display display() const;
|
||||
|
||||
Length length_or_fallback(const StringView& property_name, const Length& fallback)
|
||||
{
|
||||
auto value = property(property_name);
|
||||
if (!value.has_value())
|
||||
return fallback;
|
||||
return value.value()->to_length();
|
||||
}
|
||||
|
||||
protected:
|
||||
explicit StyledNode(const Node*);
|
||||
|
||||
private:
|
||||
const Node* m_node { nullptr };
|
||||
HashMap<String, NonnullRefPtr<StyleValue>> m_property_values;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue