summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/DOM
diff options
context:
space:
mode:
Diffstat (limited to 'Userland/Libraries/LibWeb/DOM')
-rw-r--r--Userland/Libraries/LibWeb/DOM/CharacterData.cpp19
-rw-r--r--Userland/Libraries/LibWeb/DOM/CharacterData.h2
-rw-r--r--Userland/Libraries/LibWeb/DOM/CharacterData.idl2
3 files changed, 23 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/CharacterData.cpp b/Userland/Libraries/LibWeb/DOM/CharacterData.cpp
index 61016446af..fd08c3cc96 100644
--- a/Userland/Libraries/LibWeb/DOM/CharacterData.cpp
+++ b/Userland/Libraries/LibWeb/DOM/CharacterData.cpp
@@ -26,4 +26,23 @@ void CharacterData::set_data(String data)
document().set_needs_layout();
}
+// https://dom.spec.whatwg.org/#concept-cd-substring
+ExceptionOr<String> CharacterData::substring_data(size_t offset, size_t count) const
+{
+ // 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("Substring offset out of range.");
+
+ // 3. If offset plus count is greater than length, return a string whose value is the code units from the offsetth code unit
+ // to the end of node’s data, and then return.
+ if (offset + count > length)
+ return m_data.substring(offset);
+
+ // 4. Return a string whose value is the code units from the offsetth code unit to the offset+countth code unit in node’s data.
+ return m_data.substring(offset, count);
+}
+
}
diff --git a/Userland/Libraries/LibWeb/DOM/CharacterData.h b/Userland/Libraries/LibWeb/DOM/CharacterData.h
index 329adbbea6..051c45d9b5 100644
--- a/Userland/Libraries/LibWeb/DOM/CharacterData.h
+++ b/Userland/Libraries/LibWeb/DOM/CharacterData.h
@@ -27,6 +27,8 @@ public:
unsigned length() const { return m_data.length(); }
+ ExceptionOr<String> substring_data(size_t offset, size_t count) 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 a9c7590526..9f505b3ab9 100644
--- a/Userland/Libraries/LibWeb/DOM/CharacterData.idl
+++ b/Userland/Libraries/LibWeb/DOM/CharacterData.idl
@@ -6,6 +6,8 @@ interface CharacterData : Node {
[LegacyNullToEmptyString] attribute DOMString data;
readonly attribute unsigned long length;
+ DOMString substringData(unsigned long offset, unsigned long count);
+
readonly attribute Element? nextElementSibling;
readonly attribute Element? previousElementSibling;