Repository navigation
Remove fragments whose first or last child renders nothing - #968
Merged
Merged
Conversation
The first and last node lookups of a fragment walk its children's sibling links, which were only updated when a child's nodes were inserted. After a keyed move, or a child attached in front of existing ones, the links skipped children, so an empty fragment that ended up at an edge still hid the fragment's nodes.
Package Version ReportThe following packages have been updated: |
schultek
reviewed
Nov 12, 2026
schultek
left a comment
Owner
There was a problem hiding this comment.
Thanks for this contribution, the fix looks good.
The one Copilot comment seems accurate, after this is fixed I will approve.
The root render object survives a reload while every render object below it is replaced without being removed from its parent. Since the root now tracks its first and last child, the replaced ones stayed linked behind the new children, together with their detached DOM nodes, and each reload added another set.
Contributor
Author
|
Fixed in a63eb47, with a test for repeated reloads. Also merged |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Description
When the first or last child of a fragment is a fragment that renders nothing, such as
Component.empty()orComponent.fragment([]), the client renderer loses track of the fragment's nodes. We ran into it with a routed page built asComponent.fragment([header, table, pager]), where the pager returnsconst Component.empty()when there is only one page. After navigating away, the new page was inserted and the old page's nodes stayed below it.On
main:Component.fragment([Component.empty(), h1])leaves the<h1>in the DOM ('<p>Replacement</p><h1>A</h1>').Component.fragment([h1, Component.empty()])fails the assertion "Non-empty attached fragments must have a valid last child reference." and also leaves the<h1>. With asserts disabled,removeChildrenstarts its walk from a null node and removes nothing (from reading the code; the tests run with asserts on).'Start<p>End</p><h1>A</h1>'instead of'Start<h1>A</h1><p>End</p>'), and moving the fragment trips the same assertion.Cause
DomRenderFragment.firstChildNodeandlastChildNodelook only at the first and last child, and returnnullwhen that child is an empty fragment:jaspr/packages/jaspr/lib/src/client/dom_render_object.dart
Lines 324 to 346 in 413ad06
removeChildrenandmovethen treat the fragment as empty, or walk back from a null last node:jaspr/packages/jaspr/lib/src/client/dom_render_object.dart
Lines 408 to 415 in 413ad06
and
getRealNodeOffalls back to the fragment's previous sibling, so a node inserted after the fragment goes in before its nodes:jaspr/packages/jaspr/lib/src/client/dom_render_object.dart
Lines 497 to 504 in 413ad06
Changes
The first commit makes both getters walk the fragment's children through their sibling links, past children that have no node, up to
_firstChild/_lastChild.That walk depends on the sibling links, which
attachChildonly updated when it inserted a child's node. It skipped them when a child fragment was moved, and when the child's node was already in place:jaspr/packages/jaspr/lib/src/client/dom_render_object.dart
Lines 520 to 530 in 413ad06
A child attached in front also got
nextSibling = null, sinceafterwas null:jaspr/packages/jaspr/lib/src/client/dom_render_object.dart
Lines 553 to 557 in 413ad06
So
[h1]rebuilt as[Component.empty(), h1], or a keyed reorder that moves an empty fragment to an edge, still lost the fragment's nodes with the first commit alone. The second commit moves the first and last child tracking fromDomRenderFragmentintoMultiChildDomRenderObject. It also links the child on every path throughattachChild, unlinking it from its old position first.removeChildandDomRenderFragment.removeuse the same unlink step.The third commit links the child before
attachChildresolves the node to place it after. During a keyed reorder, a child whose slot did not change (same index, same previous sibling) is not attached again, so its links can still point at the old position of the child being moved. When that sibling is an empty fragment,getRealNodeOf(after)walks back through those links and can resolve to the moved child itself. Children[b S, span X, empty 1, empty 2, empty 3, b E]rebuilt as[b S, empty 3, empty 1, empty 2, span X, b E]trip "Child node should have been placed after the specified node." and leave the<span>behind on the next replacement. This also happens onmain. With the child linked first, everything in front of it is already in its final order when the walk runs. The same commit readsfirstChildNodeonce before the loops inmoveandremoveChildren, since the getter now walks past leading empty children.Tests
New browser tests in
packages/jaspr/test/client/fragments/fragments_browser_test.dartcover an empty fragment first, last, both, nested inside an edge child, and around the only child of a nested fragment. For each case, the tests replace the fragment, insert a sibling after it, and move it as a keyed fragment. Four more cover an empty fragment that reaches an edge through a rebuild (added first or last, or moved first or last by key), followed by an insertion and a replacement. Two cover a hydrated fragment with empty first and last children, and the same kind of fragment moved to another parent with aGlobalKey. One more covers the keyed reorder described above.dart test -p chrome test/client/fragments/fragments_browser_test.dartinpackages/jaspr:main(413ad06)Five of the new tests already pass on
main: the four keyed moves other than "empty last" (the sibling moved past the fragment ends up in the right place either way) and inserting after a fragment whose last child is not empty. They are there to guard against regressions.dart test(vm and chrome) passes inpackages/jaspr(382 passed, 3 skipped) andpackages/jaspr_router(20 passed).dart analyze --fatal-infosanddart format --set-exit-if-changedreport nothing.Not in this PR: replacing a fragment that is itself the child of another fragment fails the assertion "Cannot remove fragment from a different parent." and leaves its nodes in place. It happens on
maintoo, only with asserts enabled, and has a different cause (the assertion inremoveChildren), so I'll open a separate issue for it.Type of Change
Ready Checklist
the semantic_changelog format.
///).