Skip to content

Commit

Permalink
LibWeb: Calculate length for all CharacterData type nodes correctly
Browse files Browse the repository at this point in the history
We now ensure that `Node::is_character_data()` returns true for all
nodes of type character data.

Previously, calling `Node::length()` on `CDataSection` or
`ProcessingInstruction` nodes would return an incorrect value.
  • Loading branch information
tcl3 committed Jul 24, 2024
1 parent e89783c commit 63e60b1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
range start offset: 0, end offset: 3
11 changes: 11 additions & 0 deletions Tests/LibWeb/Text/input/DOM/Range-containing-CDATASection.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script src="../include.js"></script>
<script>
test(() => {
const xmlDocument = new DOMParser().parseFromString(`<xml></xml>`, "application/xml");
const cdata = xmlDocument.createCDATASection("DATA");
const range = xmlDocument.createRange();
range.setStart(cdata, 0);
range.setEnd(cdata, 3);
println(`range start offset: ${range.startOffset}, end offset: ${range.endOffset}`);
});
</script>
3 changes: 2 additions & 1 deletion Userland/Libraries/LibWeb/DOM/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <AK/Badge.h>
#include <AK/FlyString.h>
#include <AK/GenericShorthands.h>
#include <AK/JsonObjectSerializer.h>
#include <AK/RefPtr.h>
#include <AK/TypeCasts.h>
Expand Down Expand Up @@ -69,7 +70,7 @@ class Node : public EventTarget {
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; }
bool is_character_data() const { return type() == NodeType::TEXT_NODE || type() == NodeType::COMMENT_NODE; }
bool is_character_data() const { return first_is_one_of(type(), NodeType::TEXT_NODE, NodeType::COMMENT_NODE, NodeType::CDATA_SECTION_NODE, NodeType::PROCESSING_INSTRUCTION_NODE); }
bool is_document_fragment() const { return type() == NodeType::DOCUMENT_FRAGMENT_NODE; }
bool is_parent_node() const { return is_element() || is_document() || is_document_fragment(); }
bool is_slottable() const { return is_element() || is_text() || is_cdata_section(); }
Expand Down

0 comments on commit 63e60b1

Please sign in to comment.