2020-01-18 09:38:21 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
|
*/
|
|
|
|
|
|
2020-03-07 10:32:51 +01:00
|
|
|
|
#include <LibWeb/DOM/CharacterData.h>
|
2021-02-10 18:32:16 +01:00
|
|
|
|
#include <LibWeb/DOM/Document.h>
|
2019-10-12 23:26:47 +02:00
|
|
|
|
|
2020-07-26 19:37:56 +02:00
|
|
|
|
namespace Web::DOM {
|
2020-03-07 10:27:02 +01:00
|
|
|
|
|
2019-10-12 23:26:47 +02:00
|
|
|
|
CharacterData::CharacterData(Document& document, NodeType type, const String& data)
|
|
|
|
|
: Node(document, type)
|
|
|
|
|
, m_data(data)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 18:32:16 +01:00
|
|
|
|
void CharacterData::set_data(String data)
|
|
|
|
|
{
|
|
|
|
|
if (m_data == data)
|
|
|
|
|
return;
|
|
|
|
|
m_data = move(data);
|
2022-02-25 18:40:07 +01:00
|
|
|
|
if (parent())
|
|
|
|
|
parent()->children_changed();
|
2021-10-05 22:30:53 +02:00
|
|
|
|
set_needs_style_update(true);
|
2022-03-16 14:52:35 +01:00
|
|
|
|
document().set_needs_layout();
|
2021-02-10 18:32:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-21 17:20:42 +01:00
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 10:27:02 +01:00
|
|
|
|
}
|