LibWeb: Make XHR.response an actual XMLDocument for XML documents

Before this change, we were producing a generic DOM::Document, which
was not distinguishable from an XMLDocument by IDL interface type.
This commit is contained in:
Andreas Kling 2023-10-07 08:11:18 +02:00
parent 2b343c9508
commit 0762388709
5 changed files with 29 additions and 2 deletions

View file

@ -0,0 +1,19 @@
<script src="include.js"></script>
<script>
asyncTest((done) => {
const xhr = new XMLHttpRequest();
xhr.responseType = "document";
xhr.open("GET", "data:text/xml,<?xml version='1.0'?><lol/>", true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
let xml = xhr.responseXML;
if (xml instanceof XMLDocument)
println("PASS");
else
println("FAIL");
done();
}
};
xhr.send();
});
</script>

View file

@ -9,6 +9,11 @@
namespace Web::DOM {
JS::NonnullGCPtr<XMLDocument> XMLDocument::create(JS::Realm& realm, AK::URL const& url)
{
return realm.heap().allocate<XMLDocument>(realm, realm, url);
}
XMLDocument::XMLDocument(JS::Realm& realm, AK::URL const& url)
: Document(realm, url)
{

View file

@ -14,6 +14,7 @@ class XMLDocument final : public Document {
WEB_PLATFORM_OBJECT(XMLDocument, Document);
public:
static JS::NonnullGCPtr<XMLDocument> create(JS::Realm&, AK::URL const&);
virtual ~XMLDocument() override = default;
private:

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2023, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022-2023, Luke Wilde <lukew@serenityos.org>
* Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
@ -22,6 +22,7 @@
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/EventDispatcher.h>
#include <LibWeb/DOM/IDLEventListener.h>
#include <LibWeb/DOM/XMLDocument.h>
#include <LibWeb/Fetch/BodyInit.h>
#include <LibWeb/Fetch/Fetching/Fetching.h>
#include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
@ -317,7 +318,7 @@ void XMLHttpRequest::set_document_response()
// 6. Otherwise, let document be a document that represents the result of running the XML parser with XML scripting support disabled on xhrs received bytes. If that fails (unsupported character encoding, namespace well-formedness error, etc.), then return null.
else {
document = DOM::Document::create(realm());
document = DOM::XMLDocument::create(realm(), m_response->url().value_or({}));
if (!Web::build_xml_document(*document, m_received_bytes)) {
m_response_object = Empty {};
return;