mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:57:43 +00:00
LibHTML: Create some subdirectories.
This commit is contained in:
parent
0522a8f71c
commit
1f51c2b7da
25 changed files with 49 additions and 50 deletions
42
LibHTML/DOM/Document.cpp
Normal file
42
LibHTML/DOM/Document.cpp
Normal file
|
@ -0,0 +1,42 @@
|
|||
#include <LibHTML/DOM/Document.h>
|
||||
#include <LibHTML/DOM/Element.h>
|
||||
#include <LibHTML/Layout/LayoutDocument.h>
|
||||
#include <stdio.h>
|
||||
|
||||
Document::Document()
|
||||
: ParentNode(NodeType::DOCUMENT_NODE)
|
||||
{
|
||||
}
|
||||
|
||||
Document::~Document()
|
||||
{
|
||||
}
|
||||
|
||||
static void create_layout_tree_for_node(Node& node)
|
||||
{
|
||||
if (auto layout_node = node.create_layout_node()) {
|
||||
node.set_layout_node(*layout_node);
|
||||
#ifdef DEBUG_LAYOUT_TREE_BUILD
|
||||
if (node.is_element()) {
|
||||
printf("created layout node for <%s>, parent is %p, parent ln is %p\n", static_cast<const Element&>(node).tag_name().characters(), node.parent_node(), node.parent_node()->layout_node());
|
||||
}
|
||||
#endif
|
||||
if (node.parent_node() && node.parent_node()->layout_node())
|
||||
node.parent_node()->layout_node()->append_child(*layout_node);
|
||||
}
|
||||
if (node.is_parent_node()) {
|
||||
static_cast<ParentNode&>(node).for_each_child([&](auto& child) {
|
||||
create_layout_tree_for_node(child);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void Document::build_layout_tree()
|
||||
{
|
||||
create_layout_tree_for_node(*this);
|
||||
}
|
||||
|
||||
RetainPtr<LayoutNode> Document::create_layout_node()
|
||||
{
|
||||
return adopt(*new LayoutDocument(*this));
|
||||
}
|
18
LibHTML/DOM/Document.h
Normal file
18
LibHTML/DOM/Document.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/AKString.h>
|
||||
#include <LibHTML/DOM/ParentNode.h>
|
||||
|
||||
class LayoutNode;
|
||||
|
||||
class Document : public ParentNode {
|
||||
public:
|
||||
Document();
|
||||
virtual ~Document() override;
|
||||
|
||||
virtual RetainPtr<LayoutNode> create_layout_node() override;
|
||||
|
||||
void build_layout_tree();
|
||||
|
||||
private:
|
||||
};
|
66
LibHTML/DOM/Element.cpp
Normal file
66
LibHTML/DOM/Element.cpp
Normal file
|
@ -0,0 +1,66 @@
|
|||
#include <LibHTML/DOM/Element.h>
|
||||
#include <LibHTML/Layout/LayoutBlock.h>
|
||||
#include <LibHTML/Layout/LayoutInline.h>
|
||||
|
||||
Element::Element(const String& tag_name)
|
||||
: ParentNode(NodeType::ELEMENT_NODE)
|
||||
, m_tag_name(tag_name)
|
||||
{
|
||||
}
|
||||
|
||||
Element::~Element()
|
||||
{
|
||||
}
|
||||
|
||||
Attribute* Element::find_attribute(const String& name)
|
||||
{
|
||||
for (auto& attribute : m_attributes) {
|
||||
if (attribute.name() == name)
|
||||
return &attribute;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const Attribute* Element::find_attribute(const String& name) const
|
||||
{
|
||||
for (auto& attribute : m_attributes) {
|
||||
if (attribute.name() == name)
|
||||
return &attribute;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
String Element::attribute(const String& name) const
|
||||
{
|
||||
if (auto* attribute = find_attribute(name))
|
||||
return attribute->value();
|
||||
return { };
|
||||
}
|
||||
|
||||
void Element::set_attribute(const String& name, const String& value)
|
||||
{
|
||||
if (auto* attribute = find_attribute(name))
|
||||
attribute->set_value(value);
|
||||
else
|
||||
m_attributes.append({ name, value });
|
||||
}
|
||||
|
||||
void Element::set_attributes(Vector<Attribute>&& attributes)
|
||||
{
|
||||
m_attributes = move(attributes);
|
||||
}
|
||||
|
||||
RetainPtr<LayoutNode> Element::create_layout_node()
|
||||
{
|
||||
if (m_tag_name == "html")
|
||||
return adopt(*new LayoutBlock(*this));
|
||||
if (m_tag_name == "body")
|
||||
return adopt(*new LayoutBlock(*this));
|
||||
if (m_tag_name == "h1")
|
||||
return adopt(*new LayoutBlock(*this));
|
||||
if (m_tag_name == "p")
|
||||
return adopt(*new LayoutBlock(*this));
|
||||
if (m_tag_name == "b")
|
||||
return adopt(*new LayoutInline(*this));
|
||||
return nullptr;
|
||||
}
|
52
LibHTML/DOM/Element.h
Normal file
52
LibHTML/DOM/Element.h
Normal file
|
@ -0,0 +1,52 @@
|
|||
#pragma once
|
||||
|
||||
#include <LibHTML/DOM/ParentNode.h>
|
||||
#include <AK/AKString.h>
|
||||
|
||||
class Attribute {
|
||||
public:
|
||||
Attribute(const String& name, const String& value)
|
||||
: m_name(name)
|
||||
, m_value(value)
|
||||
{
|
||||
}
|
||||
|
||||
const String& name() const { return m_name; }
|
||||
const String& value() const { return m_value; }
|
||||
|
||||
void set_value(const String& value) { m_value = value; }
|
||||
|
||||
private:
|
||||
String m_name;
|
||||
String m_value;
|
||||
};
|
||||
|
||||
class Element : public ParentNode {
|
||||
public:
|
||||
explicit Element(const String& tag_name);
|
||||
virtual ~Element() override;
|
||||
|
||||
const String& tag_name() const { return m_tag_name; }
|
||||
|
||||
String attribute(const String& name) const;
|
||||
void set_attribute(const String& name, const String& value);
|
||||
|
||||
void set_attributes(Vector<Attribute>&&);
|
||||
|
||||
template<typename Callback>
|
||||
void for_each_attribute(Callback callback) const
|
||||
{
|
||||
for (auto& attribute : m_attributes)
|
||||
callback(attribute.name(), attribute.value());
|
||||
}
|
||||
|
||||
virtual RetainPtr<LayoutNode> create_layout_node() override;
|
||||
|
||||
private:
|
||||
Attribute* find_attribute(const String& name);
|
||||
const Attribute* find_attribute(const String& name) const;
|
||||
|
||||
String m_tag_name;
|
||||
Vector<Attribute> m_attributes;
|
||||
};
|
||||
|
34
LibHTML/DOM/Node.cpp
Normal file
34
LibHTML/DOM/Node.cpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
#include <LibHTML/DOM/Node.h>
|
||||
#include <LibHTML/Layout/LayoutNode.h>
|
||||
|
||||
Node::Node(NodeType type)
|
||||
: m_type(type)
|
||||
{
|
||||
}
|
||||
|
||||
Node::~Node()
|
||||
{
|
||||
}
|
||||
|
||||
void Node::retain()
|
||||
{
|
||||
ASSERT(m_retain_count);
|
||||
++m_retain_count;
|
||||
}
|
||||
|
||||
void Node::release()
|
||||
{
|
||||
ASSERT(m_retain_count);
|
||||
if (!--m_retain_count)
|
||||
delete this;
|
||||
}
|
||||
|
||||
RetainPtr<LayoutNode> Node::create_layout_node()
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void Node::set_layout_node(Retained<LayoutNode> layout_node)
|
||||
{
|
||||
m_layout_node = move(layout_node);
|
||||
}
|
60
LibHTML/DOM/Node.h
Normal file
60
LibHTML/DOM/Node.h
Normal file
|
@ -0,0 +1,60 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Badge.h>
|
||||
#include <AK/RetainPtr.h>
|
||||
#include <AK/Vector.h>
|
||||
|
||||
enum class NodeType : unsigned {
|
||||
INVALID = 0,
|
||||
ELEMENT_NODE = 1,
|
||||
TEXT_NODE = 3,
|
||||
DOCUMENT_NODE = 9,
|
||||
};
|
||||
|
||||
class LayoutNode;
|
||||
class ParentNode;
|
||||
|
||||
class Node {
|
||||
public:
|
||||
virtual ~Node();
|
||||
|
||||
void retain();
|
||||
void release();
|
||||
int retain_count() const { return m_retain_count; }
|
||||
|
||||
ParentNode* parent_node() { return m_parent_node; }
|
||||
const ParentNode* parent_node() const { return m_parent_node; }
|
||||
|
||||
void set_parent_node(Badge<ParentNode>, ParentNode* parent_node) { m_parent_node = parent_node; }
|
||||
|
||||
NodeType type() const { return m_type; }
|
||||
bool is_element() const { return type() == NodeType::ELEMENT_NODE; }
|
||||
bool is_text() const { return type() == NodeType::TEXT_NODE; }
|
||||
bool is_document() const { return type() == NodeType::DOCUMENT_NODE; }
|
||||
bool is_parent_node() const { return is_element() || is_document(); }
|
||||
|
||||
Node* next_sibling() { return m_next_sibling; }
|
||||
Node* previous_sibling() { return m_previous_sibling; }
|
||||
const Node* next_sibling() const { return m_next_sibling; }
|
||||
const Node* previous_sibling() const { return m_previous_sibling; }
|
||||
|
||||
void set_next_sibling(Node* node) { m_next_sibling = node; }
|
||||
void set_previous_sibling(Node* node) { m_previous_sibling = node; }
|
||||
|
||||
virtual RetainPtr<LayoutNode> create_layout_node();
|
||||
|
||||
const LayoutNode* layout_node() const { return m_layout_node; }
|
||||
LayoutNode* layout_node() { return m_layout_node; }
|
||||
|
||||
void set_layout_node(Retained<LayoutNode>);
|
||||
|
||||
protected:
|
||||
explicit Node(NodeType);
|
||||
|
||||
int m_retain_count { 1 };
|
||||
NodeType m_type { NodeType::INVALID };
|
||||
ParentNode* m_parent_node { nullptr };
|
||||
Node* m_next_sibling { nullptr };
|
||||
Node* m_previous_sibling { nullptr };
|
||||
RetainPtr<LayoutNode> m_layout_node;
|
||||
};
|
11
LibHTML/DOM/ParentNode.cpp
Normal file
11
LibHTML/DOM/ParentNode.cpp
Normal file
|
@ -0,0 +1,11 @@
|
|||
#include <LibHTML/DOM/ParentNode.h>
|
||||
|
||||
void ParentNode::append_child(Retained<Node> node)
|
||||
{
|
||||
if (m_last_child)
|
||||
m_last_child->set_next_sibling(node.ptr());
|
||||
node->set_parent_node({}, this);
|
||||
m_last_child = &node.leak_ref();
|
||||
if (!m_first_child)
|
||||
m_first_child = m_last_child;
|
||||
}
|
40
LibHTML/DOM/ParentNode.h
Normal file
40
LibHTML/DOM/ParentNode.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
#pragma once
|
||||
|
||||
#include <LibHTML/DOM/Node.h>
|
||||
|
||||
class ParentNode : public Node {
|
||||
public:
|
||||
void append_child(Retained<Node>);
|
||||
|
||||
Node* first_child() { return m_first_child; }
|
||||
Node* last_child() { return m_last_child; }
|
||||
const Node* first_child() const { return m_first_child; }
|
||||
const Node* last_child() const { return m_last_child; }
|
||||
|
||||
template<typename F> void for_each_child(F) const;
|
||||
template<typename F> void for_each_child(F);
|
||||
|
||||
protected:
|
||||
explicit ParentNode(NodeType type)
|
||||
: Node(type)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
Node* m_first_child { nullptr };
|
||||
Node* m_last_child { nullptr };
|
||||
};
|
||||
|
||||
template<typename Callback>
|
||||
inline void ParentNode::for_each_child(Callback callback) const
|
||||
{
|
||||
for (auto* node = first_child(); node; node = node->next_sibling())
|
||||
callback(*node);
|
||||
}
|
||||
|
||||
template<typename Callback>
|
||||
inline void ParentNode::for_each_child(Callback callback)
|
||||
{
|
||||
for (auto* node = first_child(); node; node = node->next_sibling())
|
||||
callback(*node);
|
||||
}
|
17
LibHTML/DOM/Text.cpp
Normal file
17
LibHTML/DOM/Text.cpp
Normal file
|
@ -0,0 +1,17 @@
|
|||
#include <LibHTML/DOM/Text.h>
|
||||
#include <LibHTML/Layout/LayoutText.h>
|
||||
|
||||
Text::Text(const String& data)
|
||||
: Node(NodeType::TEXT_NODE)
|
||||
, m_data(data)
|
||||
{
|
||||
}
|
||||
|
||||
Text::~Text()
|
||||
{
|
||||
}
|
||||
|
||||
RetainPtr<LayoutNode> Text::create_layout_node()
|
||||
{
|
||||
return adopt(*new LayoutText(*this));
|
||||
}
|
17
LibHTML/DOM/Text.h
Normal file
17
LibHTML/DOM/Text.h
Normal file
|
@ -0,0 +1,17 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/AKString.h>
|
||||
#include <LibHTML/DOM/Node.h>
|
||||
|
||||
class Text final : public Node {
|
||||
public:
|
||||
explicit Text(const String&);
|
||||
virtual ~Text() override;
|
||||
|
||||
const String& data() const { return m_data; }
|
||||
|
||||
virtual RetainPtr<LayoutNode> create_layout_node() override;
|
||||
|
||||
private:
|
||||
String m_data;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue