Przeglądaj źródła

Merge branch t/ckeditor5-engine/532

Internal: Aligned Schema API use to the new Schema API.
Piotrek Koszuliński 8 lat temu
rodzic
commit
211f658927

+ 3 - 9
packages/ckeditor5-block-quote/src/blockquotecommand.js

@@ -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;
 }

+ 16 - 20
packages/ckeditor5-block-quote/src/blockquoteengine.js

@@ -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' )
@@ -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' ]
-			} );
-		}
-	}
 }

+ 32 - 15
packages/ckeditor5-block-quote/tests/blockquotecommand.js

@@ -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' )
@@ -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' );
 
@@ -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>' );
 
@@ -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' );
@@ -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' )

+ 11 - 3
packages/ckeditor5-block-quote/tests/blockquoteengine.js

@@ -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', () => {

+ 25 - 17
packages/ckeditor5-block-quote/tests/integration.js

@@ -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( () => {
@@ -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 );
 
@@ -330,9 +332,11 @@ 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();
@@ -340,7 +344,7 @@ describe( 'BlockQuote', () => {
 				} );
 		} );
 
-		it( 'does not quote an image with caption', () => {
+		it( 'quotes an image with caption', () => {
 			setModelData( model,
 				'<paragraph>fo[o</paragraph>' +
 				'<image src="foo.png">' +
@@ -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">' +
@@ -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>'
 			);
 		} );
 	} );