resolveNode function
Resolves a Morphic tree by expanding all Component instances found.
FragmentMorphic nodes are preserved in the resolved tree — they are
NOT flattened here. Flattening happens at the DOM level in createDom
and _patchChildren in diff.dart, where the parent element context is
available. Preserving fragments in the resolved tree lets the differ
compare prev/next fragments at the same position accurately.
Implementation
Morphic resolveNode(dynamic node) {
// ── Component ────────────────────────────────────────────────────────────
if (node is Component) {
node.attach(RenderContext.runtime);
final rendered = RenderContext.runWithComponent(node, () {
return node.render();
});
final resolved = resolveNode(rendered);
RenderContext.runtime.registerComponentTree(node, resolved);
return resolved;
}
// ── FragmentMorphic ──────────────────────────────────────────────────────
if (node is FragmentMorphic) {
final resolvedChildren = <Morphic>[];
for (final child in node.children) {
resolvedChildren.add(resolveNode(child));
}
return FragmentMorphic(children: resolvedChildren, key: node.key);
}
// ── ElementMorphic ───────────────────────────────────────────────────────
if (node is ElementMorphic) {
final resolvedChildren = <Morphic>[];
for (final child in node.children) {
final resolved = resolveNode(child);
resolvedChildren.add(resolved);
}
return ElementMorphic(
tag: node.tag,
attributes: node.attributes,
children: resolvedChildren,
key: node.key,
);
}
// ── Primitives ───────────────────────────────────────────────────────────
if (node is TextMorphic) return node;
if (node is String) return TextMorphic(node);
if (node is int || node is double) return TextMorphic(node.toString());
throw UnsupportedError('Unknown node type: ${node.runtimeType}');
}