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

Commit

Permalink
Merge pull request #218 from ckeditor/t/214
Browse files Browse the repository at this point in the history
Other: Introduced the `isText()` helper. Closes #214.
  • Loading branch information
Reinmar authored Feb 1, 2018
2 parents 46ef539 + 796bb69 commit a9a6bec
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
18 changes: 18 additions & 0 deletions src/dom/istext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/

/**
* @module utils/dom/istext
*/

/**
* Checks if the object is a native DOM Text node.
*
* @param {*} obj
* @returns {Boolean}
*/
export default function isText( obj ) {
return Object.prototype.toString.call( obj ) == '[object Text]';
}
5 changes: 2 additions & 3 deletions src/dom/rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
* @module utils/dom/rect
*/

/* global Node */

import isRange from './isrange';
import isWindow from './iswindow';
import isElement from '../lib/lodash/isElement';
import getBorderWidths from './getborderwidths';
import log from '../log';
import isText from './istext';

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

if ( startContainer.nodeType === Node.TEXT_NODE ) {
if ( isText( startContainer ) ) {
startContainer = startContainer.parentNode;
}

Expand Down
5 changes: 2 additions & 3 deletions src/dom/scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
* For licensing, see LICENSE.md.
*/

/* global Node */

/**
* @module utils/dom/scroll
*/

import isRange from './isrange';
import Rect from './rect';
import isText from './istext';

const utils = {};

Expand Down Expand Up @@ -255,7 +254,7 @@ function getParentElement( elementOrRange ) {
let parent = elementOrRange.commonAncestorContainer;

// If a Range is attached to the Text, use the closest element ancestor.
if ( parent.nodeType == Node.TEXT_NODE ) {
if ( isText( parent ) ) {
parent = parent.parentNode;
}

Expand Down
41 changes: 41 additions & 0 deletions tests/dom/istext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/

/* global document, Text */

import isText from '../../src/dom/istext';

describe( 'isText()', () => {
it( 'detects native DOM Text', () => {
expect( isText( new Text( 'foo' ) ) ).to.be.true;

expect( isText( 'foo' ) ).to.be.false;
expect( isText( {} ) ).to.be.false;
expect( isText( null ) ).to.be.false;
expect( isText( undefined ) ).to.be.false;
expect( isText( new Date() ) ).to.be.false;
expect( isText( 42 ) ).to.be.false;
expect( isText( document.createElement( 'div' ) ) ).to.be.false;
expect( isText( document.createDocumentFragment() ) ).to.be.false;
expect( isText( document.createComment( 'a' ) ) ).to.be.false;
} );

it( 'works for texts in an iframe', done => {
const iframe = document.createElement( 'iframe' );

iframe.addEventListener( 'load', () => {
const iframeDocument = iframe.contentWindow.document;

const textNode = iframeDocument.createTextNode( 'foo' );

expect( isText( textNode ) ).to.equal( true );

iframe.remove();
done();
} );

document.body.appendChild( iframe );
} );
} );

0 comments on commit a9a6bec

Please sign in to comment.