Skip to content

Truly independent web browser

LibWeb: Leave layout up to date on a read below an undisplayed element

Andreas Kling6926a996fffc
diff --git a/Libraries/LibWeb/CSS/UpdateStyle.cpp b/Libraries/LibWeb/CSS/UpdateStyle.cpp
index a87ee14e269..08af3721b25 100644
--- a/Libraries/LibWeb/CSS/UpdateStyle.cpp
+++ b/Libraries/LibWeb/CSS/UpdateStyle.cpp
@@ -117,6 +117,10 @@ static void apply_element_style_invalidation_after_style_change(Layout::BegunRea
// The render state installed the element's record in its box ahead of this install, and marked the relayout, or the
// layout tree rebuild, the move asks for, which a frame's layout round, or the host's next one, runs.
bool const installed_by_render_state = StyleEngineFFI::style_engine_render_state_installed(element.document().style_computer().style_engine().host(), element.style_node_id().value(), element.style_record_identity().value());
+ // NB: A read of the style of an element below one that is not displayed installs it with no box to move, and leaves
+ // layout as it was for what reads it next.
+ if (auto parent = element.element_to_inherit_style_from({}); !installed_by_render_state && parent && has_flag(parent->installed_style().dependency_flags(), StyleRecordDependencyFlag::InDisplayNoneSubtree) && !element.unsafe_layout_node(read))
+ return;
bool const needs_relayout = invalidation.needs_relayout() && !installed_by_render_state;
// Only a full layout pass applies viewport propagation again, so a relayout of an element the viewport takes its
diff --git a/Tests/LibWeb/Text/expected/Editing/insertHTML-block-that-is-not-displayed.txt b/Tests/LibWeb/Text/expected/Editing/insertHTML-block-that-is-not-displayed.txt
new file mode 100644
index 00000000000..2866be20242
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Editing/insertHTML-block-that-is-not-displayed.txt
@@ -0,0 +1,2 @@
+true
+<p id="paragraph">he</p><div style="display: none"><span>hidden</span></div><p id="paragraph">llo</p>
diff --git a/Tests/LibWeb/Text/input/Editing/insertHTML-block-that-is-not-displayed.html b/Tests/LibWeb/Text/input/Editing/insertHTML-block-that-is-not-displayed.html
new file mode 100644
index 00000000000..d64cc48f2bd
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Editing/insertHTML-block-that-is-not-displayed.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<div id="editor" contenteditable="true"><p id="paragraph">hello</p></div>
+<script>
+ test(() => {
+ // Planning the insertion reads the style of what it inserts below the element that is not displayed, and goes on
+ // to read the layout of the text around it.
+ editor.focus();
+ getSelection().collapse(paragraph.firstChild, 2);
+ println(document.execCommand("insertHTML", false, `<div style="display: none"><span>hidden</span></div>`));
+ println(editor.innerHTML);
+ });
+</script>