Browse Source

Merge branch 'master' into t/ckeditor5-engine/1198

# Conflicts:
#	src/headingengine.js
#	tests/headingcommand.js
Maciej Gołaszewski 8 years ago
parent
commit
e077a31d1f

+ 2 - 1
packages/ckeditor5-heading/README.md

@@ -5,7 +5,8 @@ CKEditor 5 headings feature
 [![npm version](https://badge.fury.io/js/%40ckeditor%2Fckeditor5-heading.svg)](https://www.npmjs.com/package/@ckeditor/ckeditor5-heading)
 [![Build Status](https://travis-ci.org/ckeditor/ckeditor5-heading.svg?branch=master)](https://travis-ci.org/ckeditor/ckeditor5-heading)
 [![BrowserStack Status](https://www.browserstack.com/automate/badge.svg?badge_key=d3hvenZqQVZERFQ5d09FWXdyT0ozVXhLaVltRFRjTTUyZGpvQWNmWVhUUT0tLUZqNlJ1YWRUd0RvdEVOaEptM1B2Q0E9PQ==--c9d3dee40b9b4471ff3fb516d9ecf8d09292c7e0)](https://www.browserstack.com/automate/public-build/d3hvenZqQVZERFQ5d09FWXdyT0ozVXhLaVltRFRjTTUyZGpvQWNmWVhUUT0tLUZqNlJ1YWRUd0RvdEVOaEptM1B2Q0E9PQ==--c9d3dee40b9b4471ff3fb516d9ecf8d09292c7e0)
-[![Test Coverage](https://codeclimate.com/github/ckeditor/ckeditor5-heading/badges/coverage.svg)](https://codeclimate.com/github/ckeditor/ckeditor5-heading/coverage)
+[![Coverage Status](https://coveralls.io/repos/github/ckeditor/ckeditor5-heading/badge.svg?branch=master)](https://coveralls.io/github/ckeditor/ckeditor5-heading?branch=master)
+<br>
 [![Dependency Status](https://david-dm.org/ckeditor/ckeditor5-heading/status.svg)](https://david-dm.org/ckeditor/ckeditor5-heading)
 [![devDependency Status](https://david-dm.org/ckeditor/ckeditor5-heading/dev-status.svg)](https://david-dm.org/ckeditor/ckeditor5-heading?type=dev)
 

+ 1 - 5
packages/ckeditor5-heading/src/headingcommand.js

@@ -8,7 +8,6 @@
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
-import Position from '@ckeditor/ckeditor5-engine/src/model/position';
 import first from '@ckeditor/ckeditor5-utils/src/first';
 
 /**
@@ -87,8 +86,5 @@ export default class HeadingCommand extends Command {
 // @param {module:engine/model/schema~Schema} schema The schema of the document.
 // @returns {Boolean}
 function checkCanBecomeHeading( block, heading, schema ) {
-	return schema.check( {
-		name: heading,
-		inside: Position.createBefore( block )
-	} );
+	return schema.checkChild( block.parent, heading ) && !schema.isObject( block );
 }

+ 3 - 1
packages/ckeditor5-heading/src/headingengine.js

@@ -61,7 +61,9 @@ export default class HeadingEngine extends Plugin {
 			// Skip paragraph - it is defined in required Paragraph feature.
 			if ( option.model !== defaultModelElement ) {
 				// Schema.
-				editor.model.schema.registerItem( option.model, '$block' );
+				editor.model.schema.register( option.model, {
+					inheritAllFrom: '$block'
+				} );
 
 				// Build converter from model to view for data and editing pipelines.
 				modelElementToViewContainerElement( option, [ data.modelToView, editing.modelToView ] );

+ 38 - 14
packages/ckeditor5-heading/tests/headingcommand.js

@@ -27,16 +27,16 @@ describe( 'HeadingCommand', () => {
 			schema = model.schema;
 
 			editor.commands.add( 'paragraph', new ParagraphCommand( editor ) );
-			schema.registerItem( 'paragraph', '$block' );
+			schema.register( 'paragraph', { inheritAllFrom: '$block' } );
 
 			for ( const option of options ) {
 				commands[ option.model ] = new HeadingCommand( editor, option.model );
-				schema.registerItem( option.model, '$block' );
+				schema.register( option.model, { inheritAllFrom: '$block' } );
 			}
 
-			schema.registerItem( 'notBlock' );
-			schema.allow( { name: 'notBlock', inside: '$root' } );
-			schema.allow( { name: '$text', inside: 'notBlock' } );
+			schema.register( 'notBlock' );
+			schema.extend( 'notBlock', { allowIn: '$root' } );
+			schema.extend( '$text', { allowIn: 'notBlock' } );
 
 			root = document.getRoot();
 		} );
@@ -112,19 +112,20 @@ describe( 'HeadingCommand', () => {
 		} );
 
 		// https://github.com/ckeditor/ckeditor5-heading/issues/73
-		it( 'should not rename blocks which cannot become headings', () => {
-			schema.registerItem( 'restricted' );
-			schema.allow( { name: 'restricted', inside: '$root' } );
-			schema.disallow( { name: 'heading1', inside: 'restricted' } );
+		it( 'should not rename blocks which cannot become headings (heading is not allowed in their parent)', () => {
+			schema.register( 'restricted' );
+			schema.extend( 'restricted', { allowIn: '$root' } );
 
-			schema.registerItem( 'fooBlock', '$block' );
-			schema.allow( { name: 'fooBlock', inside: 'restricted' } );
+			schema.register( 'fooBlock', { inheritAllFrom: '$block' } );
+			schema.extend( 'fooBlock', {
+				allowIn: [ 'restricted', '$root' ]
+			} );
 
 			setData(
 				model,
 				'<paragraph>a[bc</paragraph>' +
 				'<restricted><fooBlock></fooBlock></restricted>' +
-				'<paragraph>de]f</paragraph>'
+				'<fooBlock>de]f</fooBlock>'
 			);
 
 			commands.heading1.execute();
@@ -136,6 +137,29 @@ describe( 'HeadingCommand', () => {
 			);
 		} );
 
+		it( 'should not rename blocks which cannot become headings (block is an object)', () => {
+			schema.register( 'image', {
+				isBlock: true,
+				isObject: true,
+				allowIn: '$root'
+			} );
+
+			setData(
+				model,
+				'<paragraph>a[bc</paragraph>' +
+				'<image></image>' +
+				'<paragraph>de]f</paragraph>'
+			);
+
+			commands.heading1.execute();
+
+			expect( getData( model ) ).to.equal(
+				'<heading1>a[bc</heading1>' +
+				'<image></image>' +
+				'<heading1>de]f</heading1>'
+			);
+		} );
+
 		it( 'should use parent batch', () => {
 			const command = commands.heading1;
 
@@ -166,8 +190,8 @@ describe( 'HeadingCommand', () => {
 			} );
 
 			it( 'converts topmost blocks', () => {
-				schema.registerItem( 'inlineImage', '$inline' );
-				schema.allow( { name: '$text', inside: 'inlineImage' } );
+				schema.register( 'inlineImage', { allowWhere: '$text' } );
+				schema.extend( '$text', { allowIn: 'inlineImage' } );
 
 				setData( model, '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
 				commands.heading1.execute();

+ 14 - 14
packages/ckeditor5-heading/tests/headingengine.js

@@ -33,18 +33,18 @@ describe( 'HeadingEngine', () => {
 	} );
 
 	it( 'should set proper schema rules', () => {
-		expect( model.schema.hasItem( 'heading1' ) ).to.be.true;
-		expect( model.schema.hasItem( 'heading2' ) ).to.be.true;
-		expect( model.schema.hasItem( 'heading3' ) ).to.be.true;
+		expect( model.schema.isRegistered( 'heading1' ) ).to.be.true;
+		expect( model.schema.isRegistered( 'heading2' ) ).to.be.true;
+		expect( model.schema.isRegistered( 'heading3' ) ).to.be.true;
 
-		expect( model.schema.check( { name: 'heading1', inside: '$root' } ) ).to.be.true;
-		expect( model.schema.check( { name: '$inline', inside: 'heading1' } ) ).to.be.true;
+		expect( model.schema.checkChild( [ '$root' ], 'heading1' ) ).to.be.true;
+		expect( model.schema.checkChild( [ 'heading1' ], '$text' ) ).to.be.true;
 
-		expect( model.schema.check( { name: 'heading2', inside: '$root' } ) ).to.be.true;
-		expect( model.schema.check( { name: '$inline', inside: 'heading2' } ) ).to.be.true;
+		expect( model.schema.checkChild( [ '$root' ], 'heading2' ) ).to.be.true;
+		expect( model.schema.checkChild( [ 'heading2' ], '$text' ) ).to.be.true;
 
-		expect( model.schema.check( { name: 'heading3', inside: '$root' } ) ).to.be.true;
-		expect( model.schema.check( { name: '$inline', inside: 'heading3' } ) ).to.be.true;
+		expect( model.schema.checkChild( [ '$root' ], 'heading3' ) ).to.be.true;
+		expect( model.schema.checkChild( [ 'heading3' ], '$text' ) ).to.be.true;
 	} );
 
 	it( 'should register #commands', () => {
@@ -156,12 +156,12 @@ describe( 'HeadingEngine', () => {
 						expect( editor.commands.get( 'h4' ) ).to.be.instanceOf( HeadingCommand );
 						expect( editor.commands.get( 'paragraph' ) ).to.be.instanceOf( ParagraphCommand );
 
-						expect( model.schema.hasItem( 'paragraph' ) ).to.be.true;
-						expect( model.schema.hasItem( 'h4' ) ).to.be.true;
+						expect( model.schema.isRegistered( 'paragraph' ) ).to.be.true;
+						expect( model.schema.isRegistered( 'h4' ) ).to.be.true;
 
-						expect( model.schema.hasItem( 'heading1' ) ).to.be.false;
-						expect( model.schema.hasItem( 'heading2' ) ).to.be.false;
-						expect( model.schema.hasItem( 'heading3' ) ).to.be.false;
+						expect( model.schema.isRegistered( 'heading1' ) ).to.be.false;
+						expect( model.schema.isRegistered( 'heading2' ) ).to.be.false;
+						expect( model.schema.isRegistered( 'heading3' ) ).to.be.false;
 					} );
 			} );
 		} );