Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge branch t/ckeditor5-engine/532
Browse files Browse the repository at this point in the history
Internal: Aligned Schema API use to the new Schema API.
  • Loading branch information
Reinmar committed Jan 1, 2018
2 parents cb7b739 + 726ed1b commit 74bed89
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 34 deletions.
6 changes: 1 addition & 5 deletions src/headingcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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 );
}
4 changes: 3 additions & 1 deletion src/headingengine.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export default class HeadingEngine extends Plugin {
// Skip paragraph - it is defined in required Paragraph feature.
if ( option.modelElement !== defaultModelElement ) {
// Schema.
editor.model.schema.registerItem( option.modelElement, '$block' );
editor.model.schema.register( option.modelElement, {
inheritAllFrom: '$block'
} );

// Build converter from model to view for data and editing pipelines.
buildModelConverter().for( data.modelToView, editing.modelToView )
Expand Down
52 changes: 38 additions & 14 deletions tests/headingcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.modelElement ] = new HeadingCommand( editor, option.modelElement );
schema.registerItem( option.modelElement, '$block' );
schema.register( option.modelElement, { 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();
} );
Expand Down Expand Up @@ -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();
Expand All @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
28 changes: 14 additions & 14 deletions tests/headingengine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -114,12 +114,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;
} );
} );
} );
Expand Down

0 comments on commit 74bed89

Please sign in to comment.