1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:27:35 +00:00

LibWeb: Add CharacterData.substringData(offset, count)

This commit is contained in:
Andreas Kling 2022-03-21 17:20:42 +01:00
parent 394cd77467
commit e50c7de1b2
3 changed files with 23 additions and 0 deletions

View file

@ -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 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("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 nodes 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 nodes data.
return m_data.substring(offset, count);
}
}

View file

@ -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&);

View file

@ -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;