Przeglądaj źródła

Third batch of changes aligning schema use to the new API.

Piotrek Koszuliński 8 lat temu
rodzic
commit
5aa760b000

+ 1 - 1
packages/ckeditor5-block-quote/src/blockquoteengine.js

@@ -42,7 +42,7 @@ export default class BlockQuoteEngine extends Plugin {
 			const child = args[ 1 ];
 			const childRule = schema.getRule( child );
 
-			if ( childRule.name == 'blockQuote' && ctx.matchEnd( 'blockQuote' ) ) {
+			if ( childRule && childRule.name == 'blockQuote' && ctx.matchEnd( 'blockQuote' ) ) {
 				evt.stop();
 				evt.return = false;
 			}

+ 18 - 2
packages/ckeditor5-block-quote/tests/blockquotecommand.js

@@ -125,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.xdisallow( 'blockQuote', { allowIn: '$root' } );
+				model.schema.on( 'checkChild', ( evt, args ) => {
+					const rule = model.schema.getRule( args[ 1 ] );
+
+					if ( rule.name == 'blockQuote' ) {
+						evt.stop();
+						evt.return = false;
+					}
+				} );
 
 				setModelData( model, '<paragraph>x[]x</paragraph>' );
 
@@ -373,7 +380,16 @@ 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.register( 'fooBlock', { inheritAllFrom: '$block' } );
-				model.schema.xdisallow( 'fooBlock', { allowIn: 'blockQuote' } );
+				model.schema.on( 'checkChild', ( evt, args ) => {
+					const rule = model.schema.getRule( args[ 1 ] );
+					const ctx = args[ 0 ];
+
+					if ( ctx.matchEnd( 'blockQuote' ) && rule.name == 'fooBlock' ) {
+						evt.stop();
+						evt.return = false;
+					}
+				} );
+
 				buildModelConverter().for( editor.editing.modelToView )
 					.fromElement( 'fooBlock' )
 					.toElement( 'fooblock' );

+ 10 - 2
packages/ckeditor5-block-quote/tests/blockquoteengine.js

@@ -39,8 +39,16 @@ describe( 'BlockQuoteEngine', () => {
 	} );
 
 	it( 'allows for $block in blockQuote', () => {
-		expect( model.schema.checkChild( [ 'blockQuote' ], '$block' ) ).to.be.true;
-		expect( model.schema.checkChild( [ 'blockQuote' ], 'paragraph' ) ).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>'
 			);
 		} );
 	} );