Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/src/dom_components/model/Components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const getComponentsFromDefs = (
result = all[id] as any;
const { onAttributes, onStyle } = updateOptions;
const component = result as unknown as Component;
// Detach the reused model from its old container so re-inserting it updates `collection`/`parent`
component.collection?.remove(component, { silent: true } as any);
const htmlImportOpts = { ...opts, parsedImportSource: 'html' as const };
tagName && component.set({ tagName }, { ...htmlImportOpts, silent: true });

Expand Down
25 changes: 25 additions & 0 deletions packages/core/test/specs/dom_components/model/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,31 @@ describe('Component', () => {
);
});

test('resetFromString re-parents a reused component when its wrapper is removed', () => {
const wrapper = dcomp.getWrapper()!;
wrapper.components().resetFromString(`<div id="i5y6"><p id="i8sd">Insert your text here</p></div>`);

const div = wrapper.components().at(0);
const paragraph = div.components().at(0);
expect(div.getId()).toBe('i5y6');
expect(paragraph.getId()).toBe('i8sd');
expect(paragraph.parent()).toBe(div);

// Remove the wrapping div, promoting the reused <p> to a direct child of the wrapper.
wrapper.components().resetFromString(`<p id="i8sd">Insert your text here</p>`);

const allById = dcomp.allById();
// The same <p> model instance is reused, not rebuilt.
expect(wrapper.components().at(0)).toBe(paragraph);
expect(allById['i8sd']).toBe(paragraph);
// The removed div is gone from the global registry.
expect(allById['i5y6']).toBeUndefined();
// The reused <p> reports the wrapper as its parent, not the removed div.
expect(paragraph.parent()).toBe(wrapper);
expect(paragraph.collection).toBe(wrapper.components());
expect(em.getHtml()).toBe('<body><p id="i8sd">Insert your text here</p></body>');
});

test('Ability to stop/change propagation chain', () => {
obj.append({
removable: false,
Expand Down
Loading