CharacterData.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/CharacterData.h>
  7. #include <LibWeb/DOM/Document.h>
  8. namespace Web::DOM {
  9. CharacterData::CharacterData(Document& document, NodeType type, const String& data)
  10. : Node(document, type)
  11. , m_data(data)
  12. {
  13. }
  14. void CharacterData::set_data(String data)
  15. {
  16. if (m_data == data)
  17. return;
  18. m_data = move(data);
  19. if (parent())
  20. parent()->children_changed();
  21. set_needs_style_update(true);
  22. document().set_needs_layout();
  23. }
  24. // https://dom.spec.whatwg.org/#concept-cd-substring
  25. ExceptionOr<String> CharacterData::substring_data(size_t offset, size_t count) const
  26. {
  27. // 1. Let length be node’s length.
  28. auto length = this->length();
  29. // 2. If offset is greater than length, then throw an "IndexSizeError" DOMException.
  30. if (offset > length)
  31. return DOM::IndexSizeError::create("Substring offset out of range.");
  32. // 3. If offset plus count is greater than length, return a string whose value is the code units from the offsetth code unit
  33. // to the end of node’s data, and then return.
  34. if (offset + count > length)
  35. return m_data.substring(offset);
  36. // 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.
  37. return m_data.substring(offset, count);
  38. }
  39. }