From 1c47695baededa13c57e1504d9c511325da67a05 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 16 Aug 2023 11:03:00 +0200 Subject: [PATCH] LibWeb: Invalidate layout-transformed text on DOM text node change This fixes an issue where programmatically changing the value of an input element wasn't reflected visually. --- ...text-node-invalidation-on-value-change.txt | 22 +++++++++++++++++++ ...ext-node-invalidation-on-value-change.html | 9 ++++++++ .../Libraries/LibWeb/DOM/CharacterData.cpp | 7 ++++++ Userland/Libraries/LibWeb/Layout/TextNode.cpp | 5 +++++ Userland/Libraries/LibWeb/Layout/TextNode.h | 1 + 5 files changed, 44 insertions(+) create mode 100644 Tests/LibWeb/Layout/expected/input-text-node-invalidation-on-value-change.txt create mode 100644 Tests/LibWeb/Layout/input/input-text-node-invalidation-on-value-change.html diff --git a/Tests/LibWeb/Layout/expected/input-text-node-invalidation-on-value-change.txt b/Tests/LibWeb/Layout/expected/input-text-node-invalidation-on-value-change.txt new file mode 100644 index 00000000000..481a3e2bfa3 --- /dev/null +++ b/Tests/LibWeb/Layout/expected/input-text-node-invalidation-on-value-change.txt @@ -0,0 +1,22 @@ +Viewport <#document> at (0,0) content-size 800x600 children: not-inline + BlockContainer at (0,0) content-size 800x600 [BFC] children: not-inline + BlockContainer at (8,8) content-size 784x21.46875 children: inline + line 0 width: 120, height: 21.46875, bottom: 21.46875, baseline: 21.46875 + frag 0 from BlockContainer start: 0, length: 0, rect: [9,9 118x19.46875] + BlockContainer at (9,9) content-size 118x19.46875 inline-block [BFC] children: not-inline + Box
at (11,10) content-size 114x17.46875 flex-container(row) [FFC] children: not-inline + BlockContainer
at (11,10) content-size 49.734375x17.46875 flex-item [BFC] children: inline + line 0 width: 49.734375, height: 17.46875, bottom: 17.46875, baseline: 13.53125 + frag 0 from TextNode start: 0, length: 4, rect: [11,10 49.734375x17.46875] + "PASS" + TextNode <#text> + TextNode <#text> + TextNode <#text> + +PaintableWithLines (Viewport<#document>) [0,0 800x600] + PaintableWithLines (BlockContainer) [0,0 800x600] + PaintableWithLines (BlockContainer) [8,8 784x21.46875] + PaintableWithLines (BlockContainer#foo) [8,8 120x21.46875] + PaintableBox (Box
) [9,9 118x19.46875] + PaintableWithLines (BlockContainer
) [11,10 49.734375x17.46875] + TextPaintable (TextNode<#text>) diff --git a/Tests/LibWeb/Layout/input/input-text-node-invalidation-on-value-change.html b/Tests/LibWeb/Layout/input/input-text-node-invalidation-on-value-change.html new file mode 100644 index 00000000000..2d44904490c --- /dev/null +++ b/Tests/LibWeb/Layout/input/input-text-node-invalidation-on-value-change.html @@ -0,0 +1,9 @@ + + diff --git a/Userland/Libraries/LibWeb/DOM/CharacterData.cpp b/Userland/Libraries/LibWeb/DOM/CharacterData.cpp index 3b123b85dc9..8bfa1f7bf4f 100644 --- a/Userland/Libraries/LibWeb/DOM/CharacterData.cpp +++ b/Userland/Libraries/LibWeb/DOM/CharacterData.cpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace Web::DOM { @@ -108,6 +109,12 @@ WebIDL::ExceptionOr CharacterData::replace_data(size_t offset, size_t coun if (parent()) parent()->children_changed(); + // 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()) + static_cast(*layout_node).invalidate_text_for_rendering(); + set_needs_style_update(true); document().set_needs_layout(); return {}; diff --git a/Userland/Libraries/LibWeb/Layout/TextNode.cpp b/Userland/Libraries/LibWeb/Layout/TextNode.cpp index b3f11f7400e..9a224c6fa25 100644 --- a/Userland/Libraries/LibWeb/Layout/TextNode.cpp +++ b/Userland/Libraries/LibWeb/Layout/TextNode.cpp @@ -41,6 +41,11 @@ static ErrorOr apply_text_transform(DeprecatedString const& st return string; } +void TextNode::invalidate_text_for_rendering() +{ + m_text_for_rendering = {}; +} + DeprecatedString const& TextNode::text_for_rendering() const { if (m_text_for_rendering.is_null()) diff --git a/Userland/Libraries/LibWeb/Layout/TextNode.h b/Userland/Libraries/LibWeb/Layout/TextNode.h index 417baeadb2d..0c7ca28049d 100644 --- a/Userland/Libraries/LibWeb/Layout/TextNode.h +++ b/Userland/Libraries/LibWeb/Layout/TextNode.h @@ -47,6 +47,7 @@ public: Utf8View::Iterator m_iterator; }; + void invalidate_text_for_rendering(); void compute_text_for_rendering(); virtual JS::GCPtr create_paintable() const override;