Skip to content

fix(drag-drop): prevent dragging selected text with the mouse #18103

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
Jan 22, 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
28 changes: 28 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,34 @@ describe('CdkDrag', () => {
subscription.unsubscribe();
}));

it('should prevent the default `mousemove` action even before the drag threshold has ' +
'been reached', fakeAsync(() => {
const fixture = createComponent(StandaloneDraggable, [], 5);
fixture.detectChanges();
const dragElement = fixture.componentInstance.dragElement.nativeElement;

dispatchMouseEvent(dragElement, 'mousedown', 2, 2);
fixture.detectChanges();
const mousemoveEvent = dispatchMouseEvent(document, 'mousemove', 2, 2);
fixture.detectChanges();

expect(mousemoveEvent.defaultPrevented).toBe(true);
}));

it('should prevent the default `touchmove` action even before the drag threshold has ' +
'been reached', fakeAsync(() => {
const fixture = createComponent(StandaloneDraggable, [], 5);
fixture.detectChanges();
const dragElement = fixture.componentInstance.dragElement.nativeElement;

dispatchTouchEvent(dragElement, 'touchstart', 2, 2);
fixture.detectChanges();
const touchmoveEvent = dispatchTouchEvent(document, 'touchmove', 2, 2);
fixture.detectChanges();

expect(touchmoveEvent.defaultPrevented).toBe(true);
}));

});

describe('draggable with a handle', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,10 @@ export class DragRef<T = any> {

/** Handler that is invoked when the user moves their pointer after they've initiated a drag. */
private _pointerMove = (event: MouseEvent | TouchEvent) => {
// Prevent the default action as early as possible in order to block
// native actions like dragging the selected text or images with the mouse.
event.preventDefault();

if (!this._hasStartedDragging) {
const pointerPosition = this._getPointerPositionOnPage(event);
const distanceX = Math.abs(pointerPosition.x - this._pickupPositionOnPage.x);
Expand Down Expand Up @@ -565,7 +569,6 @@ export class DragRef<T = any> {

const constrainedPointerPosition = this._getConstrainedPointerPosition(event);
this._hasMoved = true;
event.preventDefault();
this._updatePointerDirectionDelta(constrainedPointerPosition);

if (this._dropContainer) {
Expand Down