From 6c6f9936e28bd701554d01a70d9775e5e97a649d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 7 Mar 2025 21:10:16 +0100 Subject: [PATCH] LibWeb: Avoid more unnecessary relayouts on CharacterData text change If the CharacterData node has no layout node when we're changing its text, we don't need to mark the document for relayout. This is fine, because if the node ends up getting a layout node attached to it, we'll naturally perform relayout after that anyway. --- Libraries/LibWeb/DOM/CharacterData.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Libraries/LibWeb/DOM/CharacterData.cpp b/Libraries/LibWeb/DOM/CharacterData.cpp index 8b0347195c8..0c008293277 100644 --- a/Libraries/LibWeb/DOM/CharacterData.cpp +++ b/Libraries/LibWeb/DOM/CharacterData.cpp @@ -140,13 +140,16 @@ WebIDL::ExceptionOr CharacterData::replace_data(size_t offset, size_t coun if (characters_are_the_same) return {}; - // NOTE: Since the text node's data has changed, we need to invalidate the text for rendering. - // This ensures that the new text is reflected in layout, even if we don't end up - // doing a full layout tree rebuild. - if (auto* layout_node = this->layout_node(); layout_node && layout_node->is_text_node()) + if (auto* layout_node = this->layout_node(); layout_node && layout_node->is_text_node()) { + // NOTE: Since the text node's data has changed, we need to invalidate the text for rendering. + // This ensures that the new text is reflected in layout, even if we don't end up + // doing a full layout tree rebuild. static_cast(*layout_node).invalidate_text_for_rendering(); - document().set_needs_layout(SetNeedsLayoutReason::CharacterDataReplaceData); + // We also need to relayout. + document().set_needs_layout(SetNeedsLayoutReason::CharacterDataReplaceData); + } + document().bump_character_data_version(); if (m_grapheme_segmenter)