Browse Source

Do not trigger resize observer's callback when the observed element is not a child of body.

Aleksander Nowodzinski 6 năm trước cách đây
mục cha
commit
dabb0e3240

+ 4 - 0
packages/ckeditor5-utils/src/dom/getresizeobserver.js

@@ -206,6 +206,10 @@ class ResizeObserverPolyfill {
 	 * @returns {Boolean}
 	 */
 	_hasRectChanged( element ) {
+		if ( !element.ownerDocument.body.contains( element ) ) {
+			return false;
+		}
+
 		const currentRect = new Rect( element );
 		const previousRect = this._previousRects.get( element );
 

+ 11 - 1
packages/ckeditor5-utils/tests/dom/getresizeobserver.js

@@ -3,7 +3,7 @@
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  */
 
-/* globals document, setTimeout, Event */
+/* globals document, setTimeout, Event, console */
 
 import getResizeObserver from '../../src/dom/getresizeobserver';
 import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
@@ -89,6 +89,16 @@ describe( 'getResizeObserver()', () => {
 				expect( contentRect ).to.deep.equal( elementRectA );
 			} );
 
+			it( 'does not execute the callback if element has no parent in DOM', () => {
+				const warnSpy = testUtils.sinon.spy( console, 'warn' );
+
+				elementA.remove();
+				observer.observe( elementA );
+
+				sinon.assert.notCalled( callback );
+				sinon.assert.notCalled( warnSpy );
+			} );
+
 			it( 'starts periodic check and asynchronously does not execute the callback if the element rect is the same', done => {
 				observer.observe( elementA );