Skip to content

Truly independent web browser

LibWeb: Test full layout tree updates over a live tree

Andreas Klingb4c33989462f
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index 189fa424e11..04deff3bb8f 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -3088,6 +3088,11 @@ GC::Ref<JS::Object> Internals::compare_layout_tree_with_full_rebuild()
return object;
}
+void Internals::request_full_layout_tree_update()
+{
+ window().associated_document().set_needs_full_layout_tree_update();
+}
+
GC::Ref<JS::Object> Internals::style_memory_report()
{
auto& document = window().associated_document();
diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.h
index f71b9985a19..adc8a462a64 100644
--- a/Libraries/LibWeb/Internals/Internals.h
+++ b/Libraries/LibWeb/Internals/Internals.h
@@ -330,6 +330,7 @@ public:
void force_visual_context_tree_rebuild_on_next_compositor_animation_update_for_testing();
void request_reentrant_animation_style_flush_for_testing(GC::Ref<DOM::Node>);
GC::Ref<JS::Object> compare_layout_tree_with_full_rebuild();
+ void request_full_layout_tree_update();
GC::Ref<JS::Object> style_engine_counters();
GC::Ref<JS::Object> style_memory_report();
u64 style_memory_of_frames();
diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idl
index 74ac645e7a0..2428118f9d4 100644
--- a/Libraries/LibWeb/Internals/Internals.idl
+++ b/Libraries/LibWeb/Internals/Internals.idl
@@ -428,6 +428,9 @@ interface Internals {
// returns { matches }, plus { incremental, full } on mismatch. This is a test oracle for
// incremental tree builds.
object compareLayoutTreeWithFullRebuild();
+ // Makes the next layout update of the document build its layout tree again over the live one, as a full update
+ // requested by the engine does, without tearing the tree down first.
+ undefined requestFullLayoutTreeUpdate();
// What the style engine and the style updates of the document counted and measured, for the style update capture
// tools. Tests do not read it: traces and dumps say what the engine did.
object styleEngineCounters();
diff --git a/Tests/LibWeb/Text/expected/layout-tree-update/full-rebuild-over-live-tree-after-mutations.txt b/Tests/LibWeb/Text/expected/layout-tree-update/full-rebuild-over-live-tree-after-mutations.txt
new file mode 100644
index 00000000000..cc238211af6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/layout-tree-update/full-rebuild-over-live-tree-after-mutations.txt
@@ -0,0 +1,2 @@
+PASS: full update over the live tree after mutations
+PASS: full update over the live tree after reopening the dialog
diff --git a/Tests/LibWeb/Text/expected/layout-tree-update/full-rebuild-over-live-tree.txt b/Tests/LibWeb/Text/expected/layout-tree-update/full-rebuild-over-live-tree.txt
new file mode 100644
index 00000000000..9a5bfcbea6b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/layout-tree-update/full-rebuild-over-live-tree.txt
@@ -0,0 +1,2 @@
+PASS: full update over the live tree
+PASS: second full update over the live tree
diff --git a/Tests/LibWeb/Text/input/layout-tree-update/full-rebuild-over-live-tree-after-mutations.html b/Tests/LibWeb/Text/input/layout-tree-update/full-rebuild-over-live-tree-after-mutations.html
new file mode 100644
index 00000000000..88298cf100c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/layout-tree-update/full-rebuild-over-live-tree-after-mutations.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<div id="fixture"></div>
+<script src="../include.js"></script>
+<script src="../work-test.js"></script>
+<script src="resources/full-rebuild-comparison.js"></script>
+<script src="resources/live-tree-fixture.js"></script>
+<script>
+ // Incremental updates free rows and reuse them, so the full update runs over a tree whose rows are spread over
+ // reused slots, some older than what the arena remembers of its last layout.
+ test(() => {
+ const fixture = document.getElementById("fixture");
+ setUpLiveTreeFixture(fixture);
+ document.body.offsetHeight;
+ const mutations = [
+ () => fixture.querySelector("section").insertAdjacentHTML("beforeend", "<p>appended <i>paragraph</i></p>"),
+ () => fixture.querySelector("ol li").remove(),
+ () => fixture.querySelectorAll("section")[1].style.display = "none",
+ () => fixture.querySelectorAll("section")[1].style.display = "",
+ () => fixture.querySelector("table tr").insertAdjacentHTML("afterend", "<tr><td>inserted row</td></tr>"),
+ () => fixture.querySelector(".first-letter").firstChild.data = "Changed first letter",
+ () => fixture.querySelector("#host").shadowRoot.querySelector("p").remove(),
+ () => fixture.querySelector("#modal").close(),
+ ];
+ for (const mutate of mutations) {
+ mutate();
+ document.body.offsetHeight;
+ }
+ printFullUpdateOverLiveTreeComparison("full update over the live tree after mutations");
+ fixture.querySelector("#modal").showModal();
+ document.body.offsetHeight;
+ printFullUpdateOverLiveTreeComparison("full update over the live tree after reopening the dialog");
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/layout-tree-update/full-rebuild-over-live-tree.html b/Tests/LibWeb/Text/input/layout-tree-update/full-rebuild-over-live-tree.html
new file mode 100644
index 00000000000..e724fc4fe2c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/layout-tree-update/full-rebuild-over-live-tree.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<div id="fixture"></div>
+<script src="../include.js"></script>
+<script src="../work-test.js"></script>
+<script src="resources/full-rebuild-comparison.js"></script>
+<script src="resources/live-tree-fixture.js"></script>
+<script>
+ test(() => {
+ setUpLiveTreeFixture(document.getElementById("fixture"));
+ document.body.offsetHeight;
+ printFullUpdateOverLiveTreeComparison("full update over the live tree");
+ printFullUpdateOverLiveTreeComparison("second full update over the live tree");
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/layout-tree-update/resources/full-rebuild-comparison.js b/Tests/LibWeb/Text/input/layout-tree-update/resources/full-rebuild-comparison.js
index 2647606e977..363dfea545e 100644
--- a/Tests/LibWeb/Text/input/layout-tree-update/resources/full-rebuild-comparison.js
+++ b/Tests/LibWeb/Text/input/layout-tree-update/resources/full-rebuild-comparison.js
@@ -2444,3 +2444,26 @@ function anonymousWrapperCases() {
}
return cases;
}
+
+// Dumps the document's layout tree, has the next layout update build the tree again over the live one (old rows are
+// freed and taken over as the engine's own full updates do), dumps it again and compares the two dumps.
+function compareLayoutTreeWithFullUpdateOverLiveTree() {
+ const before = internals.dumpLayoutTree(document);
+ const work = layoutTreeBuildWork(() => internals.requestFullLayoutTreeUpdate());
+ const after = internals.dumpLayoutTree(document);
+ return { rebuilt: work.rebuiltWholeTree(), matches: before === after, before, after };
+}
+
+function printFullUpdateOverLiveTreeComparison(name) {
+ const comparison = compareLayoutTreeWithFullUpdateOverLiveTree();
+ println(`${comparison.rebuilt && comparison.matches ? "PASS" : "FAIL"}: ${name}`);
+ if (!comparison.rebuilt) println(" the request built no tree");
+ if (!comparison.matches) {
+ const before = comparison.before.split("\n");
+ const after = comparison.after.split("\n");
+ const line = before.findIndex((text, index) => text !== after[index]);
+ println(` first difference at line ${line}:`);
+ println(` before: ${before[line]}`);
+ println(` after: ${after[line]}`);
+ }
+}
diff --git a/Tests/LibWeb/Text/input/layout-tree-update/resources/live-tree-fixture.js b/Tests/LibWeb/Text/input/layout-tree-update/resources/live-tree-fixture.js
new file mode 100644
index 00000000000..82df613169e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/layout-tree-update/resources/live-tree-fixture.js
@@ -0,0 +1,40 @@
+// A fixture with every kind of box a layout tree build makes: pseudo-elements, list markers, counters and quotes,
+// ::first-letter, a top-layer element with its ::backdrop, SVG resources, tables with missing parts, display:
+// contents, shadow trees with slots, anonymous wrappers and form controls.
+const liveTreeFixtureStyle = `<style>
+ body { counter-reset: section; quotes: "<" ">" "[" "]"; }
+ section { counter-increment: section; }
+ section::before { content: "S" counter(section) " "; }
+ section::after { content: " end"; display: block; }
+ .first-letter::first-letter { font-size: 2em; }
+ .contents { display: contents; }
+ .loose-cell { display: table-cell; }
+ .loose-row { display: table-row; }
+ dialog::backdrop { background: rgba(0, 0, 0, 0.1); }
+ ol.reversed-list li::marker { content: counter(list-item) ") "; }
+ q::before { content: open-quote; }
+ q::after { content: close-quote; }
+</style>`;
+
+const liveTreeFixtureMarkup = `
+<section><p class="first-letter">Lorem <b>ipsum</b> dolor</p><ol><li>one</li><li>two<ol><li>nested</li></ol></li></ol>
+ <ol reversed class="reversed-list"><li>a</li><li>b</li></ol><p>quoted <q>outer <q>inner</q></q> text</p></section>
+<section><div>inline start<div>block in the middle</div>inline end</div>
+ <span>inline <div>block inside inline</div> after</span>
+ <div class="contents"><p>in contents</p>text in contents</div></section>
+<section><table><caption>caption</caption><tr><td>a</td><td rowspan="2">b</td></tr><tr><td>c</td></tr></table>
+ <div><span class="loose-cell">loose cell</span><span class="loose-cell">another</span></div>
+ <div class="loose-row"><div class="loose-cell">row cell</div></div></section>
+<section><svg width="40" height="20"><defs><linearGradient id="gradient"><stop offset="0" stop-color="red"/>
+ <stop offset="1" stop-color="blue"/></linearGradient><clipPath id="clip"><rect width="10" height="10"/></clipPath></defs>
+ <rect width="20" height="20" fill="url(#gradient)" clip-path="url(#clip)"/></svg>
+ <div id="host"><span slot="named">slotted named</span><span>slotted default</span></div>
+ <button>button <b>label</b></button><fieldset><legend>legend</legend>fieldset content</fieldset></section>
+<dialog id="modal">modal <b>dialog</b></dialog>`;
+
+function setUpLiveTreeFixture(container) {
+ container.innerHTML = liveTreeFixtureStyle + liveTreeFixtureMarkup;
+ const shadow = container.querySelector("#host").attachShadow({ mode: "open" });
+ shadow.innerHTML = `<style>p { margin: 0; }</style><p>shadow before</p><slot name="named"></slot><p>between</p><slot></slot>`;
+ container.querySelector("#modal").showModal();
+}