Skip to content

fix(drag-drop): clear duplicate ids from descendants #15135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2019
Merged
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
30 changes: 30 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,21 @@ describe('CdkDrag', () => {
expect(preview.getAttribute('id')).toBeFalsy();
}));

it('should clear the ids from descendants of the preview', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone);
fixture.detectChanges();
const item = fixture.componentInstance.dragItems.toArray()[1].element.nativeElement;
const extraChild = document.createElement('div');
extraChild.id = 'child-id';
extraChild.classList.add('preview-child');
item.appendChild(extraChild);

startDraggingViaMouse(fixture, item);

expect(document.querySelectorAll('.preview-child').length).toBeGreaterThan(1);
expect(document.querySelectorAll('[id="child-id"]').length).toBe(1);
}));

it('should not create a preview if the element was not dragged far enough', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone, [], 5);
fixture.detectChanges();
Expand Down Expand Up @@ -1512,6 +1527,21 @@ describe('CdkDrag', () => {
expect(placeholder.getAttribute('id')).toBeFalsy();
}));

it('should clear the ids from descendants of the placeholder', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone);
fixture.detectChanges();
const item = fixture.componentInstance.dragItems.toArray()[1].element.nativeElement;
const extraChild = document.createElement('div');
extraChild.id = 'child-id';
extraChild.classList.add('placeholder-child');
item.appendChild(extraChild);

startDraggingViaMouse(fixture, item);

expect(document.querySelectorAll('.placeholder-child').length).toBeGreaterThan(1);
expect(document.querySelectorAll('[id="child-id"]').length).toBe(1);
}));

it('should not create placeholder if the element was not dragged far enough', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone, [], 5);
fixture.detectChanges();
Expand Down
7 changes: 7 additions & 0 deletions src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -992,8 +992,15 @@ function getTransform(x: number, y: number): string {
/** Creates a deep clone of an element. */
function deepCloneNode(node: HTMLElement): HTMLElement {
const clone = node.cloneNode(true) as HTMLElement;
const descendantsWithId = clone.querySelectorAll('[id]');

// Remove the `id` to avoid having multiple elements with the same id on the page.
clone.removeAttribute('id');

for (let i = 0; i < descendantsWithId.length; i++) {
descendantsWithId[i].removeAttribute('id');
}

return clone;
}

Expand Down