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:
parent
e50c7de1b2
commit
24e25fe3d0
Notes:
sideshowbarker
2024-07-17 16:59:29 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/24e25fe3d0
3 changed files with 42 additions and 0 deletions
|
@ -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 node’s 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, node’s data, « », « », null, and null.
|
||||
|
||||
// 5. Insert data into node’s data after offset code units.
|
||||
// 6. Let delete offset be offset + data’s length.
|
||||
// 7. Starting from delete offset code units, remove count code units from node’s 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 data’s 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 data’s length and decrease it by count.
|
||||
|
||||
// 12. If node’s parent is non-null, then run the children changed steps for node’s parent.
|
||||
if (parent())
|
||||
parent()->children_changed();
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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&);
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue