Skip to content

fix(drag-drop): error during device emulation on firefox #19396

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
May 21, 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
21 changes: 21 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,27 @@ describe('CdkDrag', () => {
expect(drag.rootElementSelector).toBe('.root');
}));

it('should not throw if touches and changedTouches are empty', fakeAsync(() => {
const fixture = createComponent(StandaloneDraggable);
fixture.detectChanges();
const dragElement = fixture.componentInstance.dragElement.nativeElement;

startDraggingViaTouch(fixture, dragElement);
continueDraggingViaTouch(fixture, 50, 100);

const event = createTouchEvent('touchend', 50, 100);
Object.defineProperties(event, {
touches: {get: () => []},
changedTouches: {get: () => []}
});

expect(() => {
dispatchEvent(document, event);
fixture.detectChanges();
tick();
}).not.toThrow();
}));

});

describe('draggable with a handle', () => {
Expand Down
11 changes: 9 additions & 2 deletions src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1034,9 +1034,16 @@ export class DragRef<T = any> {

/** Determines the point of the page that was touched by the user. */
private _getPointerPositionOnPage(event: MouseEvent | TouchEvent): Point {
// `touches` will be empty for start/end events so we have to fall back to `changedTouches`.
const point = isTouchEvent(event) ? (event.touches[0] || event.changedTouches[0]) : event;
const scrollPosition = this._getViewportScrollPosition();
const point = isTouchEvent(event) ?
// `touches` will be empty for start/end events so we have to fall back to `changedTouches`.
// Also note that on real devices we're guaranteed for either `touches` or `changedTouches`
// to have a value, but Firefox in device emulation mode has a bug where both can be empty
// for `touchstart` and `touchend` so we fall back to a dummy object in order to avoid
// throwing an error. The value returned here will be incorrect, but since this only
// breaks inside a developer tool and the value is only used for secondary information,
// we can get away with it. See https://bugzilla.mozilla.org/show_bug.cgi?id=1615824.
(event.touches[0] || event.changedTouches[0] || {pageX: 0, pageY: 0}) : event;

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