diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-10-05 22:27:52 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-10-05 23:29:01 +0200 |
commit | f150134de9b1bd8200359ffd31cefaa822faf7d0 (patch) | |
tree | 0769a853f885bac5551f2c338ac7b24043c57398 /Libraries/LibHTML/DOM | |
parent | 09dccb32245485e626048edfec484c52de145007 (diff) | |
download | serenity-f150134de9b1bd8200359ffd31cefaa822faf7d0.zip |
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.
Diffstat (limited to 'Libraries/LibHTML/DOM')
-rw-r--r-- | Libraries/LibHTML/DOM/Document.cpp | 5 | ||||
-rw-r--r-- | Libraries/LibHTML/DOM/Document.h | 2 | ||||
-rw-r--r-- | Libraries/LibHTML/DOM/Element.cpp | 20 | ||||
-rw-r--r-- | Libraries/LibHTML/DOM/Element.h | 2 | ||||
-rw-r--r-- | Libraries/LibHTML/DOM/Node.cpp | 25 | ||||
-rw-r--r-- | Libraries/LibHTML/DOM/Node.h | 2 | ||||
-rw-r--r-- | Libraries/LibHTML/DOM/Text.cpp | 5 | ||||
-rw-r--r-- | Libraries/LibHTML/DOM/Text.h | 2 |
8 files changed, 36 insertions, 27 deletions
diff --git a/Libraries/LibHTML/DOM/Document.cpp b/Libraries/LibHTML/DOM/Document.cpp index 4cdceb6069..eaf005eb2a 100644 --- a/Libraries/LibHTML/DOM/Document.cpp +++ b/Libraries/LibHTML/DOM/Document.cpp @@ -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())); +} diff --git a/Libraries/LibHTML/DOM/Document.h b/Libraries/LibHTML/DOM/Document.h index c66ebc6afa..7a13f813c6 100644 --- a/Libraries/LibHTML/DOM/Document.h +++ b/Libraries/LibHTML/DOM/Document.h @@ -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; diff --git a/Libraries/LibHTML/DOM/Element.cpp b/Libraries/LibHTML/DOM/Element.cpp index 780dc1b08e..c60319c075 100644 --- a/Libraries/LibHTML/DOM/Element.cpp +++ b/Libraries/LibHTML/DOM/Element.cpp @@ -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(); +} diff --git a/Libraries/LibHTML/DOM/Element.h b/Libraries/LibHTML/DOM/Element.h index c01b44d29c..a45f09ba4b 100644 --- a/Libraries/LibHTML/DOM/Element.h +++ b/Libraries/LibHTML/DOM/Element.h @@ -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; diff --git a/Libraries/LibHTML/DOM/Node.cpp b/Libraries/LibHTML/DOM/Node.cpp index 5bfa99895f..700a26bcd7 100644 --- a/Libraries/LibHTML/DOM/Node.cpp +++ b/Libraries/LibHTML/DOM/Node.cpp @@ -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); diff --git a/Libraries/LibHTML/DOM/Node.h b/Libraries/LibHTML/DOM/Node.h index 6c60717670..e43aa3b2f0 100644 --- a/Libraries/LibHTML/DOM/Node.h +++ b/Libraries/LibHTML/DOM/Node.h @@ -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; diff --git a/Libraries/LibHTML/DOM/Text.cpp b/Libraries/LibHTML/DOM/Text.cpp index 6b41f7c8f8..a066a767f6 100644 --- a/Libraries/LibHTML/DOM/Text.cpp +++ b/Libraries/LibHTML/DOM/Text.cpp @@ -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)); +} diff --git a/Libraries/LibHTML/DOM/Text.h b/Libraries/LibHTML/DOM/Text.h index adc5fcb265..46e29ba0c6 100644 --- a/Libraries/LibHTML/DOM/Text.h +++ b/Libraries/LibHTML/DOM/Text.h @@ -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; }; |