summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2022-03-21 18:05:20 +0100
committerAndreas Kling <kling@serenityos.org>2022-03-21 18:05:20 +0100
commit24e25fe3d0f22e2bec8a830faf2ac88293d85c9f (patch)
treec302769e07e341ba265e21ce15258ab65ee2d6da /Userland/Libraries
parente50c7de1b2a2b2b295dddc4bf5db0c1ae211eb19 (diff)
downloadserenity-24e25fe3d0f22e2bec8a830faf2ac88293d85c9f.zip
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.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibWeb/DOM/CharacterData.cpp40
-rw-r--r--Userland/Libraries/LibWeb/DOM/CharacterData.h1
-rw-r--r--Userland/Libraries/LibWeb/DOM/CharacterData.idl1
3 files changed, 42 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/CharacterData.cpp b/Userland/Libraries/LibWeb/DOM/CharacterData.cpp
index fd08c3cc96..a01b36e029 100644
--- a/Userland/Libraries/LibWeb/DOM/CharacterData.cpp
+++ b/Userland/Libraries/LibWeb/DOM/CharacterData.cpp
@@ -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 {};
+}
+
}
diff --git a/Userland/Libraries/LibWeb/DOM/CharacterData.h b/Userland/Libraries/LibWeb/DOM/CharacterData.h
index 051c45d9b5..0b38ec92a0 100644
--- a/Userland/Libraries/LibWeb/DOM/CharacterData.h
+++ b/Userland/Libraries/LibWeb/DOM/CharacterData.h
@@ -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&);
diff --git a/Userland/Libraries/LibWeb/DOM/CharacterData.idl b/Userland/Libraries/LibWeb/DOM/CharacterData.idl
index 9f505b3ab9..15b7af0825 100644
--- a/Userland/Libraries/LibWeb/DOM/CharacterData.idl
+++ b/Userland/Libraries/LibWeb/DOM/CharacterData.idl
@@ -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;