Bladeren bron

Made the selectionRootObtainer helper bulletproof when selection has no #editableElement.

Aleksander Nowodzinski 8 jaren geleden
bovenliggende
commit
a5f4abaf4f

+ 6 - 1
packages/ckeditor5-ui/src/selectionrootobtainer.js

@@ -18,7 +18,12 @@
 export default function selectionRootObtainer( editor ) {
 	return () => {
 		const view = editor.editing.view;
+		const editableElement = view.selection.editableElement;
 
-		return view.domConverter.mapViewToDom( view.selection.editableElement.root );
+		if ( editableElement ) {
+			return view.domConverter.mapViewToDom( editableElement.root );
+		}
+
+		return null;
 	};
 }

+ 7 - 0
packages/ckeditor5-ui/tests/selectionrootobtainer.js

@@ -46,6 +46,13 @@ describe( 'selectionRootObtainer()', () => {
 		expect( obtainer() ).to.equal( viewDocument.domConverter.mapViewToDom( root ) );
 	} );
 
+	it( 'does not fail if selection has no #editableElement', () => {
+		const obtainer = selectionRootObtainer( editor );
+
+		sinon.stub( viewDocument.selection, 'editableElement' ).value( null );
+		expect( obtainer() ).to.equal( null );
+	} );
+
 	it( 'obtains the farthest root of the selection (nested editable)', () => {
 		doc.schema.registerItem( 'widget' );
 		doc.schema.registerItem( 'nestededitable' );