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

LibHTML: Add basic <!DOCTYPE> parsing and a DocumentType class

Plus, Document::fixup() will now make sure that the document always
starts with a doctype node, followed by an <html> element.
This commit is contained in:
Andreas Kling 2019-10-09 20:17:01 +02:00
parent 850955053f
commit fc53867937
9 changed files with 84 additions and 6 deletions

View file

@ -2,6 +2,7 @@
#include <AK/StringBuilder.h>
#include <LibHTML/CSS/StyleResolver.h>
#include <LibHTML/DOM/Document.h>
#include <LibHTML/DOM/DocumentType.h>
#include <LibHTML/DOM/Element.h>
#include <LibHTML/DOM/HTMLBodyElement.h>
#include <LibHTML/DOM/HTMLHeadElement.h>
@ -29,11 +30,14 @@ StyleResolver& Document::style_resolver()
void Document::fixup()
{
if (is<HTMLHtmlElement>(first_child()))
if (!is<DocumentType>(first_child()))
prepend_child(adopt(*new DocumentType(*this)));
if (is<HTMLHtmlElement>(first_child()->next_sibling()))
return;
NonnullRefPtr<Element> body = adopt(*new Element(*this, "body"));
NonnullRefPtr<Element> html = adopt(*new Element(*this, "html"));
auto body = adopt(*new HTMLBodyElement(*this, "body"));
auto html = adopt(*new HTMLHtmlElement(*this, "html"));
html->append_child(body);
this->donate_all_children_to(body);
this->append_child(html);

View file

@ -0,0 +1,10 @@
#include <LibHTML/DOM/DocumentType.h>
DocumentType::DocumentType(Document& document)
: Node(document, NodeType::DOCUMENT_TYPE_NODE)
{
}
DocumentType::~DocumentType()
{
}

View file

@ -0,0 +1,17 @@
#pragma once
#include <LibHTML/DOM/Node.h>
class DocumentType final : public Node {
public:
explicit DocumentType(Document&);
virtual ~DocumentType() override;
virtual String tag_name() const override { return "!DOCTYPE"; }
};
template<>
inline bool is<DocumentType>(const Node& node)
{
return node.type() == NodeType::DOCUMENT_TYPE_NODE;
}

View file

@ -97,3 +97,8 @@ const Element* Node::previous_element_sibling() const
}
return nullptr;
}
RefPtr<LayoutNode> Node::create_layout_node(const StyleResolver&, const StyleProperties*) const
{
return nullptr;
}

View file

@ -11,6 +11,7 @@ enum class NodeType : unsigned {
ELEMENT_NODE = 1,
TEXT_NODE = 3,
DOCUMENT_NODE = 9,
DOCUMENT_TYPE_NODE = 10,
};
class Document;
@ -30,9 +31,10 @@ public:
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_document_type() const { return type() == NodeType::DOCUMENT_TYPE_NODE; }
bool is_parent_node() const { return is_element() || is_document(); }
virtual RefPtr<LayoutNode> create_layout_node(const StyleResolver&, const StyleProperties* parent_style) const = 0;
virtual RefPtr<LayoutNode> create_layout_node(const StyleResolver&, const StyleProperties* parent_style) const;
RefPtr<LayoutNode> create_layout_tree(const StyleResolver&, const StyleProperties* parent_style) const;
virtual String tag_name() const = 0;