LibWeb: Calculate length for all CharacterData type nodes correctly

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.
This commit is contained in:
Tim Ledbetter 2024-07-23 21:55:48 +01:00 committed by Sam Atkins
parent f8b1e96e2b
commit 3802d9ccc4
Notes: github-actions[bot] 2024-07-25 14:58:20 +00:00
3 changed files with 14 additions and 1 deletions

View File

@ -0,0 +1 @@
range start offset: 0, end offset: 3

View File

@ -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>

View File

@ -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>
@ -69,7 +70,7 @@ public:
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(); }