1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
/*
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/DOM/CharacterData.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Range.h>
namespace Web::DOM {
CharacterData::CharacterData(Document& document, NodeType type, String const& data)
: Node(document, type)
, m_data(data)
{
}
// https://dom.spec.whatwg.org/#dom-characterdata-data
void CharacterData::set_data(String data)
{
// [The data] setter must replace data with node this, offset 0, count this’s length, and data new value.
// NOTE: Since the offset is 0, it can never be above data's length, so this can never throw.
// NOTE: Setting the data to the same value as the current data still causes a mutation observer callback.
// FIXME: Figure out a way to make this a no-op again if the passed in data is the same as the current data.
MUST(replace_data(0, m_data.length(), data));
}
// 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);
}
// 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);
builder.append(this->data().substring_view(offset + count));
m_data = builder.to_string();
// 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.
for (auto& range : Range::live_ranges()) {
if (range->start_container() == this && range->start_offset() > offset && range->start_offset() <= (offset + count))
range->set_start(*range->start_container(), offset);
}
// 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.
for (auto& range : Range::live_ranges()) {
if (range->end_container() == this && range->end_offset() > offset && range->end_offset() <= (offset + count))
range->set_end(*range->end_container(), range->end_offset());
}
// 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.
for (auto& range : Range::live_ranges()) {
if (range->start_container() == this && range->start_offset() > (offset + count))
range->set_start(*range->start_container(), range->start_offset() + data.length() - count);
}
// 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.
for (auto& range : Range::live_ranges()) {
if (range->end_container() == this && range->end_offset() > (offset + count))
range->set_end(*range->end_container(), range->end_offset() + data.length() - count);
}
// 12. If node’s parent is non-null, then run the children changed steps for node’s parent.
if (parent())
parent()->children_changed();
set_needs_style_update(true);
document().set_needs_layout();
return {};
}
}
|