From 3847d64542eede5366e63199e9bf93ba15e7e7b1 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 6 Mar 2025 17:17:20 -0500 Subject: [PATCH] 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. --- Libraries/LibWeb/DOM/CharacterData.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Libraries/LibWeb/DOM/CharacterData.cpp b/Libraries/LibWeb/DOM/CharacterData.cpp index 8f34d27a1ed..63e84fdbde3 100644 --- a/Libraries/LibWeb/DOM/CharacterData.cpp +++ b/Libraries/LibWeb/DOM/CharacterData.cpp @@ -80,9 +80,6 @@ WebIDL::ExceptionOr 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, node’s data, « », « », null, and null. - queue_mutation_record(MutationType::characterData, {}, {}, m_data, {}, {}, nullptr, nullptr); - // 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. @@ -97,12 +94,17 @@ WebIDL::ExceptionOr 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, node’s 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))