LibWeb: Slightly delay queueing a character data mutation event

For DevTools, we will want to forward mutation events to the UI in order
to inform the DevTools client about changed DOM nodes. The API for this
requires the new values associated with the events; for example, for
character data events, this will be the node's new text data.

This patch moves the queueing of the mutation record until after we have
the new character data stored. This is not observable.
This commit is contained in:
Timothy Flynn 2025-03-06 17:17:20 -05:00 committed by Andreas Kling
parent ddea67034f
commit 3847d64542
Notes: github-actions[bot] 2025-03-08 00:27:56 +00:00

View file

@ -80,9 +80,6 @@ WebIDL::ExceptionOr<void> CharacterData::replace_data(size_t offset, size_t coun
if (offset + count > length)
count = length - offset;
// 4. Queue a mutation record of "characterData" for node with null, null, nodes data, « », « », null, and null.
queue_mutation_record(MutationType::characterData, {}, {}, m_data, {}, {}, nullptr, nullptr);
// 5. Insert data into nodes data after offset code units.
// 6. Let delete offset be offset + datas length.
// 7. Starting from delete offset code units, remove count code units from nodes data.
@ -97,12 +94,17 @@ WebIDL::ExceptionOr<void> CharacterData::replace_data(size_t offset, size_t coun
Utf16View full_view { full_data };
bool characters_are_the_same = utf16_view == full_view;
auto old_data = m_data;
// OPTIMIZATION: Skip UTF-8 encoding if the characters are the same.
if (!characters_are_the_same) {
m_data = MUST(full_view.to_utf8(Utf16View::AllowInvalidCodeUnits::Yes));
}
// 4. Queue a mutation record of "characterData" for node with null, null, nodes data, « », « », null, and null.
// NOTE: We do this later so that the mutation observer may notify UI clients of this node's new value.
queue_mutation_record(MutationType::characterData, {}, {}, old_data, {}, {}, nullptr, nullptr);
// 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))