Skip to content

Commit

Permalink
Merge pull request #8022 from ckeditor/i/7995-input-events-detection
Browse files Browse the repository at this point in the history
Feature (utils): Implemented the Input Events Level 1 support detection. Closes #7995.
  • Loading branch information
niegowski authored Sep 8, 2020
2 parents 7493039 + d744cd2 commit 2078aae
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
25 changes: 24 additions & 1 deletion packages/ckeditor5-utils/src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

/* globals navigator:false */

import global from './dom/global';

/**
* @module utils/env
*/
Expand Down Expand Up @@ -71,7 +73,15 @@ const env = {
*
* @type {Boolean}
*/
isRegExpUnicodePropertySupported: isRegExpUnicodePropertySupported()
isRegExpUnicodePropertySupported: isRegExpUnicodePropertySupported(),

/**
* Indicates that the environment supports at least [Input Events Level 1](https://www.w3.org/TR/input-events-1/)
* (`input` and `beforeinput` events).
*
* @type {Boolean}
*/
isInputEventsLevel1Supported: isInputEventsLevel1Supported( global.window )
}
};

Expand Down Expand Up @@ -151,3 +161,16 @@ export function isRegExpUnicodePropertySupported() {

return isSupported;
}

/**
* Checks if the current environment supports at least [Input Events Level 1](https://www.w3.org/TR/input-events-1/)
* (`input` and `beforeinput` events).
*
* @param {Window} domWindow The DOM Window interface.
* @returns {Boolean}
*/
export function isInputEventsLevel1Supported( domWindow ) {
const inputEvent = new domWindow.InputEvent( 'input' );

return 'inputType' in inputEvent && 'getTargetRanges' in inputEvent;
}
39 changes: 38 additions & 1 deletion packages/ckeditor5-utils/tests/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

import env, { isMac, isGecko, isSafari, isAndroid, isRegExpUnicodePropertySupported, isBlink } from '../src/env';
import env, {
isMac,
isGecko,
isSafari,
isAndroid,
isBlink,
isRegExpUnicodePropertySupported,
isInputEventsLevel1Supported
} from '../src/env';

function toLowerCase( str ) {
return str.toLowerCase();
Expand Down Expand Up @@ -54,6 +62,12 @@ describe( 'Env', () => {
expect( env.features.isRegExpUnicodePropertySupported ).to.be.a( 'boolean' );
} );
} );

describe( 'isInputEventsLevel1Supported', () => {
it( 'is a boolean', () => {
expect( env.features.isInputEventsLevel1Supported ).to.be.a( 'boolean' );
} );
} );
} );

describe( 'isMac()', () => {
Expand Down Expand Up @@ -201,4 +215,27 @@ describe( 'Env', () => {
}
} );
} );

describe( 'isInputEventsLevel1Supported()', () => {
it( 'should detect the Input Events Level 1 support', () => {
class InputEventStubWhenSupported {
constructor() {
this.inputType = '';
this.getTargetRanges = () => {};
}
}

const DOMWindowStubWhenSupported = {
InputEvent: InputEventStubWhenSupported
};

const DOMWindowStubWhenUnsupported = {
// eslint-disable-next-line
InputEvent: function () {}
};

expect( isInputEventsLevel1Supported( DOMWindowStubWhenSupported ) ).to.be.true;
expect( isInputEventsLevel1Supported( DOMWindowStubWhenUnsupported ) ).to.be.false;
} );
} );
} );

0 comments on commit 2078aae

Please sign in to comment.