Skip to content

Commit 751fe8a

Browse files
crisbetojelbourn
authored andcommitted
fix(drag-drop): clear duplicate ids from descendants (#15135)
Fixes `CdkDrag` generating elements with duplicate ids, if one of its descendants has an id. Fixes #15120.
1 parent f1e1528 commit 751fe8a

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/cdk/drag-drop/directives/drag.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,21 @@ describe('CdkDrag', () => {
13921392
expect(preview.getAttribute('id')).toBeFalsy();
13931393
}));
13941394

1395+
it('should clear the ids from descendants of the preview', fakeAsync(() => {
1396+
const fixture = createComponent(DraggableInDropZone);
1397+
fixture.detectChanges();
1398+
const item = fixture.componentInstance.dragItems.toArray()[1].element.nativeElement;
1399+
const extraChild = document.createElement('div');
1400+
extraChild.id = 'child-id';
1401+
extraChild.classList.add('preview-child');
1402+
item.appendChild(extraChild);
1403+
1404+
startDraggingViaMouse(fixture, item);
1405+
1406+
expect(document.querySelectorAll('.preview-child').length).toBeGreaterThan(1);
1407+
expect(document.querySelectorAll('[id="child-id"]').length).toBe(1);
1408+
}));
1409+
13951410
it('should not create a preview if the element was not dragged far enough', fakeAsync(() => {
13961411
const fixture = createComponent(DraggableInDropZone, [], 5);
13971412
fixture.detectChanges();
@@ -1597,6 +1612,21 @@ describe('CdkDrag', () => {
15971612
expect(placeholder.getAttribute('id')).toBeFalsy();
15981613
}));
15991614

1615+
it('should clear the ids from descendants of the placeholder', fakeAsync(() => {
1616+
const fixture = createComponent(DraggableInDropZone);
1617+
fixture.detectChanges();
1618+
const item = fixture.componentInstance.dragItems.toArray()[1].element.nativeElement;
1619+
const extraChild = document.createElement('div');
1620+
extraChild.id = 'child-id';
1621+
extraChild.classList.add('placeholder-child');
1622+
item.appendChild(extraChild);
1623+
1624+
startDraggingViaMouse(fixture, item);
1625+
1626+
expect(document.querySelectorAll('.placeholder-child').length).toBeGreaterThan(1);
1627+
expect(document.querySelectorAll('[id="child-id"]').length).toBe(1);
1628+
}));
1629+
16001630
it('should not create placeholder if the element was not dragged far enough', fakeAsync(() => {
16011631
const fixture = createComponent(DraggableInDropZone, [], 5);
16021632
fixture.detectChanges();

src/cdk/drag-drop/drag-ref.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,8 +1003,15 @@ function getTransform(x: number, y: number): string {
10031003
/** Creates a deep clone of an element. */
10041004
function deepCloneNode(node: HTMLElement): HTMLElement {
10051005
const clone = node.cloneNode(true) as HTMLElement;
1006+
const descendantsWithId = clone.querySelectorAll('[id]');
1007+
10061008
// Remove the `id` to avoid having multiple elements with the same id on the page.
10071009
clone.removeAttribute('id');
1010+
1011+
for (let i = 0; i < descendantsWithId.length; i++) {
1012+
descendantsWithId[i].removeAttribute('id');
1013+
}
1014+
10081015
return clone;
10091016
}
10101017

0 commit comments

Comments
 (0)