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

Commit a9a6bec

Browse files
authored
Merge pull request #218 from ckeditor/t/214
Other: Introduced the `isText()` helper. Closes #214.
2 parents 46ef539 + 796bb69 commit a9a6bec

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed

src/dom/istext.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
3+
* For licensing, see LICENSE.md.
4+
*/
5+
6+
/**
7+
* @module utils/dom/istext
8+
*/
9+
10+
/**
11+
* Checks if the object is a native DOM Text node.
12+
*
13+
* @param {*} obj
14+
* @returns {Boolean}
15+
*/
16+
export default function isText( obj ) {
17+
return Object.prototype.toString.call( obj ) == '[object Text]';
18+
}

src/dom/rect.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
* @module utils/dom/rect
88
*/
99

10-
/* global Node */
11-
1210
import isRange from './isrange';
1311
import isWindow from './iswindow';
1412
import isElement from '../lib/lodash/isElement';
1513
import getBorderWidths from './getborderwidths';
1614
import log from '../log';
15+
import isText from './istext';
1716

1817
/**
1918
* A helper class representing a `ClientRect` object, e.g. value returned by
@@ -367,7 +366,7 @@ export default class Rect {
367366
else {
368367
let startContainer = range.startContainer;
369368

370-
if ( startContainer.nodeType === Node.TEXT_NODE ) {
369+
if ( isText( startContainer ) ) {
371370
startContainer = startContainer.parentNode;
372371
}
373372

src/dom/scroll.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
* For licensing, see LICENSE.md.
44
*/
55

6-
/* global Node */
7-
86
/**
97
* @module utils/dom/scroll
108
*/
119

1210
import isRange from './isrange';
1311
import Rect from './rect';
12+
import isText from './istext';
1413

1514
const utils = {};
1615

@@ -255,7 +254,7 @@ function getParentElement( elementOrRange ) {
255254
let parent = elementOrRange.commonAncestorContainer;
256255

257256
// If a Range is attached to the Text, use the closest element ancestor.
258-
if ( parent.nodeType == Node.TEXT_NODE ) {
257+
if ( isText( parent ) ) {
259258
parent = parent.parentNode;
260259
}
261260

tests/dom/istext.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
3+
* For licensing, see LICENSE.md.
4+
*/
5+
6+
/* global document, Text */
7+
8+
import isText from '../../src/dom/istext';
9+
10+
describe( 'isText()', () => {
11+
it( 'detects native DOM Text', () => {
12+
expect( isText( new Text( 'foo' ) ) ).to.be.true;
13+
14+
expect( isText( 'foo' ) ).to.be.false;
15+
expect( isText( {} ) ).to.be.false;
16+
expect( isText( null ) ).to.be.false;
17+
expect( isText( undefined ) ).to.be.false;
18+
expect( isText( new Date() ) ).to.be.false;
19+
expect( isText( 42 ) ).to.be.false;
20+
expect( isText( document.createElement( 'div' ) ) ).to.be.false;
21+
expect( isText( document.createDocumentFragment() ) ).to.be.false;
22+
expect( isText( document.createComment( 'a' ) ) ).to.be.false;
23+
} );
24+
25+
it( 'works for texts in an iframe', done => {
26+
const iframe = document.createElement( 'iframe' );
27+
28+
iframe.addEventListener( 'load', () => {
29+
const iframeDocument = iframe.contentWindow.document;
30+
31+
const textNode = iframeDocument.createTextNode( 'foo' );
32+
33+
expect( isText( textNode ) ).to.equal( true );
34+
35+
iframe.remove();
36+
done();
37+
} );
38+
39+
document.body.appendChild( iframe );
40+
} );
41+
} );

0 commit comments

Comments
 (0)