Skip to content

Commit b99a3ec

Browse files
trflynn89awesomekling
authored andcommitted
LibWeb: Clone CDATASection nodes with the correct node type
We were cloning these as plain Text nodes, but the clone must also be a CDATASection node.
1 parent 74b27d6 commit b99a3ec

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

Libraries/LibWeb/DOM/Node.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,15 +1057,21 @@ WebIDL::ExceptionOr<GC::Ref<Node>> Node::clone_node(Document* document, bool clo
10571057
// Set copy’s namespace, namespace prefix, local name, and value to those of node.
10581058
auto& attr = static_cast<Attr&>(*this);
10591059
copy = attr.clone(*document);
1060-
}
1061-
// NOTE: is<Text>() currently returns true only for text nodes, not for descendant types of Text.
1062-
else if (is<Text>(this) || is<CDATASection>(this)) {
1060+
} else if (is<Text>(this)) {
10631061
// Text
10641062
auto& text = static_cast<Text&>(*this);
10651063

10661064
// Set copy’s data to that of node.
1067-
auto text_copy = realm().create<Text>(*document, text.data());
1068-
copy = move(text_copy);
1065+
copy = [&]() -> GC::Ref<Text> {
1066+
switch (type()) {
1067+
case NodeType::TEXT_NODE:
1068+
return realm().create<Text>(*document, text.data());
1069+
case NodeType::CDATA_SECTION_NODE:
1070+
return realm().create<CDATASection>(*document, text.data());
1071+
default:
1072+
VERIFY_NOT_REACHED();
1073+
}
1074+
}();
10691075
} else if (is<Comment>(this)) {
10701076
// Comment
10711077
auto comment = verify_cast<Comment>(this);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Cloned CDATASection node data: PASS
1+
Cloned #cdata-section node data: PASS

Tests/LibWeb/Text/input/DOM/CDATASection-cloneNode.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
const xmlDocument = new DOMParser().parseFromString(`<xml></xml>`, "application/xml");
55
const cdata = xmlDocument.createCDATASection("PASS");
66
const clone = cdata.cloneNode();
7-
println(`Cloned CDATASection node data: ${clone.data}`);
7+
println(`Cloned ${clone.nodeName} node data: ${clone.data}`);
88
});
99
</script>

0 commit comments

Comments
 (0)