Skip to content

fix(drag-drop): error on touch end #14392

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
Dec 5, 2018
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
24 changes: 24 additions & 0 deletions src/cdk/drag-drop/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,30 @@ describe('CdkDrag', () => {
.toEqual(['Zero', 'One', 'Two', 'Three']);
}));

it('should not throw if the `touches` array is empty', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone);
fixture.detectChanges();
const item = fixture.componentInstance.dragItems.toArray()[1].element.nativeElement;

dispatchTouchEvent(item, 'touchstart');
fixture.detectChanges();

dispatchTouchEvent(document, 'touchmove');
fixture.detectChanges();

dispatchTouchEvent(document, 'touchmove', 50, 50);
fixture.detectChanges();

expect(() => {
const endEvent = createTouchEvent('touchend', 50, 50);
Object.defineProperty(endEvent, 'touches', {get: () => []});

dispatchEvent(document, endEvent);
fixture.detectChanges();
}).not.toThrow();

}));

});

describe('in a connected drop container', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/cdk/drag-drop/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,8 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {

/** Determines the point of the page that was touched by the user. */
private _getPointerPositionOnPage(event: MouseEvent | TouchEvent): Point {
const point = this._isTouchEvent(event) ? event.touches[0] : event;
// `touches` will be empty for start/end events so we have to fall back to `changedTouches`.
const point = this._isTouchEvent(event) ? (event.touches[0] || event.changedTouches[0]) : event;

return {
x: point.pageX - this._scrollPosition.left,
Expand Down