Fix I-2: dangling parent on shell/ColumnLayout-rooted AI-replace trees

treeToCraftState's ROOT re-key branch reparented nodes['ROOT'].nodes
children to 'ROOT' but not nodes['ROOT'].linkedNodes children. For a
ColumnLayout- or Section/BackgroundSection/FormContainer-rooted AI
`replace`, flattenTreeForCraft puts content in linkedNodes (col-N /
section-inner etc.) whose parent was left pointing at the OLD root id
-- which is then deleted, leaving a dangling parent reference that
breaks select/move/delete of those nodes in the Craft.js editor.

Now the same loop that reparents nodes[] children also reparents
linkedNodes children.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-12 17:31:01 -07:00
parent 25e674badd
commit 9d3bbc2c46
2 changed files with 60 additions and 5 deletions
@@ -96,12 +96,57 @@ describe('treeToCraftState resolvedName guard', () => {
expect(parsed.ROOT.type.resolvedName).toBe('ColumnLayout');
expect(parsed.ROOT.linkedNodes).toEqual({ 'col-0': 'a', 'col-1': 'b' });
expect(parsed.ROOT.props.columns).toBe(2);
// NB: the ROOT-aliasing step only reassigns `nodes[]` children's parent,
// not `linkedNodes` children's — a pre-existing quirk (not introduced by
// this refactor, and out of scope for E3) that leaves column children's
// `parent` pointing at the tree's original (pre-alias) root id.
expect(parsed['a'].parent).toBe('cols-1');
expect(parsed['a'].isCanvas).toBe(true);
// I-2: the ROOT-aliasing step must reparent linkedNodes children too, not
// just nodes[] children. Before the fix, 'a'/'b'.parent stayed pointing
// at the tree's original (pre-alias) root id ('cols-1'), which is then
// `delete`d from the output -- a dangling parent reference that breaks
// select/move/delete of those nodes in the Craft.js editor.
expect(parsed['a'].parent).toBe('ROOT');
expect(parsed['b'].parent).toBe('ROOT');
expect(parsed['cols-1']).toBeUndefined();
});
test('I-2: every child parent in the output tree references an id that exists (no dangling reference to the deleted old-root id) -- ColumnLayout root', () => {
const tree: SerializedTreeNode = {
type: { resolvedName: 'ColumnLayout' },
props: { node_id: 'cols-1' },
nodes: [
{ type: { resolvedName: 'Heading' }, props: { node_id: 'a' }, nodes: [] },
{ type: { resolvedName: 'Heading' }, props: { node_id: 'b' }, nodes: [] },
],
};
const parsed = JSON.parse(treeToCraftState(tree));
for (const id of Object.keys(parsed)) {
const parent = parsed[id].parent;
if (parent == null) continue;
expect(parsed[parent]).toBeDefined();
}
});
test('I-2: a Section (SHELL_INNER) root reparents its section-inner linkedNode child to ROOT', () => {
const tree: SerializedTreeNode = {
type: { resolvedName: 'Section' },
props: { node_id: 'sec-root' },
nodes: [
{ type: { resolvedName: 'Heading' }, props: { node_id: 'h-1', text: 'Hi' }, nodes: [] },
],
};
const parsed = JSON.parse(treeToCraftState(tree));
const innerId = parsed.ROOT.linkedNodes['section-inner'];
expect(innerId).toBeDefined();
// The section-inner node itself is a linkedNodes child of the
// (re-keyed) root -- its parent must point at 'ROOT', not the deleted
// original root id 'sec-root'.
expect(parsed[innerId].parent).toBe('ROOT');
expect(parsed['sec-root']).toBeUndefined();
// And every parent reference in the whole tree resolves to a real node.
for (const id of Object.keys(parsed)) {
const parent = parsed[id].parent;
if (parent == null) continue;
expect(parsed[parent]).toBeDefined();
}
});
test('style: [] normalizes to {} (regression)', () => {