Bladeren bron

Using VirtualTest editor in tests. Improved FormatsCommand.

Szymon Kupś 9 jaren geleden
bovenliggende
commit
0e916ef79a

+ 19 - 10
packages/ckeditor5-heading/src/formatscommand.js

@@ -6,6 +6,7 @@
 'use strict';
 
 import Command from '../command/command.js';
+import RootElement from '../engine/model/rootelement.js';
 
 export default class FormatsCommand extends Command {
 	constructor( editor, formats ) {
@@ -45,14 +46,17 @@ export default class FormatsCommand extends Command {
 
 		// Collect elements to change format.
 		if ( selection.isCollapsed ) {
-			elements.push( _findTopmostBlock( startPosition.parent ) );
+			const block = _findTopmostBlock( startPosition );
+
+			if ( block ) {
+				elements.push( block );
+			}
 		} else {
 			const ranges = selection.getRanges();
 
 			for ( let range in ranges ) {
-				// Get topmost blocks element from start and end range position and adds all elements between them.
-				let startBlock = _findTopmostBlock( range.start.parent );
-				const endBlock = _findTopmostBlock( range.end.parent );
+				let startBlock = _findTopmostBlock( range.start );
+				const endBlock = _findTopmostBlock( range.end, false );
 
 				elements.push( startBlock );
 
@@ -63,8 +67,6 @@ export default class FormatsCommand extends Command {
 			}
 		}
 
-		//TODO: when selection is not collapsed - gather all elements that needs to be renamed.
-
 		document.enqueueChanges( () => {
 			const batch = document.batch();
 
@@ -96,10 +98,17 @@ export default class FormatsCommand extends Command {
 	}
 }
 
-function _findTopmostBlock( element ) {
-	while ( element.parent.name !== '$root' ) {
-		element = element.parent;
+function _findTopmostBlock( position, nodeAfter = true ) {
+	let parent = position.parent;
+
+	// If position is placed inside root - get element after/before it.
+	if ( parent instanceof RootElement ) {
+		return nodeAfter ? position.nodeAfter : position.nodeBefore ;
+	}
+
+	while ( !( parent instanceof RootElement ) ) {
+		parent = parent.parent;
 	}
 
-	return element;
+	return parent;
 }

+ 3 - 3
packages/ckeditor5-heading/tests/formats.js

@@ -21,9 +21,9 @@ describe( 'Formats', () => {
 			features: [ Formats ],
 			toolbar: [ 'formats' ]
 		} )
-			.then( newEditor => {
-				editor = newEditor;
-			} );
+		.then( newEditor => {
+			editor = newEditor;
+		} );
 	} );
 
 	afterEach( () => {

+ 14 - 11
packages/ckeditor5-heading/tests/formatscommand.js

@@ -5,7 +5,7 @@
 
 'use strict';
 
-import StandardEditor from '/ckeditor5/editor/standardeditor.js';
+import VirtualTestEditor from '/tests/ckeditor5/_utils/virtualtesteditor.js';
 import FormatsCommand from '/ckeditor5/formats/formatscommand.js';
 import Range from '/ckeditor5/engine/model/range.js';
 import { setData, getData } from '/tests/engine/_utils/model.js';
@@ -21,16 +21,19 @@ describe( 'FormatsCommand', () => {
 	let editor, document, command, root;
 
 	beforeEach( () => {
-		editor = new StandardEditor( null );
-		document = editor.document;
-		command = new FormatsCommand( editor, formats );
-		const schema = document.schema;
-
-		for ( let format of formats ) {
-			schema.registerItem( format.id, '$block' );
-		}
-
-		root = document.createRoot();
+		return VirtualTestEditor.create()
+			.then( newEditor => {
+				editor = newEditor;
+				document = editor.document;
+				command = new FormatsCommand( editor, formats );
+				const schema = document.schema;
+
+				for ( let format of formats ) {
+					schema.registerItem( format.id, '$block' );
+				}
+
+				root = document.getRoot();
+			} );
 	} );
 
 	afterEach( () => {

+ 5 - 6
packages/ckeditor5-heading/tests/formatsengine.js

@@ -7,7 +7,7 @@
 
 import FormatsEngine from '/ckeditor5/formats/formatsengine.js';
 import Paragraph from '/ckeditor5/paragraph/paragraph.js';
-import StandardEditor from '/ckeditor5/editor/standardeditor.js';
+import VirtualTestEditor from '/tests/ckeditor5/_utils/virtualtesteditor.js';
 import FormatsCommand from '/ckeditor5/formats/formatscommand.js';
 import { getData } from '/tests/engine/_utils/model.js';
 
@@ -15,13 +15,12 @@ describe( 'FormatsEngine', () => {
 	let editor, document;
 
 	beforeEach( () => {
-		editor = new StandardEditor( null, {
+		return VirtualTestEditor.create( {
 			features: [ FormatsEngine ]
-		} );
-
-		return editor.initPlugins().then( () => {
+		} )
+		.then( newEditor => {
+			editor = newEditor;
 			document = editor.document;
-			document.createRoot( 'main' );
 		} );
 	} );