LibXML: Set the doctype when parsing via Parser::parse_with_listener

This fixes at least one WPT test under /domparsing
This commit is contained in:
Florian Cramer 2024-10-30 00:07:13 +01:00 committed by Ali Mohammad Pur
parent bd25d0b1b4
commit df7cac539e
Notes: github-actions[bot] 2024-10-30 13:54:34 +00:00
8 changed files with 87 additions and 0 deletions

View file

@ -0,0 +1,3 @@
PASSED doctype.name
PASSED doctype.systemId
PASSED doctype.publicId

View file

@ -0,0 +1,3 @@
PASSED doctype.name
PASSED doctype.systemId
PASSED doctype.publicId

View file

@ -0,0 +1,25 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
test(() => {
var doc = new DOMParser().parseFromString('<!DOCTYPE foo PUBLIC "this is the public type" "this is the system type"><bar></bar>', "text/xml");
var doctype = doc.doctype;
if (doctype.name == "foo") {
println("PASSED doctype.name");
} else {
println("FAILED doctype.name");
}
if (doctype.systemId == "this is the system type") {
println("PASSED doctype.systemId");
} else {
println("FAILED doctype.systemId");
}
if (doctype.publicId == "this is the public type") {
println("PASSED doctype.publicId");
} else {
println("FAILED doctype.publicId");
}
});
</script>

View file

@ -0,0 +1,25 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
test(() => {
var doc = new DOMParser().parseFromString('<!DOCTYPE foo SYSTEM "this is the system type"><bar></bar>', "text/xml");
var doctype = doc.doctype;
if (doctype.name == "foo") {
println("PASSED doctype.name");
} else {
println("FAILED doctype.name");
}
if (doctype.systemId == "this is the system type") {
println("PASSED doctype.systemId");
} else {
println("FAILED doctype.systemId");
}
if (doctype.publicId == "") {
println("PASSED doctype.publicId");
} else {
println("FAILED doctype.publicId");
}
});
</script>

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/DOM/DocumentType.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/HTML/HTMLTemplateElement.h>
#include <LibWeb/HTML/Window.h>
@ -61,6 +62,31 @@ void XMLDocumentBuilder::set_source(ByteString source)
m_document->set_source(MUST(String::from_byte_string(source)));
}
void XMLDocumentBuilder::set_doctype(XML::Doctype doctype)
{
if (m_document->doctype()) {
return;
}
auto document_type = DOM::DocumentType::create(m_document);
auto name = MUST(AK::String::from_byte_string(doctype.type));
document_type->set_name(name);
if (doctype.external_id.has_value()) {
auto external_id = doctype.external_id.release_value();
auto system_id = MUST(AK::String::from_byte_string(external_id.system_id.system_literal));
document_type->set_system_id(system_id);
if (external_id.public_id.has_value()) {
auto public_id = MUST(AK::String::from_byte_string(external_id.public_id.release_value().public_literal));
document_type->set_public_id(public_id);
}
}
m_document->insert_before(document_type, m_document->first_child(), false);
}
void XMLDocumentBuilder::element_start(const XML::Name& name, HashMap<XML::Name, ByteString> const& attributes)
{
if (m_has_error)

View file

@ -31,6 +31,7 @@ public:
private:
virtual void set_source(ByteString) override;
virtual void set_doctype(XML::Doctype) override;
virtual void element_start(XML::Name const& name, HashMap<XML::Name, ByteString> const& attributes) override;
virtual void element_end(XML::Name const& name) override;
virtual void text(StringView data) override;

View file

@ -182,6 +182,9 @@ ErrorOr<void, ParseError> Parser::parse_with_listener(Listener& listener)
if (result.is_error())
m_listener->error(result.error());
m_listener->document_end();
if (m_doctype.has_value()) {
m_listener->set_doctype(m_doctype.release_value());
}
m_root_node.clear();
return result;
}

View file

@ -34,6 +34,7 @@ struct Listener {
virtual ~Listener() { }
virtual void set_source(ByteString) { }
virtual void set_doctype(XML::Doctype) { }
virtual void document_start() { }
virtual void document_end() { }
virtual void element_start(Name const&, HashMap<Name, ByteString> const&) { }