mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-20 19:45:12 +00:00
LibHTML: Make Node::create_layout_node() virtual
Instead of branching on the Node type, let subclasses decide how their layout nodes get constructed. This will allow elements to create custom layout nodes if they want.
This commit is contained in:
parent
09dccb3224
commit
f150134de9
Notes:
sideshowbarker
2024-07-19 11:48:31 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/f150134de9b
8 changed files with 36 additions and 27 deletions
|
@ -125,3 +125,8 @@ URL Document::complete_url(const String& string) const
|
|||
url.set_path(fspath.string());
|
||||
return url;
|
||||
}
|
||||
|
||||
RefPtr<LayoutNode> Document::create_layout_node(const StyleResolver&, const StyleProperties*) const
|
||||
{
|
||||
return adopt(*new LayoutDocument(*this, StyleProperties::create()));
|
||||
}
|
||||
|
|
|
@ -55,6 +55,8 @@ public:
|
|||
Color background_color() const;
|
||||
|
||||
private:
|
||||
virtual RefPtr<LayoutNode> create_layout_node(const StyleResolver&, const StyleProperties* parent_properties) const override;
|
||||
|
||||
OwnPtr<StyleResolver> m_style_resolver;
|
||||
NonnullRefPtrVector<StyleSheet> m_sheets;
|
||||
RefPtr<Node> m_hovered_node;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include <LibHTML/CSS/StyleResolver.h>
|
||||
#include <LibHTML/DOM/Element.h>
|
||||
#include <LibHTML/Layout/LayoutBlock.h>
|
||||
#include <LibHTML/Layout/LayoutInline.h>
|
||||
|
@ -34,7 +35,7 @@ String Element::attribute(const String& name) const
|
|||
{
|
||||
if (auto* attribute = find_attribute(name))
|
||||
return attribute->value();
|
||||
return { };
|
||||
return {};
|
||||
}
|
||||
|
||||
void Element::set_attribute(const String& name, const String& value)
|
||||
|
@ -62,3 +63,20 @@ bool Element::has_class(const StringView& class_name) const
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
RefPtr<LayoutNode> Element::create_layout_node(const StyleResolver& resolver, const StyleProperties* parent_properties) const
|
||||
{
|
||||
auto style_properties = resolver.resolve_style(*this, parent_properties);
|
||||
|
||||
auto display_property = style_properties->property("display");
|
||||
String display = display_property.has_value() ? display_property.release_value()->to_string() : "inline";
|
||||
|
||||
if (display == "none")
|
||||
return nullptr;
|
||||
if (display == "block" || display == "list-item")
|
||||
return adopt(*new LayoutBlock(this, move(style_properties)));
|
||||
if (display == "inline")
|
||||
return adopt(*new LayoutInline(*this, move(style_properties)));
|
||||
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
|
|
@ -45,6 +45,8 @@ public:
|
|||
virtual void apply_presentational_hints(StyleProperties&) const {}
|
||||
|
||||
private:
|
||||
RefPtr<LayoutNode> create_layout_node(const StyleResolver&, const StyleProperties* parent_properties) const override;
|
||||
|
||||
Attribute* find_attribute(const String& name);
|
||||
const Attribute* find_attribute(const String& name) const;
|
||||
|
||||
|
|
|
@ -19,31 +19,6 @@ Node::~Node()
|
|||
{
|
||||
}
|
||||
|
||||
RefPtr<LayoutNode> Node::create_layout_node(const StyleResolver& resolver, const StyleProperties* parent_properties) const
|
||||
{
|
||||
if (is_document())
|
||||
return adopt(*new LayoutDocument(static_cast<const Document&>(*this), StyleProperties::create()));
|
||||
|
||||
if (is_text())
|
||||
return adopt(*new LayoutText(static_cast<const Text&>(*this)));
|
||||
|
||||
ASSERT(is_element());
|
||||
|
||||
auto style_properties = resolver.resolve_style(static_cast<const Element&>(*this), parent_properties);
|
||||
|
||||
auto display_property = style_properties->property("display");
|
||||
String display = display_property.has_value() ? display_property.release_value()->to_string() : "inline";
|
||||
|
||||
if (display == "none")
|
||||
return nullptr;
|
||||
if (display == "block" || display == "list-item")
|
||||
return adopt(*new LayoutBlock(this, move(style_properties)));
|
||||
if (display == "inline")
|
||||
return adopt(*new LayoutInline(*this, move(style_properties)));
|
||||
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
RefPtr<LayoutNode> Node::create_layout_tree(const StyleResolver& resolver, const StyleProperties* parent_properties) const
|
||||
{
|
||||
auto layout_node = create_layout_node(resolver, parent_properties);
|
||||
|
|
|
@ -31,7 +31,7 @@ public:
|
|||
bool is_document() const { return type() == NodeType::DOCUMENT_NODE; }
|
||||
bool is_parent_node() const { return is_element() || is_document(); }
|
||||
|
||||
RefPtr<LayoutNode> create_layout_node(const StyleResolver&, const StyleProperties* parent_properties) const;
|
||||
virtual RefPtr<LayoutNode> create_layout_node(const StyleResolver&, const StyleProperties* parent_properties) const = 0;
|
||||
RefPtr<LayoutNode> create_layout_tree(const StyleResolver&, const StyleProperties* parent_properties) const;
|
||||
|
||||
virtual String tag_name() const = 0;
|
||||
|
|
|
@ -10,3 +10,8 @@ Text::Text(Document& document, const String& data)
|
|||
Text::~Text()
|
||||
{
|
||||
}
|
||||
|
||||
RefPtr<LayoutNode> Text::create_layout_node(const StyleResolver&, const StyleProperties*) const
|
||||
{
|
||||
return adopt(*new LayoutText(*this));
|
||||
}
|
||||
|
|
|
@ -15,5 +15,7 @@ public:
|
|||
virtual String text_content() const override { return m_data; }
|
||||
|
||||
private:
|
||||
virtual RefPtr<LayoutNode> create_layout_node(const StyleResolver&, const StyleProperties* parent_properties) const override;
|
||||
|
||||
String m_data;
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue