LibWeb: Add CharacterData.replaceData(offset, count, data)

Note that we don't queue mutation records or update live ranges yet,
I've left those as FIXMEs.
This commit is contained in:
Andreas Kling 2022-03-21 18:05:20 +01:00
parent e50c7de1b2
commit 24e25fe3d0
Notes: sideshowbarker 2024-07-17 16:59:29 +09:00
3 changed files with 42 additions and 0 deletions

View file

@ -45,4 +45,44 @@ ExceptionOr<String> CharacterData::substring_data(size_t offset, size_t count) c
return m_data.substring(offset, count);
}
// https://dom.spec.whatwg.org/#concept-cd-replace
ExceptionOr<void> CharacterData::replace_data(size_t offset, size_t count, String const& data)
{
// 1. Let length be nodes length.
auto length = this->length();
// 2. If offset is greater than length, then throw an "IndexSizeError" DOMException.
if (offset > length)
return DOM::IndexSizeError::create("Replacement offset out of range.");
// 3. If offset plus count is greater than length, then set count to length minus offset.
if (offset + count > length)
count = length - offset;
// FIXME: 4. Queue a mutation record of "characterData" for node with null, null, nodes data, « », « », null, and null.
// 5. Insert data into nodes data after offset code units.
// 6. Let delete offset be offset + datas length.
// 7. Starting from delete offset code units, remove count code units from nodes data.
StringBuilder builder;
builder.append(this->data().substring_view(0, offset));
builder.append(data.characters(), count);
builder.append(this->data().substring_view(offset + count));
set_data(builder.to_string());
// FIXME: 8. For each live range whose start node is node and start offset is greater than offset but less than or equal to offset plus count, set its start offset to offset.
// FIXME: 9. For each live range whose end node is node and end offset is greater than offset but less than or equal to offset plus count, set its end offset to offset.
// FIXME: 10. For each live range whose start node is node and start offset is greater than offset plus count, increase its start offset by datas length and decrease it by count.
// FIXME: 11. For each live range whose end node is node and end offset is greater than offset plus count, increase its end offset by datas length and decrease it by count.
// 12. If nodes parent is non-null, then run the children changed steps for nodes parent.
if (parent())
parent()->children_changed();
return {};
}
}

View file

@ -28,6 +28,7 @@ public:
unsigned length() const { return m_data.length(); }
ExceptionOr<String> substring_data(size_t offset, size_t count) const;
ExceptionOr<void> replace_data(size_t offset, size_t count, String const&);
protected:
explicit CharacterData(Document&, NodeType, const String&);

View file

@ -7,6 +7,7 @@ interface CharacterData : Node {
readonly attribute unsigned long length;
DOMString substringData(unsigned long offset, unsigned long count);
undefined replaceData(unsigned long offset, unsigned long count, DOMString data);
readonly attribute Element? nextElementSibling;
readonly attribute Element? previousElementSibling;