Skip to content

Commit

Permalink
LibWeb: Use correct factory function when cloning a Document node
Browse files Browse the repository at this point in the history
Cloning an XMLDocument should produce a new XMLDocument. Same for
HTMLDocument.

This fixes at least one WPT test, which we're also importing. :^)
  • Loading branch information
awesomekling committed Nov 19, 2024
1 parent 24a6fd3 commit 98b1df6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Libraries/LibWeb/DOM/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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 copy’s encoding, content type, URL, origin, type, and mode to those of node.
document_copy->set_encoding(document_->encoding());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Summary

Harness status: OK

Rerun

Found 1 tests

1 Pass
Details
Result Test Name MessagePass Created with createDocument
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 98b1df6

Please sign in to comment.