Browse Source

Made sure the toolbar does not cause console.warns when detached from visible DOM.

Aleksander Nowodzinski 6 years ago
parent
commit
5c6ad5a25e

+ 8 - 3
packages/ckeditor5-ui/src/toolbar/toolbarview.js

@@ -417,12 +417,17 @@ export default class ToolbarView extends View {
 			return;
 		}
 
+		// Do no grouping–related geometry analysis when the toolbar is detached from visible DOM,
+		// for instance before #render(), or after render but without a parent or a parent detached
+		// from DOM. DOMRects won't work anyway and there will be tons of warning in the console and
+		// nothing else.
+		if ( !this.element.ownerDocument.body.contains( this.element ) ) {
+			return;
+		}
+
 		// There's no way to make any decisions concerning geometry when there is no element to work with
 		// (before #render()). Or when element has no parent because ClientRects won't work when
 		// #element is not in DOM.
-		if ( !this.element || !this.element.parentNode ) {
-			return;
-		}
 
 		this._groupWhenFullLock = true;
 

+ 10 - 4
packages/ckeditor5-ui/tests/toolbar/toolbarview.js

@@ -636,12 +636,18 @@ describe( 'ToolbarView', () => {
 			expect( view.groupedItems ).to.be.null;
 		} );
 
-		it( 'does not throw when the view element has no geometry', () => {
+		it( 'stays silent if the toolbar is detached from visible DOM', () => {
+			testUtils.sinon.spy( console, 'warn' );
 			view.element.remove();
 
-			expect( () => {
-				view.updateGroupedItems();
-			} ).to.not.throw();
+			view.items.add( focusable() );
+			view.items.add( focusable() );
+			view.items.add( focusable() );
+			view.items.add( focusable() );
+
+			view.shouldGroupWhenFull = true;
+
+			sinon.assert.notCalled( console.warn );
 		} );
 
 		it( 'does not group when items fit', () => {