Skip to content

Commit dc74786

Browse files
committed
LibWeb: Make Node::is_text() return true for CDATASection nodes
CDATASection inherits from Text, and so it was incorrect for them to claim not to be Text nodes. This fixes at least two WPT subtests. :^) It also exposed a bug in the DOM Parsing and Serialization spec, where we're not told how to serialize CDATASection nodes. Spec bug: w3c/DOM-Parsing#38
1 parent 93bc8f3 commit dc74786

File tree

5 files changed

+2545
-7
lines changed

5 files changed

+2545
-7
lines changed

Libraries/LibWeb/DOM/Node.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class Node : public EventTarget {
110110

111111
NodeType type() const { return m_type; }
112112
bool is_element() const { return type() == NodeType::ELEMENT_NODE; }
113-
bool is_text() const { return type() == NodeType::TEXT_NODE; }
113+
bool is_text() const { return type() == NodeType::TEXT_NODE || type() == NodeType::CDATA_SECTION_NODE; }
114114
bool is_document() const { return type() == NodeType::DOCUMENT_NODE; }
115115
bool is_document_type() const { return type() == NodeType::DOCUMENT_TYPE_NODE; }
116116
bool is_comment() const { return type() == NodeType::COMMENT_NODE; }

Libraries/LibWeb/DOMParsing/XMLSerializer.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ WebIDL::ExceptionOr<String> serialize_node_to_xml_string_impl(GC::Ref<DOM::Node
196196
return serialize_comment(static_cast<DOM::Comment const&>(*root), require_well_formed);
197197
}
198198

199+
// NOTE: CDATASection comes before Text since CDATASection is a subclass of Text.
200+
if (is<DOM::CDATASection>(*root)) {
201+
// Note: Serialization of CDATASection nodes is not mentioned in the specification, but treating CDATASection nodes as
202+
// text leads to incorrect serialization.
203+
// Spec bug: https://github.com/w3c/DOM-Parsing/issues/38
204+
return serialize_cdata_section(static_cast<DOM::CDATASection const&>(*root), require_well_formed);
205+
}
206+
199207
if (is<DOM::Text>(*root)) {
200208
// -> Text
201209
// Run the algorithm for XML serializing a Text node node.
@@ -220,12 +228,6 @@ WebIDL::ExceptionOr<String> serialize_node_to_xml_string_impl(GC::Ref<DOM::Node
220228
return serialize_processing_instruction(static_cast<DOM::ProcessingInstruction const&>(*root), require_well_formed);
221229
}
222230

223-
if (is<DOM::CDATASection>(*root)) {
224-
// Note: Serialization of CDATASection nodes is not mentioned in the specification, but treating CDATASection nodes as
225-
// text leads to incorrect serialization.
226-
return serialize_cdata_section(static_cast<DOM::CDATASection const&>(*root), require_well_formed);
227-
}
228-
229231
if (is<DOM::Attr>(*root)) {
230232
// -> An Attr object
231233
// Return an empty string.

0 commit comments

Comments
 (0)