diff --git a/Tests/LibWeb/Text/expected/DOM/Document-createCDATASection.txt b/Tests/LibWeb/Text/expected/DOM/Document-createCDATASection.txt
new file mode 100644
index 0000000000..7b7e01bba8
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/DOM/Document-createCDATASection.txt
@@ -0,0 +1,3 @@
+ data & then some]]>
+Exception: NotSupportedError
+Exception: InvalidCharacterError
diff --git a/Tests/LibWeb/Text/input/DOM/Document-createCDATASection.html b/Tests/LibWeb/Text/input/DOM/Document-createCDATASection.html
new file mode 100644
index 0000000000..befeacf070
--- /dev/null
+++ b/Tests/LibWeb/Text/input/DOM/Document-createCDATASection.html
@@ -0,0 +1,21 @@
+
+
diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp
index 6c93dd3e08..25e60707a3 100644
--- a/Userland/Libraries/LibWeb/DOM/Document.cpp
+++ b/Userland/Libraries/LibWeb/DOM/Document.cpp
@@ -25,6 +25,7 @@
#include
#include
#include
+#include
#include
#include
#include
@@ -1473,6 +1474,21 @@ JS::NonnullGCPtr Document::create_text_node(String const& data)
return heap().allocate(realm(), *this, data);
}
+// https://dom.spec.whatwg.org/#dom-document-createcdatasection
+WebIDL::ExceptionOr> Document::create_cdata_section(String const& data)
+{
+ // 1. If this is an HTML document, then throw a "NotSupportedError" DOMException.
+ if (is_html_document())
+ return WebIDL::NotSupportedError::create(realm(), "This operation is not supported for HTML documents"_fly_string);
+
+ // 2. If data contains the string "]]>", then throw an "InvalidCharacterError" DOMException.
+ if (data.contains("]]>"sv))
+ return WebIDL::InvalidCharacterError::create(realm(), "String may not contain ']]>'"_fly_string);
+
+ // 3. Return a new CDATASection node with its data set to data and node document set to this.
+ return heap().allocate(realm(), *this, data);
+}
+
JS::NonnullGCPtr Document::create_comment(String const& data)
{
return heap().allocate(realm(), *this, data);
diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h
index 7b2fe36774..e08d5a27d8 100644
--- a/Userland/Libraries/LibWeb/DOM/Document.h
+++ b/Userland/Libraries/LibWeb/DOM/Document.h
@@ -242,6 +242,7 @@ public:
WebIDL::ExceptionOr> create_element_ns(Optional const& namespace_, String const& qualified_name, Variant const& options);
JS::NonnullGCPtr create_document_fragment();
JS::NonnullGCPtr create_text_node(String const& data);
+ WebIDL::ExceptionOr> create_cdata_section(String const& data);
JS::NonnullGCPtr create_comment(String const& data);
WebIDL::ExceptionOr> create_processing_instruction(String const& target, String const& data);
diff --git a/Userland/Libraries/LibWeb/DOM/Document.idl b/Userland/Libraries/LibWeb/DOM/Document.idl
index a964941768..5054da003d 100644
--- a/Userland/Libraries/LibWeb/DOM/Document.idl
+++ b/Userland/Libraries/LibWeb/DOM/Document.idl
@@ -1,5 +1,6 @@
#import
#import
+#import
#import
#import
#import
@@ -80,6 +81,7 @@ interface Document : Node {
[CEReactions, NewObject] Element createElementNS([FlyString] DOMString? namespace, DOMString qualifiedName, optional (DOMString or ElementCreationOptions) options = {});
DocumentFragment createDocumentFragment();
Text createTextNode(DOMString data);
+ [NewObject] CDATASection createCDATASection(DOMString data);
Comment createComment(DOMString data);
[NewObject] ProcessingInstruction createProcessingInstruction(DOMString target, DOMString data);
diff --git a/Userland/Libraries/LibWeb/DOM/Node.cpp b/Userland/Libraries/LibWeb/DOM/Node.cpp
index ef4f941e0c..00e7762e66 100644
--- a/Userland/Libraries/LibWeb/DOM/Node.cpp
+++ b/Userland/Libraries/LibWeb/DOM/Node.cpp
@@ -15,6 +15,7 @@
#include
#include
#include
+#include
#include
#include
#include
@@ -360,7 +361,7 @@ WebIDL::ExceptionOr Node::ensure_pre_insertion_validity(JS::NonnullGCPtr(*node) && !is(*node) && !is(*node) && !is(*node) && !is(*node) && !is(*node))
+ if (!is(*node) && !is(*node) && !is(*node) && !is(*node) && !is(*node) && !is(*node) && !is(*node))
return WebIDL::HierarchyRequestError::create(realm(), "Invalid node type for insertion"_fly_string);
// 5. If either node is a Text node and parent is a document, or node is a doctype and parent is not a document, then throw a "HierarchyRequestError" DOMException.