LibWeb: Listen for CDATASections and ProcessingInstructions in XML docs

Using the new hooks in the XML Parser's listener interface, we now
append DOM nodes for CDATASections and ProcessingInstructions
to the document as they are encountered. This commit also fixes where
comment nodes are appended, ensuring they are added to the current node
instead of the document root.
This commit is contained in:
Andrew Kaster 2025-07-18 12:35:14 -06:00 committed by Ali Mohammad Pur
commit 9d93d37644
Notes: github-actions[bot] 2025-07-19 12:57:37 +00:00
2 changed files with 25 additions and 2 deletions

View file

@ -4,8 +4,10 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/DOM/CDATASection.h>
#include <LibWeb/DOM/DocumentType.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/ProcessingInstruction.h>
#include <LibWeb/HTML/HTMLTemplateElement.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
@ -260,9 +262,28 @@ void XMLDocumentBuilder::text(StringView data)
void XMLDocumentBuilder::comment(StringView data)
{
if (m_has_error)
if (m_has_error || !m_current_node)
return;
MUST(m_document->append_child(m_document->create_comment(MUST(String::from_utf8(data)))));
MUST(m_current_node->append_child(m_document->create_comment(MUST(String::from_utf8(data)))));
}
void XMLDocumentBuilder::cdata_section(StringView data)
{
if (m_has_error || !m_current_node)
return;
auto section = MUST(m_document->create_cdata_section(MUST(String::from_utf8(data))));
MUST(m_current_node->append_child(section));
}
void XMLDocumentBuilder::processing_instruction(StringView target, StringView data)
{
if (m_has_error || !m_current_node)
return;
auto processing_instruction = MUST(m_document->create_processing_instruction(MUST(String::from_utf8(target)), MUST(String::from_utf8(data))));
MUST(m_current_node->append_child(processing_instruction));
}
void XMLDocumentBuilder::document_end()

View file

@ -36,6 +36,8 @@ private:
virtual void element_end(XML::Name const& name) override;
virtual void text(StringView data) override;
virtual void comment(StringView data) override;
virtual void cdata_section(StringView data) override;
virtual void processing_instruction(StringView target, StringView data) override;
virtual void document_end() override;
Optional<FlyString> namespace_for_name(XML::Name const&);