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 0bd12c2 + c6311bc commit cb93002
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 64 deletions.
12 changes: 3 additions & 9 deletions src/blockquotecommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,9 @@ function getRangesOfBlockGroups( blocks ) {

// Checks whether <bQ> can wrap the block.
function checkCanBeQuoted( schema, block ) {
const isBQAllowed = schema.check( {
name: 'blockQuote',
inside: Position.createBefore( block )
} );
const isBlockAllowedInBQ = schema.check( {
name: block.name,
attributes: Array.from( block.getAttributeKeys() ),
inside: 'blockQuote'
} );
// TMP will be replaced with schema.checkWrap().
const isBQAllowed = schema.checkChild( block.parent, 'blockQuote' );
const isBlockAllowedInBQ = schema.checkChild( [ '$root', 'blockQuote' ], block );

return isBQAllowed && isBlockAllowedInBQ;
}
36 changes: 16 additions & 20 deletions src/blockquoteengine.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,22 @@ export default class BlockQuoteEngine extends Plugin {

editor.commands.add( 'blockQuote', new BlockQuoteCommand( editor ) );

schema.registerItem( 'blockQuote' );
schema.allow( { name: 'blockQuote', inside: '$root' } );
schema.allow( { name: '$block', inside: 'blockQuote' } );
schema.register( 'blockQuote', {
allowWhere: '$block',
allowContentOf: '$root'
} );

// Disallow blockQuote in blockQuote.
schema.on( 'checkChild', ( evt, args ) => {
const ctx = args[ 0 ];
const child = args[ 1 ];
const childRule = schema.getDefinition( child );

if ( childRule && childRule.name == 'blockQuote' && ctx.endsWith( 'blockQuote' ) ) {
evt.stop();
evt.return = false;
}
}, { priority: 'high' } );

buildViewConverter().for( editor.data.viewToModel )
.fromElement( 'blockquote' )
Expand All @@ -43,21 +56,4 @@ export default class BlockQuoteEngine extends Plugin {
.fromElement( 'blockQuote' )
.toElement( 'blockquote' );
}

/**
* @inheritDoc
*/
afterInit() {
const schema = this.editor.model.schema;

// TODO
// Workaround for https://github.com/ckeditor/ckeditor5-engine/issues/532#issuecomment-280924650.
if ( schema.hasItem( 'listItem' ) ) {
schema.allow( {
name: 'listItem',
inside: 'blockQuote',
attributes: [ 'type', 'indent' ]
} );
}
}
}
47 changes: 32 additions & 15 deletions tests/blockquotecommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ describe( 'BlockQuoteCommand', () => {

model = editor.model;

model.schema.registerItem( 'paragraph', '$block' );
model.schema.registerItem( 'heading', '$block' );
model.schema.registerItem( 'widget' );
model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
model.schema.register( 'heading', { inheritAllFrom: '$block' } );
model.schema.register( 'widget' );

model.schema.allow( { name: 'widget', inside: '$root' } );
model.schema.allow( { name: '$text', inside: 'widget' } );

model.schema.limits.add( 'widget' );
model.schema.extend( 'widget', {
allowIn: '$root',
isLimit: true
} );
model.schema.extend( '$text', { allowIn: 'widget' } );

buildModelConverter().for( editor.editing.modelToView )
.fromElement( 'paragraph' )
Expand Down Expand Up @@ -75,7 +76,7 @@ describe( 'BlockQuoteCommand', () => {
} );

it( 'is false when selection starts in a blockless space', () => {
model.schema.allow( { name: '$text', inside: '$root' } );
model.schema.extend( '$text', { allowIn: '$root' } );

setModelData( model, 'x[]x' );

Expand Down Expand Up @@ -124,7 +125,14 @@ describe( 'BlockQuoteCommand', () => {
'is false when selection is in an element which cannot be wrapped with blockQuote' +
'(because mQ is not allowed in its parent)',
() => {
model.schema.disallow( { name: 'blockQuote', inside: '$root' } );
model.schema.on( 'checkChild', ( evt, args ) => {
const def = model.schema.getDefinition( args[ 1 ] );

if ( def.name == 'blockQuote' ) {
evt.stop();
evt.return = false;
}
} );

setModelData( model, '<paragraph>x[]x</paragraph>' );

Expand Down Expand Up @@ -371,8 +379,17 @@ describe( 'BlockQuoteCommand', () => {

it( 'should not wrap a block which can not be in a quote', () => {
// blockQuote is allowed in root, but fooBlock can not be inside blockQuote.
model.schema.registerItem( 'fooBlock', '$block' );
model.schema.disallow( { name: 'fooBlock', inside: 'blockQuote' } );
model.schema.register( 'fooBlock', { inheritAllFrom: '$block' } );
model.schema.on( 'checkChild', ( evt, args ) => {
const def = model.schema.getDefinition( args[ 1 ] );
const ctx = args[ 0 ];

if ( ctx.endsWith( 'blockQuote' ) && def.name == 'fooBlock' ) {
evt.stop();
evt.return = false;
}
} );

buildModelConverter().for( editor.editing.modelToView )
.fromElement( 'fooBlock' )
.toElement( 'fooblock' );
Expand All @@ -399,11 +416,11 @@ describe( 'BlockQuoteCommand', () => {

it( 'should not wrap a block which parent does not allow quote inside itself', () => {
// blockQuote is not be allowed in fooWrapper, but fooBlock can be inside blockQuote.
model.schema.registerItem( 'fooWrapper' );
model.schema.registerItem( 'fooBlock', '$block' );
model.schema.register( 'fooWrapper' );
model.schema.register( 'fooBlock', { inheritAllFrom: '$block' } );

model.schema.allow( { name: 'fooWrapper', inside: '$root' } );
model.schema.allow( { name: 'fooBlock', inside: 'fooWrapper' } );
model.schema.extend( 'fooWrapper', { allowIn: '$root' } );
model.schema.extend( 'fooBlock', { allowIn: 'fooWrapper' } );

buildModelConverter().for( editor.editing.modelToView )
.fromElement( 'fooWrapper' )
Expand Down
14 changes: 11 additions & 3 deletions tests/blockquoteengine.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,20 @@ describe( 'BlockQuoteEngine', () => {
} );

it( 'allows for blockQuote in the $root', () => {
expect( model.schema.check( { name: 'blockQuote', inside: '$root' } ) ).to.be.true;
expect( model.schema.checkChild( [ '$root' ], 'blockQuote' ) ).to.be.true;
} );

it( 'allows for $block in blockQuote', () => {
expect( model.schema.check( { name: '$block', inside: 'blockQuote' } ) ).to.be.true;
expect( model.schema.check( { name: 'paragraph', inside: 'blockQuote' } ) ).to.be.true;
expect( model.schema.checkChild( [ '$root', 'blockQuote' ], '$block' ) ).to.be.true;
expect( model.schema.checkChild( [ '$root', 'blockQuote' ], 'paragraph' ) ).to.be.true;
} );

it( 'does not allow for blockQuote in blockQuote', () => {
expect( model.schema.checkChild( [ '$root', 'blockQuote' ], 'blockQuote' ) ).to.be.false;
} );

it( 'does not break when checking an unregisterd item', () => {
expect( model.schema.checkChild( [ '$root', 'blockQuote' ], 'foo' ) ).to.be.false;
} );

it( 'adds converters to the data pipeline', () => {
Expand Down
42 changes: 25 additions & 17 deletions tests/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Delete from '@ckeditor/ckeditor5-typing/src/delete';
import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';

describe( 'BlockQuote', () => {
describe( 'BlockQuote integration', () => {
let editor, model, element;

beforeEach( () => {
Expand Down Expand Up @@ -310,8 +310,10 @@ describe( 'BlockQuote', () => {
} );
} );

// Historically, due to problems with schema, images were not quotable.
// These tests were left here to confirm that after schema was fixed, images are properly quotable.
describe( 'compatibility with images', () => {
it( 'does not quote a simple image', () => {
it( 'quotes a simple image', () => {
const element = document.createElement( 'div' );
document.body.appendChild( element );

Expand All @@ -330,17 +332,19 @@ describe( 'BlockQuote', () => {
editor.execute( 'blockQuote' );

expect( getModelData( editor.model ) ).to.equal(
'<blockQuote><paragraph>fo[o</paragraph></blockQuote>' +
'<image src="foo.png"></image>' +
'<blockQuote><paragraph>b]ar</paragraph></blockQuote>'
'<blockQuote>' +
'<paragraph>fo[o</paragraph>' +
'<image src="foo.png"></image>' +
'<paragraph>b]ar</paragraph>' +
'</blockQuote>'
);

element.remove();
return editor.destroy();
} );
} );

it( 'does not quote an image with caption', () => {
it( 'quotes an image with caption', () => {
setModelData( model,
'<paragraph>fo[o</paragraph>' +
'<image src="foo.png">' +
Expand All @@ -352,15 +356,17 @@ describe( 'BlockQuote', () => {
editor.execute( 'blockQuote' );

expect( getModelData( model ) ).to.equal(
'<blockQuote><paragraph>fo[o</paragraph></blockQuote>' +
'<image src="foo.png">' +
'<caption>xxx</caption>' +
'</image>' +
'<blockQuote><paragraph>b]ar</paragraph></blockQuote>'
'<blockQuote>' +
'<paragraph>fo[o</paragraph>' +
'<image src="foo.png">' +
'<caption>xxx</caption>' +
'</image>' +
'<paragraph>b]ar</paragraph>' +
'</blockQuote>'
);
} );

it( 'does not add an image to existing quote', () => {
it( 'adds an image to an existing quote', () => {
setModelData( model,
'<paragraph>fo[o</paragraph>' +
'<image src="foo.png">' +
Expand All @@ -372,11 +378,13 @@ describe( 'BlockQuote', () => {
editor.execute( 'blockQuote' );

expect( getModelData( model ) ).to.equal(
'<blockQuote><paragraph>fo[o</paragraph></blockQuote>' +
'<image src="foo.png">' +
'<caption>xxx</caption>' +
'</image>' +
'<blockQuote><paragraph>b]ar</paragraph></blockQuote>'
'<blockQuote>' +
'<paragraph>fo[o</paragraph>' +
'<image src="foo.png">' +
'<caption>xxx</caption>' +
'</image>' +
'<paragraph>b]ar</paragraph>' +
'</blockQuote>'
);
} );
} );
Expand Down

0 comments on commit cb93002

Please sign in to comment.