Skip to content

Commit

Permalink
LibWeb: Make Node::is_text() return true for CDATASection nodes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
awesomekling authored and nico-engels committed Nov 23, 2024
1 parent e4858c5 commit 9384772
Show file tree
Hide file tree
Showing 5 changed files with 2,545 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Libraries/LibWeb/DOM/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class Node : public EventTarget {

NodeType type() const { return m_type; }
bool is_element() const { return type() == NodeType::ELEMENT_NODE; }
bool is_text() const { return type() == NodeType::TEXT_NODE; }
bool is_text() const { return type() == NodeType::TEXT_NODE || type() == NodeType::CDATA_SECTION_NODE; }
bool is_document() const { return type() == NodeType::DOCUMENT_NODE; }
bool is_document_type() const { return type() == NodeType::DOCUMENT_TYPE_NODE; }
bool is_comment() const { return type() == NodeType::COMMENT_NODE; }
Expand Down
14 changes: 8 additions & 6 deletions Libraries/LibWeb/DOMParsing/XMLSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ WebIDL::ExceptionOr<String> serialize_node_to_xml_string_impl(GC::Ref<DOM::Node
return serialize_comment(static_cast<DOM::Comment const&>(*root), require_well_formed);
}

// NOTE: CDATASection comes before Text since CDATASection is a subclass of Text.
if (is<DOM::CDATASection>(*root)) {
// Note: Serialization of CDATASection nodes is not mentioned in the specification, but treating CDATASection nodes as
// text leads to incorrect serialization.
// Spec bug: https://github.com/w3c/DOM-Parsing/issues/38
return serialize_cdata_section(static_cast<DOM::CDATASection const&>(*root), require_well_formed);
}

if (is<DOM::Text>(*root)) {
// -> Text
// Run the algorithm for XML serializing a Text node node.
Expand All @@ -220,12 +228,6 @@ WebIDL::ExceptionOr<String> serialize_node_to_xml_string_impl(GC::Ref<DOM::Node
return serialize_processing_instruction(static_cast<DOM::ProcessingInstruction const&>(*root), require_well_formed);
}

if (is<DOM::CDATASection>(*root)) {
// Note: Serialization of CDATASection nodes is not mentioned in the specification, but treating CDATASection nodes as
// text leads to incorrect serialization.
return serialize_cdata_section(static_cast<DOM::CDATASection const&>(*root), require_well_formed);
}

if (is<DOM::Attr>(*root)) {
// -> An Attr object
// Return an empty string.
Expand Down
Loading

0 comments on commit 9384772

Please sign in to comment.