Skip to content

fix(drag-drop): error when cloning 0x0 canvas #19707

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
Jun 29, 2020
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
37 changes: 37 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2151,6 +2151,19 @@ describe('CdkDrag', () => {
'Expected cloned canvas to have the same content as the source.');
}));

it('should not throw when cloning an invalid canvas', fakeAsync(() => {
const fixture = createComponent(DraggableWithInvalidCanvasInDropZone);
fixture.detectChanges();
const item = fixture.componentInstance.dragItems.toArray()[1].element.nativeElement;

expect(() => {
startDraggingViaMouse(fixture, item);
tick();
}).not.toThrow();

expect(document.querySelector('.cdk-drag-preview canvas')).toBeTruthy();
}));

it('should clear the ids from descendants of the preview', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone);
fixture.detectChanges();
Expand Down Expand Up @@ -5949,6 +5962,30 @@ class DraggableWithCanvasInDropZone extends DraggableInDropZone implements After
}
}

@Component({
template: `
<div
cdkDropList
style="width: 100px; background: pink;"
[id]="dropZoneId"
[cdkDropListData]="items"
(cdkDropListSorted)="sortedSpy($event)"
(cdkDropListDropped)="droppedSpy($event)">
<div
*ngFor="let item of items"
cdkDrag
[cdkDragData]="item"
[style.height.px]="item.height"
[style.margin-bottom.px]="item.margin"
style="width: 100%; background: red;">
{{item.value}}
<canvas width="0" height="0"></canvas>
</div>
</div>
`
})
class DraggableWithInvalidCanvasInDropZone extends DraggableInDropZone {}

/**
* Component that passes through whatever content is projected into it.
* Used to test having drag elements being projected into a component.
Expand Down
6 changes: 5 additions & 1 deletion src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,11 @@ function deepCloneNode(node: HTMLElement): HTMLElement {
const correspondingCloneContext = cloneCanvases[i].getContext('2d');

if (correspondingCloneContext) {
correspondingCloneContext.drawImage(descendantCanvases[i], 0, 0);
// In some cases `drawImage` can throw (e.g. if the canvas size is 0x0).
// We can't do much about it so just ignore the error.
try {
correspondingCloneContext.drawImage(descendantCanvases[i], 0, 0);
} catch {}
}
}
}
Expand Down