1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:47:34 +00:00

LibWeb: Port CharacterData from DeprecatedString to String

The existing implementation has some pre-existing issues where it is
incorrectly assumes that byte offsets are given through the IDL instead
of UTF-16 code units. While making these changes, leave some FIXMEs for
that.
This commit is contained in:
Shannon Booth 2023-09-07 21:36:05 +12:00 committed by Andreas Kling
parent 3b12a13f17
commit b603e860af
18 changed files with 87 additions and 81 deletions

View file

@ -559,11 +559,11 @@ String Range::to_string() const
// 2. If thiss start node is thiss end node and it is a Text node,
// then return the substring of that Text nodes data beginning at thiss start offset and ending at thiss end offset.
if (start_container() == end_container() && is<Text>(*start_container()))
return String::from_deprecated_string(static_cast<Text const&>(*start_container()).data().substring(start_offset(), end_offset() - start_offset())).release_value();
return MUST(static_cast<Text const&>(*start_container()).data().substring_from_byte_offset(start_offset(), end_offset() - start_offset()));
// 3. If thiss start node is a Text node, then append the substring of that nodes data from thiss start offset until the end to s.
if (is<Text>(*start_container()))
builder.append(static_cast<Text const&>(*start_container()).data().substring_view(start_offset()));
builder.append(static_cast<Text const&>(*start_container()).data().bytes_as_string_view().substring_view(start_offset()));
// 4. Append the concatenation of the data of all Text nodes that are contained in this, in tree order, to s.
for (Node const* node = start_container(); node != end_container()->next_sibling(); node = node->next_in_pre_order()) {
@ -573,7 +573,7 @@ String Range::to_string() const
// 5. If thiss end node is a Text node, then append the substring of that nodes data from its start until thiss end offset to s.
if (is<Text>(*end_container()))
builder.append(static_cast<Text const&>(*end_container()).data().substring_view(0, end_offset()));
builder.append(static_cast<Text const&>(*end_container()).data().bytes_as_string_view().substring_view(0, end_offset()));
// 6. Return s.
return MUST(builder.to_string());
@ -616,7 +616,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> Range::extract()
TRY(fragment->append_child(clone));
// 4. Replace data with node original start node, offset original start offset, count original end offset minus original start offset, and data the empty string.
TRY(static_cast<CharacterData&>(*original_start_node).replace_data(original_start_offset, original_end_offset - original_start_offset, ""));
TRY(static_cast<CharacterData&>(*original_start_node).replace_data(original_start_offset, original_end_offset - original_start_offset, String {}));
// 5. Return fragment.
return fragment;
@ -706,7 +706,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> Range::extract()
TRY(fragment->append_child(clone));
// 4. Replace data with node original start node, offset original start offset, count original start nodes length minus original start offset, and data the empty string.
TRY(static_cast<CharacterData&>(*original_start_node).replace_data(original_start_offset, original_start_node->length() - original_start_offset, ""));
TRY(static_cast<CharacterData&>(*original_start_node).replace_data(original_start_offset, original_start_node->length() - original_start_offset, String {}));
}
// 16. Otherwise, if first partially contained child is not null:
else if (first_partially_contained_child) {
@ -744,7 +744,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> Range::extract()
TRY(fragment->append_child(clone));
// 4. Replace data with node original end node, offset 0, count original end offset, and data the empty string.
TRY(verify_cast<CharacterData>(*original_end_node).replace_data(0, original_end_offset, ""));
TRY(verify_cast<CharacterData>(*original_end_node).replace_data(0, original_end_offset, String {}));
}
// 19. Otherwise, if last partially contained child is not null:
else if (last_partially_contained_child) {
@ -1086,7 +1086,7 @@ WebIDL::ExceptionOr<void> Range::delete_contents()
// 3. If original start node is original end node and it is a CharacterData node, then replace data with node original start node, offset original start offset,
// count original end offset minus original start offset, and data the empty string, and then return.
if (original_start_node.ptr() == original_end_node.ptr() && is<CharacterData>(*original_start_node)) {
TRY(static_cast<CharacterData&>(*original_start_node).replace_data(original_start_offset, original_end_offset - original_start_offset, ""));
TRY(static_cast<CharacterData&>(*original_start_node).replace_data(original_start_offset, original_end_offset - original_start_offset, String {}));
return {};
}
@ -1121,7 +1121,7 @@ WebIDL::ExceptionOr<void> Range::delete_contents()
// 7. If original start node is a CharacterData node, then replace data with node original start node, offset original start offset, count original start nodes length minus original start offset, data the empty string.
if (is<CharacterData>(*original_start_node))
TRY(static_cast<CharacterData&>(*original_start_node).replace_data(original_start_offset, original_start_node->length() - original_start_offset, ""));
TRY(static_cast<CharacterData&>(*original_start_node).replace_data(original_start_offset, original_start_node->length() - original_start_offset, String {}));
// 8. For each node in nodes to remove, in tree order, remove node.
for (auto& node : nodes_to_remove)
@ -1129,7 +1129,7 @@ WebIDL::ExceptionOr<void> Range::delete_contents()
// 9. If original end node is a CharacterData node, then replace data with node original end node, offset 0, count original end offset and data the empty string.
if (is<CharacterData>(*original_end_node))
TRY(static_cast<CharacterData&>(*original_end_node).replace_data(0, original_end_offset, ""));
TRY(static_cast<CharacterData&>(*original_end_node).replace_data(0, original_end_offset, String {}));
// 10. Set start and end to (new node, new offset).
TRY(set_start(*new_node, new_offset));