summaryrefslogtreecommitdiff
path: root/Libraries/LibHTML/DOM/Document.cpp
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-10-12 23:26:47 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-10-12 23:34:05 +0200
commitb083a233d8482ed7106d7a102866ee0dd42acc94 (patch)
treed32b52a7cc55fcd66dcaa94bb51f01e4bd04f398 /Libraries/LibHTML/DOM/Document.cpp
parent6d150df58a0b64c4cb583fc374fd05e60d387dc6 (diff)
downloadserenity-b083a233d8482ed7106d7a102866ee0dd42acc94.zip
LibHTML: Add Comment and CharacterData nodes and improve HTML parsing
This patch adds the CharacterData subclass of Node, which is now the parent class of Text and a new Comment class. A Comment node is one of these in HTML: <!--hello friends--> Since these occur somewhat frequently on the web, we need to be able to parse them. This patch also adds a child rejection mechanism to the DOM tree. Nodes can now override is_child_allowed(Node) and return false if they don't want a particular Node to become a child of theirs. This is used to prevent Document from taking on unwanted children.
Diffstat (limited to 'Libraries/LibHTML/DOM/Document.cpp')
-rw-r--r--Libraries/LibHTML/DOM/Document.cpp17
1 files changed, 17 insertions, 0 deletions
diff --git a/Libraries/LibHTML/DOM/Document.cpp b/Libraries/LibHTML/DOM/Document.cpp
index 8b4af6a0bd..4262bca329 100644
--- a/Libraries/LibHTML/DOM/Document.cpp
+++ b/Libraries/LibHTML/DOM/Document.cpp
@@ -29,6 +29,23 @@ StyleResolver& Document::style_resolver()
return *m_style_resolver;
}
+bool Document::is_child_allowed(const Node& node) const
+{
+ switch (node.type()) {
+ case NodeType::DOCUMENT_NODE:
+ case NodeType::TEXT_NODE:
+ return false;
+ case NodeType::COMMENT_NODE:
+ return true;
+ case NodeType::DOCUMENT_TYPE_NODE:
+ return !first_child_of_type<DocumentType>();
+ case NodeType::ELEMENT_NODE:
+ return !first_child_of_type<Element>();
+ default:
+ return false;
+ }
+}
+
void Document::fixup()
{
if (!is<DocumentType>(first_child()))