snapshotTree static method
Serializes node and its descendant subtree into a JSON-encodable Map.
Useful for browser DevTools extensions, snapshot assertions in tests, or external CLI inspectors. Unrecognized custom node types are serialized generically by runtime type name.
final snapshot = BloomJsDevTools.snapshotTree(
Div(className: 'btn', text: 'Click me'),
);
// Produces:
// {'kind': 'element', 'tag': 'div', 'className': 'btn', 'text': 'Click me', 'children': []}
Implementation
static Map<String, dynamic> snapshotTree(BloomNode node) {
return switch (node) {
ElNode(:final tag, :final text, :final className, :final attrs, :final children) => {
'kind': 'element',
'tag': tag,
if (text != null) 'text': text,
if (className != null) 'className': className,
if (attrs != null && attrs.isNotEmpty) 'attrs': attrs,
'children': [for (final child in children) snapshotTree(child)],
},
TextNode(:final text) => {
'kind': 'text',
'text': text,
},
FragmentNode(:final children) => {
'kind': 'fragment',
'children': [for (final child in children) snapshotTree(child)],
},
_ => {
'kind': 'node',
'type': node.runtimeType.toString(),
},
};
}