LibWeb: Use correct factory function when cloning a Document node

Cloning an XMLDocument should produce a new XMLDocument. Same for
HTMLDocument.

This fixes at least one WPT test, which we're also importing. :^)
This commit is contained in:
Andreas Kling 2024-11-19 15:16:14 +01:00 committed by Tim Ledbetter
parent 24a6fd3d76
commit 564dc0a434
Notes: github-actions[bot] 2024-11-19 19:25:40 +00:00
3 changed files with 51 additions and 1 deletions

View file

@ -32,8 +32,10 @@
#include <LibWeb/DOM/Range.h>
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/DOM/StaticNodeList.h>
#include <LibWeb/DOM/XMLDocument.h>
#include <LibWeb/HTML/CustomElements/CustomElementReactionNames.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/HTML/HTMLDocument.h>
#include <LibWeb/HTML/HTMLImageElement.h>
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/HTML/HTMLSelectElement.h>
@ -1021,7 +1023,16 @@ WebIDL::ExceptionOr<GC::Ref<Node>> Node::clone_node(Document* document, bool clo
else if (is<Document>(this)) {
// Document
auto document_ = verify_cast<Document>(this);
auto document_copy = Document::create(this->realm(), document_->url());
auto document_copy = [&] -> GC::Ref<Document> {
switch (document_->document_type()) {
case Document::Type::XML:
return XMLDocument::create(realm(), document_->url());
case Document::Type::HTML:
return HTML::HTMLDocument::create(realm(), document_->url());
default:
return Document::create(realm(), document_->url());
}
}();
// Set copys encoding, content type, URL, origin, type, and mode to those of node.
document_copy->set_encoding(document_->encoding());

View file

@ -0,0 +1,11 @@
Summary
Harness status: OK
Rerun
Found 1 tests
1 Pass
Details
Result Test Name MessagePass Created with createDocument

View file

@ -0,0 +1,28 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Cloning of an XMLDocument</title>
<link rel="help" href="https://dom.spec.whatwg.org/#dom-node-clonenode">
<link rel="help" href="https://dom.spec.whatwg.org/#concept-node-clone">
<!-- This is testing in particular "that implements the same interfaces as node" -->
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
"use strict";
test(() => {
const doc = document.implementation.createDocument("namespace", "");
assert_equals(
doc.constructor, XMLDocument,
"Precondition check: document.implementation.createDocument() creates an XMLDocument"
);
const clone = doc.cloneNode(true);
assert_equals(clone.constructor, XMLDocument);
}, "Created with createDocument");
</script>