Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit fafd3d3

Browse files
committed
Created the isDomNode helper and used it in EmitterMixin.
1 parent 7a7da1d commit fafd3d3

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

src/dom/emittermixin.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import { default as EmitterMixin, _getEmitterListenedTo, _setEmitterId } from '../emittermixin';
1111
import uid from '../uid';
1212
import extend from '../lib/lodash/extend';
13-
import isNative from '../lib/lodash/isNative';
13+
import isDomNode from './isdomnode';
1414

1515
/**
1616
* Mixin that injects the DOM events API into its host. It provides the API
@@ -273,15 +273,6 @@ function getNodeUID( node ) {
273273
return node[ 'data-ck-expando' ] || ( node[ 'data-ck-expando' ] = uid() );
274274
}
275275

276-
// Checks (naively) if given node is native DOM Node.
277-
//
278-
// @private
279-
// @param {Node} node
280-
// @return {Boolean} True when native DOM Node.
281-
function isDomNode( node ) {
282-
return node && isNative( node.addEventListener );
283-
}
284-
285276
/**
286277
* Interface representing classes which mix in {@link module:utils/dom/emittermixin~EmitterMixin}.
287278
*

src/dom/isdomnode.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
3+
* For licensing, see LICENSE.md.
4+
*/
5+
6+
/**
7+
* @module utils/dom/iswindow
8+
*/
9+
10+
import isNative from '../lib/lodash/isNative';
11+
12+
/**
13+
* Checks if the object is a native DOM Node.
14+
*
15+
* @param {*} obj
16+
* @returns {Boolean}
17+
*/
18+
export default function isDomNode( obj ) {
19+
return !!( obj && isNative( obj.addEventListener ) );
20+
}

tests/dom/isdomnode.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
3+
* For licensing, see LICENSE.md.
4+
*/
5+
6+
/* global document, window */
7+
8+
import isDomNode from '../../src/dom/isdomnode';
9+
10+
describe( 'isDomNode()', () => {
11+
it( 'detects native DOM nodes', () => {
12+
expect( isDomNode( {} ) ).to.be.false;
13+
expect( isDomNode( null ) ).to.be.false;
14+
expect( isDomNode( undefined ) ).to.be.false;
15+
expect( isDomNode( new Date() ) ).to.be.false;
16+
expect( isDomNode( 42 ) ).to.be.false;
17+
expect( isDomNode( window ) ).to.be.true;
18+
expect( isDomNode( document ) ).to.be.true;
19+
expect( isDomNode( document.createElement( 'div' ) ) ).to.be.true;
20+
expect( isDomNode( document.createTextNode( 'Foo' ) ) ).to.be.true;
21+
} );
22+
} );

0 commit comments

Comments
 (0)