Skip to content
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
14 changes: 13 additions & 1 deletion src/material/slider/slider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
dispatchKeyboardEvent,
dispatchMouseEvent,
createKeyboardEvent,
} from '../../cdk/testing/private';
createTouchEvent,
} from '@angular/cdk/testing/private';
import {Component, DebugElement, Type, ViewChild} from '@angular/core';
import {ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing';
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
Expand Down Expand Up @@ -242,6 +243,17 @@ describe('MatSlider', () => {
it('should have a focus indicator', () => {
expect(sliderNativeElement.classList.contains('mat-focus-indicator')).toBe(true);
});

it('should not try to preventDefault on a non-cancelable event', () => {
const event = createTouchEvent('touchstart');
const spy = spyOn(event, 'preventDefault');
Object.defineProperty(event, 'cancelable', {value: false});

dispatchEvent(sliderNativeElement, event);
fixture.detectChanges();

expect(spy).not.toHaveBeenCalled();
});
});

describe('disabled slider', () => {
Expand Down
8 changes: 7 additions & 1 deletion src/material/slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,14 +672,20 @@ export class MatSlider
const oldValue = this.value;
this._isSliding = 'pointer';
this._lastPointerEvent = event;
event.preventDefault();
this._focusHostElement();
this._onMouseenter(); // Simulate mouseenter in case this is a mobile device.
this._bindGlobalEvents(event);
this._focusHostElement();
this._updateValueFromPosition(pointerPosition);
this._valueOnSlideStart = oldValue;

// Despite the fact that we explicitly bind active events, in some cases the browser
// still dispatches non-cancelable events which cause this call to throw an error.
// There doesn't appear to be a good way of avoiding them. See #23820.
if (event.cancelable) {
event.preventDefault();
}

// Emit a change and input event if the value changed.
if (oldValue != this.value) {
this._emitInputEvent();
Expand Down