Explorar o código

Merge branch t/ckeditor5-engine/532

Internal: Aligned Schema API use to the new Schema API.
Piotrek Koszuliński %!s(int64=8) %!d(string=hai) anos
pai
achega
754c9f797c

+ 6 - 10
packages/ckeditor5-paragraph/src/paragraph.js

@@ -50,7 +50,7 @@ export default class Paragraph extends Plugin {
 		editor.commands.add( 'paragraph', new ParagraphCommand( editor ) );
 
 		// Schema.
-		model.schema.registerItem( 'paragraph', '$block' );
+		model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
 
 		// Build converter from model to view for data and editing pipelines.
 		buildModelConverter().for( data.modelToView, editing.modelToView )
@@ -135,11 +135,9 @@ export default class Paragraph extends Plugin {
 			// Only empty roots are in `#_rootsToFix`. Even if root got content during `changesDone` event (because of, for example
 			// other feature), this will fire `findEmptyRoots` and remove that root from `#_rootsToFix`. So we are guaranteed
 			// to have only empty roots here.
-			const query = { name: 'paragraph', inside: [ root ] };
-			const schema = model.schema;
-
+			//
 			// If paragraph element is allowed in the root, create paragraph element.
-			if ( schema.check( query ) ) {
+			if ( model.schema.checkChild( root, 'paragraph' ) ) {
 				model.enqueueChange( batch, writer => {
 					// Remove root from `rootsToFix` here, before executing batch, to prevent infinite loops.
 					this._rootsToFix.delete( root );
@@ -297,21 +295,19 @@ function autoparagraphItems( evt, data, consumable, conversionApi ) {
 }
 
 function isParagraphable( node, context, schema, insideParagraphLikeElement ) {
-	const name = node.name || '$text';
-
 	// Node is paragraphable if it is inside paragraph like element, or...
 	// It is not allowed at this context...
-	if ( !insideParagraphLikeElement && schema.check( { name, inside: context } ) ) {
+	if ( !insideParagraphLikeElement && schema.checkChild( context, node ) ) {
 		return false;
 	}
 
 	// And paragraph is allowed in this context...
-	if ( !schema.check( { name: 'paragraph', inside: context } ) ) {
+	if ( !schema.checkChild( context, 'paragraph' ) ) {
 		return false;
 	}
 
 	// And a node would be allowed in this paragraph...
-	if ( !schema.check( { name, inside: context.concat( 'paragraph' ) } ) ) {
+	if ( !schema.checkChild( [ ...context, 'paragraph' ], node ) ) {
 		return false;
 	}
 

+ 1 - 5
packages/ckeditor5-paragraph/src/paragraphcommand.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';
 
 /**
@@ -69,8 +68,5 @@ export default class ParagraphCommand extends Command {
 // @param {module:engine/model/schema~Schema} schema The schema of the document.
 // @returns {Boolean}
 function checkCanBecomeParagraph( block, schema ) {
-	return schema.check( {
-		name: 'paragraph',
-		inside: Position.createBefore( block )
-	} );
+	return schema.checkChild( block.parent, 'paragraph' ) && !schema.isObject( block );
 }

+ 42 - 32
packages/ckeditor5-paragraph/tests/paragraph.js

@@ -39,9 +39,9 @@ describe( 'Paragraph feature', () => {
 	} );
 
 	it( 'should set proper schema rules', () => {
-		expect( model.schema.hasItem( 'paragraph' ) ).to.be.true;
-		expect( model.schema.check( { name: 'paragraph', inside: '$root' } ) ).to.be.true;
-		expect( model.schema.check( { name: '$inline', inside: 'paragraph' } ) ).to.be.true;
+		expect( model.schema.isRegistered( 'paragraph' ) ).to.be.true;
+		expect( model.schema.checkChild( [ '$root' ], 'paragraph' ) ).to.be.true;
+		expect( model.schema.checkChild( [ 'paragraph' ], '$text' ) ).to.be.true;
 	} );
 
 	it( 'should have a static paragraphLikeElements property', () => {
@@ -79,8 +79,8 @@ describe( 'Paragraph feature', () => {
 			} );
 
 			it( 'should autoparagraph any inline element', () => {
-				editor.model.schema.registerItem( 'span', '$inline' );
-				editor.model.schema.allow( { name: '$text', inside: '$inline' } );
+				editor.model.schema.register( 'span', { allowWhere: '$text' } );
+				editor.model.schema.extend( '$text', { allowIn: 'span' } );
 
 				buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView ).fromElement( 'span' ).toElement( 'span' );
 				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'span' ).toElement( 'span' );
@@ -99,7 +99,7 @@ describe( 'Paragraph feature', () => {
 			} );
 
 			it( 'should not autoparagraph text (in a context which does not allow paragraphs', () => {
-				model.schema.registerItem( 'specialRoot' );
+				model.schema.register( 'specialRoot' );
 
 				const modelFragment = editor.data.parse( 'foo', 'specialRoot' );
 
@@ -108,7 +108,7 @@ describe( 'Paragraph feature', () => {
 			} );
 
 			it( 'should autoparagraph text next to allowed element', () => {
-				model.schema.registerItem( 'heading1', '$block' );
+				model.schema.register( 'heading1', { inheritAllFrom: '$block' } );
 				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
 
 				const modelFragment = editor.data.parse( '<h1>foo</h1>bar<p>bom</p>' );
@@ -132,9 +132,9 @@ describe( 'Paragraph feature', () => {
 			} );
 
 			it( 'should autoparagraph text inside converted container', () => {
-				model.schema.registerItem( 'div' );
-				model.schema.allow( { name: 'div', inside: '$root' } );
-				model.schema.allow( { name: 'paragraph', inside: 'div' } );
+				model.schema.register( 'div' );
+				model.schema.extend( 'div', { allowIn: '$root' } );
+				model.schema.extend( 'paragraph', { allowIn: 'div' } );
 
 				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
 
@@ -148,7 +148,7 @@ describe( 'Paragraph feature', () => {
 			} );
 
 			it( 'should autoparagraph text inside disallowed element next to allowed element', () => {
-				model.schema.registerItem( 'heading1', '$block' );
+				model.schema.register( 'heading1', { inheritAllFrom: '$block' } );
 				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
 
 				const modelFragment = editor.data.parse( '<div><h1>foo</h1>bar</div>' );
@@ -158,7 +158,7 @@ describe( 'Paragraph feature', () => {
 			} );
 
 			it( 'should not autoparagraph text in disallowed element', () => {
-				model.schema.registerItem( 'heading1', '$block' );
+				model.schema.register( 'heading1', { inheritAllFrom: '$block' } );
 				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
 
 				const modelFragment = editor.data.parse( '<h1><b>foo</b>bar</h1>' );
@@ -168,7 +168,14 @@ describe( 'Paragraph feature', () => {
 			} );
 
 			it( 'should not fail when text is not allowed in paragraph', () => {
-				model.schema.disallow( { name: '$text', inside: [ '$root', 'paragraph' ] } );
+				model.schema.on( 'checkChild', ( evt, args ) => {
+					const def = model.schema.getDefinition( args[ 1 ] );
+
+					if ( args[ 0 ].endsWith( '$root paragraph' ) && def.name == '$text' ) {
+						evt.stop();
+						evt.return = false;
+					}
+				} );
 
 				const modelFragment = editor.data.parse( 'foo' );
 
@@ -196,7 +203,8 @@ describe( 'Paragraph feature', () => {
 
 			describe( 'should not strip attribute elements when autoparagraphing texts', () => {
 				beforeEach( () => {
-					model.schema.allow( { name: '$inline', attributes: [ 'bold' ] } );
+					model.schema.extend( '$text', { allowAttributes: 'bold' } );
+
 					buildViewConverter().for( editor.data.viewToModel )
 						.fromElement( 'b' )
 						.toAttribute( 'bold', true );
@@ -209,20 +217,15 @@ describe( 'Paragraph feature', () => {
 				} );
 
 				it( 'inside converted element', () => {
-					model.schema.registerItem( 'blockquote' );
-					model.schema.allow( { name: 'blockquote', inside: '$root' } );
-					model.schema.allow( { name: '$block', inside: 'blockquote' } );
-
-					buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView )
-						.fromElement( 'blockquote' )
-						.toElement( 'blockquote' );
+					model.schema.register( 'blockQuote', { allowIn: '$root' } );
+					model.schema.extend( '$block', { allowIn: 'blockQuote' } );
 
-					buildViewConverter().for( editor.data.viewToModel ).fromElement( 'blockquote' ).toElement( 'blockquote' );
+					buildViewConverter().for( editor.data.viewToModel ).fromElement( 'blockquote' ).toElement( 'blockQuote' );
 
 					const modelFragment = editor.data.parse( '<blockquote>foo<b>bar</b>bom</blockquote>' );
 
 					expect( stringifyModel( modelFragment ) )
-						.to.equal( '<blockquote><paragraph>foo<$text bold="true">bar</$text>bom</paragraph></blockquote>' );
+						.to.equal( '<blockQuote><paragraph>foo<$text bold="true">bar</$text>bom</paragraph></blockQuote>' );
 				} );
 
 				it( 'inside paragraph-like element', () => {
@@ -250,10 +253,10 @@ describe( 'Paragraph feature', () => {
 			} );
 
 			it( 'should not convert h1+h2 (in a context which does not allow paragraphs)', () => {
-				model.schema.registerItem( 'div' );
-				model.schema.registerItem( 'specialRoot' );
-				model.schema.allow( { name: 'div', inside: 'specialRoot' } );
-				model.schema.allow( { name: '$text', inside: 'div' } );
+				model.schema.register( 'div' );
+				model.schema.register( 'specialRoot' );
+				model.schema.extend( 'div', { allowIn: 'specialRoot' } );
+				model.schema.extend( '$text', { allowIn: 'div' } );
 
 				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
 
@@ -329,9 +332,9 @@ describe( 'Paragraph feature', () => {
 			} );
 
 			it( 'should convert li inside converted container', () => {
-				model.schema.registerItem( 'div' );
-				model.schema.allow( { name: 'div', inside: '$root' } );
-				model.schema.allow( { name: 'paragraph', inside: 'div' } );
+				model.schema.register( 'div' );
+				model.schema.extend( 'div', { allowIn: '$root' } );
+				model.schema.extend( 'paragraph', { allowIn: 'div' } );
 
 				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
 
@@ -406,7 +409,7 @@ describe( 'Paragraph feature', () => {
 
 		it( 'should not fix root if it got content during changesDone event', () => {
 			// "Autoheading feature".
-			model.schema.registerItem( 'heading', '$block' );
+			model.schema.register( 'heading', { inheritAllFrom: '$block' } );
 
 			buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView )
 				.fromElement( 'heading' )
@@ -431,7 +434,14 @@ describe( 'Paragraph feature', () => {
 		} );
 
 		it( 'should not fix root which does not allow paragraph', () => {
-			model.schema.disallow( { name: 'paragraph', inside: '$root' } );
+			model.schema.on( 'checkChild', ( evt, args ) => {
+				const def = model.schema.getDefinition( args[ 1 ] );
+
+				if ( args[ 0 ].endsWith( '$root' ) && def.name == 'paragraph' ) {
+					evt.stop();
+					evt.return = false;
+				}
+			} );
 
 			model.change( writer => {
 				writer.remove( ModelRange.createIn( root ) );

+ 47 - 15
packages/ckeditor5-paragraph/tests/paragraphcommand.js

@@ -22,12 +22,12 @@ describe( 'ParagraphCommand', () => {
 			root = document.getRoot();
 
 			editor.commands.add( 'paragraph', command );
-			schema.registerItem( 'paragraph', '$block' );
-			schema.registerItem( 'heading1', '$block' );
+			schema.register( 'paragraph', { inheritAllFrom: '$block' } );
+			schema.register( 'heading1', { 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' } );
 		} );
 	} );
 
@@ -101,13 +101,22 @@ describe( 'ParagraphCommand', () => {
 		} );
 
 		// https://github.com/ckeditor/ckeditor5-paragraph/issues/24
-		it( 'should not rename blocks which cannot become paragraphs', () => {
-			model.schema.registerItem( 'restricted' );
-			model.schema.allow( { name: 'restricted', inside: '$root' } );
-			model.schema.disallow( { name: 'paragraph', inside: 'restricted' } );
+		it( 'should not rename blocks which cannot become paragraphs (paragraph is not allowed in their parent)', () => {
+			model.schema.register( 'restricted', { allowIn: '$root' } );
 
-			model.schema.registerItem( 'fooBlock', '$block' );
-			model.schema.allow( { name: 'fooBlock', inside: 'restricted' } );
+			model.schema.register( 'fooBlock', {
+				inheritAllFrom: '$block',
+				allowIn: 'restricted'
+			} );
+
+			model.schema.on( 'checkChild', ( evt, args ) => {
+				const def = model.schema.getDefinition( args[ 1 ] );
+
+				if ( args[ 0 ].endsWith( 'restricted' ) && def.name == 'paragraph' ) {
+					evt.stop();
+					evt.return = false;
+				}
+			} );
 
 			setData(
 				model,
@@ -125,6 +134,29 @@ describe( 'ParagraphCommand', () => {
 			);
 		} );
 
+		it( 'should not rename blocks which cannot become paragraphs (block is an object)', () => {
+			model.schema.register( 'image', {
+				isBlock: true,
+				isObject: true,
+				allowIn: '$root'
+			} );
+
+			setData(
+				model,
+				'<heading1>a[bc</heading1>' +
+				'<image></image>' +
+				'<heading1>de]f</heading1>'
+			);
+
+			command.execute();
+
+			expect( getData( model ) ).to.equal(
+				'<paragraph>a[bc</paragraph>' +
+				'<image></image>' +
+				'<paragraph>de]f</paragraph>'
+			);
+		} );
+
 		it( 'should not rename blocks which already are pargraphs', () => {
 			setData( model, '<paragraph>foo[</paragraph><heading1>bar]</heading1>' );
 
@@ -174,8 +206,8 @@ describe( 'ParagraphCommand', () => {
 			} );
 
 			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, '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
 				command.execute();
@@ -186,7 +218,7 @@ describe( 'ParagraphCommand', () => {
 
 		describe( 'non-collapsed selection', () => {
 			it( 'converts all elements where selection is applied', () => {
-				schema.registerItem( 'heading2', '$block' );
+				schema.register( 'heading2', { inheritAllFrom: '$block' } );
 
 				setData( model, '<heading1>foo[</heading1><heading2>bar</heading2><heading2>baz]</heading2>' );
 
@@ -197,7 +229,7 @@ describe( 'ParagraphCommand', () => {
 			} );
 
 			it( 'converts all elements even if already anchored in paragraph', () => {
-				schema.registerItem( 'heading2', '$block' );
+				schema.register( 'heading2', { inheritAllFrom: '$block' } );
 
 				setData( model, '<paragraph>foo[</paragraph><heading2>bar]</heading2>' );