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

Commit

Permalink
Warn about rect-source-not-in-dom only in CK_DEBUG mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Jasiun committed Jul 19, 2019
1 parent 41d6884 commit 38198d0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 45 deletions.
32 changes: 10 additions & 22 deletions src/dom/rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
* @module utils/dom/rect
*/

/* globals console */

import isRange from './isrange';
import isWindow from './iswindow';
import getBorderWidths from './getborderwidths';
import isText from './istext';
import { isElement } from 'lodash-es';
import { attachLinkToDocumentation } from '../ckeditorerror';

const rectProperties = [ 'top', 'right', 'bottom', 'left', 'width', 'height' ];

Expand Down Expand Up @@ -69,25 +66,16 @@ export default class Rect {
} );

if ( isElement( source ) || isSourceRange ) {
const sourceNode = isSourceRange ? source.startContainer : source;

if ( !sourceNode.ownerDocument || !sourceNode.ownerDocument.body.contains( sourceNode ) ) {
/**
* The `Rect` class depends on `getBoundingClientRect` and `getClientRects` DOM methods.
* If the {@link #constructor source} of a rect in an HTML element or a DOM range but it does
* not belong to any rendered DOM tree, these methods will fail to obtain the geometry and
* the rect instance makes little sense to the features using it.
*
* To get rid of this warning make sure the source passed to the constructor
* is a descendant of `window.document.body`.
*
* @error rect-source-not-in-dom
* @param {String} source The source of the Rect instance.
*/
console.warn( attachLinkToDocumentation(
'rect-source-not-in-dom: The source of this rect does not belong to any rendered DOM tree.',
), { source } );
}
// The `Rect` class depends on `getBoundingClientRect` and `getClientRects` DOM methods. If the source
// of a rect in an HTML element or a DOM range but it does not belong to any rendered DOM tree, these methods
// will fail to obtain the geometry and the rect instance makes little sense to the features using it.
// To get rid of this warning make sure the source passed to the constructor is a descendant of `window.document.body`.
// @if CK_DEBUG // const sourceNode = isSourceRange ? source.startContainer : source;
// @if CK_DEBUG // if ( !sourceNode.ownerDocument || !sourceNode.ownerDocument.body.contains( sourceNode ) ) {
// @if CK_DEBUG // console.warn(
// @if CK_DEBUG // 'rect-source-not-in-dom: The source of this rect does not belong to any rendered DOM tree.',
// @if CK_DEBUG // { source } );
// @if CK_DEBUG // }

if ( isSourceRange ) {
copyRectProperties( this, Rect.getDomRangeRects( source )[ 0 ] );
Expand Down
23 changes: 0 additions & 23 deletions tests/dom/rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,29 +161,6 @@ describe( 'Rect', () => {

assertRect( geometry, sourceGeometry );
} );

it( 'should warn if the source does not belong to rendered DOM tree (HTML element)', () => {
const element = document.createElement( 'div' );

sinon.stub( element, 'getBoundingClientRect' ).returns( geometry );

const rect = new Rect( element );
sinon.assert.calledOnce( console.warn );
sinon.assert.calledWithExactly( console.warn, sinon.match( /^rect-source-not-in-dom/ ), { source: element } );
assertRect( rect, geometry );
} );

it( 'should warn if the source does not belong to rendered DOM tree (DOM Range)', () => {
const range = document.createRange();

range.collapse();
sinon.stub( range, 'getClientRects' ).returns( [ geometry ] );

const rect = new Rect( range );
sinon.assert.calledOnce( console.warn );
sinon.assert.calledWithExactly( console.warn, sinon.match( /^rect-source-not-in-dom/ ), { source: range } );
assertRect( rect, geometry );
} );
} );

describe( 'clone()', () => {
Expand Down

0 comments on commit 38198d0

Please sign in to comment.