浏览代码

Manual tweaks in tests to make them work with the new Schema.

Piotrek Koszuliński 8 年之前
父节点
当前提交
b7bd4bf635

+ 25 - 5
packages/ckeditor5-engine/tests/conversion/buildviewconverter.js

@@ -100,7 +100,8 @@ describe( 'View converter builder', () => {
 			inheritAllFrom: '$text'
 		} );
 		schema.extend( '$text', {
-			allowAttributes: textAttributes
+			allowAttributes: textAttributes,
+			allowIn: [ '$root', 'span', 'abcd', 'MEGATRON' ]
 		} );
 
 		dispatcher = new ViewConversionDispatcher( model, { schema } );
@@ -493,8 +494,17 @@ describe( 'View converter builder', () => {
 		buildViewConverter().for( dispatcher ).fromElement( 'div' ).toElement( 'div' );
 		buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
 
-		// TODO this requires a callback
-		// schema.xdisallow( { name: 'div', inside: '$root' } );
+		// Disallow $root>div.
+		schema.on( 'checkChild', ( evt, args ) => {
+			const context = args[ 0 ];
+			const child = args[ 1 ];
+			const childRule = schema.getRule( child );
+
+			if ( childRule.name == 'div' && context[ context.length - 1 ].name == '$root' ) {
+				evt.stop();
+				evt.return = false;
+			}
+		}, { priority: 'high' } );
 
 		dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
 
@@ -513,8 +523,18 @@ describe( 'View converter builder', () => {
 		buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
 		buildViewConverter().for( dispatcher ).fromElement( 'strong' ).toAttribute( 'bold', true );
 
-		// TODO this requires a callback
-		// schema.xdisallow( { name: '$text', attributes: 'bold', inside: 'paragraph' } );
+		// Disallow bold in paragraph>$text.
+		schema.on( 'checkAttribute', ( evt, args ) => {
+			const context = args[ 0 ];
+			const ctxItem = context[ context.length - 1 ];
+			const ctxParent = context[ context.length - 2 ];
+			const attributeName = args[ 1 ];
+
+			if ( ctxItem.name == '$text' && ctxParent.name == 'paragraph' && attributeName == 'bold' ) {
+				evt.stop();
+				evt.return = false;
+			}
+		}, { priority: 'high' } );
 
 		dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
 

+ 1 - 2
packages/ckeditor5-engine/tests/conversion/definition-based-converters.js

@@ -21,7 +21,6 @@ import {
 } from '../../src/conversion/definition-based-converters';
 
 import ViewConversionDispatcher from '../../src/conversion/viewconversiondispatcher';
-import ModelSchema from '../../src/model/schema';
 import ModelWalker from '../../src/model/treewalker';
 import ModelTextProxy from '../../src/model/textproxy';
 import Model from '../../src/model/model';
@@ -104,7 +103,7 @@ describe( 'definition-based-converters', () => {
 
 	function setupViewToModelTests() {
 		additionalData = { context: [ '$root' ] };
-		schema = new ModelSchema();
+		schema = model.schema;
 		dispatcher = new ViewConversionDispatcher( model, { schema } );
 	}
 

+ 16 - 7
packages/ckeditor5-engine/tests/conversion/view-to-model-converters.js

@@ -9,7 +9,6 @@ import ViewDocumentFragment from '../../src/view/documentfragment';
 import ViewText from '../../src/view/text';
 
 import Model from '../../src/model/model';
-import ModelSchema from '../../src/model/schema';
 import ModelDocumentFragment from '../../src/model/documentfragment';
 import ModelElement from '../../src/model/element';
 import ModelText from '../../src/model/text';
@@ -17,15 +16,17 @@ import ModelText from '../../src/model/text';
 import { convertToModelFragment, convertText } from '../../src/conversion/view-to-model-converters';
 
 describe( 'view-to-model-converters', () => {
-	let dispatcher, schema, additionalData;
-
-	const model = new Model();
+	let dispatcher, schema, additionalData, model;
 
 	beforeEach( () => {
-		schema = new ModelSchema();
+		model = new Model();
+		schema = model.schema;
+
 		schema.register( 'paragraph', { inheritAllFrom: '$block' } );
 		schema.extend( '$text', { allowIn: '$root' } );
+
 		additionalData = { context: [ '$root' ] };
+
 		dispatcher = new ViewConversionDispatcher( model, { schema } );
 	} );
 
@@ -62,8 +63,16 @@ describe( 'view-to-model-converters', () => {
 		} );
 
 		it( 'should not convert text if it is wrong with schema', () => {
-			// TODO this requires a callback
-			// schema.xdisallow( { name: '$text', inside: '$root' } );
+			schema.on( 'checkChild', ( evt, args ) => {
+				const context = args[ 0 ];
+				const child = args[ 1 ];
+				const childRule = schema.getRule( child );
+
+				if ( childRule.name == '$text' && context[ context.length - 1 ].name == '$root' ) {
+					evt.stop();
+					evt.return = false;
+				}
+			}, { priority: 'high' } );
 
 			const viewText = new ViewText( 'foobar' );
 			dispatcher.on( 'text', convertText() );

+ 2 - 2
packages/ckeditor5-engine/tests/dev-utils/model.js

@@ -510,7 +510,7 @@ describe( 'model test utils', () => {
 		it( 'throws when try to set element not registered in schema', () => {
 			expect( () => {
 				parse( '<xyz></xyz>', model.schema );
-			} ).to.throw( Error, 'Element \'xyz\' not allowed in context ["$root"].' );
+			} ).to.throw( Error, 'Element \'xyz\' was not allowed in context ["$root"].' );
 		} );
 
 		it( 'throws when try to set text directly to $root without registering it', () => {
@@ -518,7 +518,7 @@ describe( 'model test utils', () => {
 
 			expect( () => {
 				parse( 'text', model.schema );
-			} ).to.throw( Error, 'Element \'$text\' not allowed in context ["$root"].' );
+			} ).to.throw( Error, 'Text was not allowed in context ["$root"].' );
 		} );
 
 		it( 'converts data in the specified context', () => {

+ 7 - 5
packages/ckeditor5-engine/tests/model/document/document.js

@@ -248,16 +248,18 @@ describe( 'Document', () => {
 		beforeEach( () => {
 			model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
 
-			model.schema.register( 'emptyBlock' );
-			model.schema.extend( 'emptyBlock', { allowIn: '$root' } );
+			model.schema.register( 'emptyBlock', { allowIn: '$root' } );
 
 			model.schema.register( 'widget', {
+				allowIn: '$root',
 				isObject: true
 			} );
-			model.schema.extend( 'widget', { allowIn: '$root' } );
 
-			model.schema.register( 'blockWidget', { inheritAllFrom: '$block' } );
-			model.schema.extend( 'blockWidget', { allowIn: '$root' } );
+			model.schema.register( 'blockWidget', {
+				allowIn: '$root',
+				allowContentOf: '$block',
+				isObject: true
+			} );
 
 			doc.createRoot();
 			selection = doc.selection;

+ 6 - 4
packages/ckeditor5-engine/tests/model/selection.js

@@ -891,7 +891,9 @@ describe( 'Selection', () => {
 
 		beforeEach( () => {
 			schema = new Schema();
-			schema.register( 'p', { inheritAllFrom: '$block' } );
+			schema.register( '$root' );
+			schema.register( 'p', { allowIn: '$root' } );
+			schema.register( '$text', { allowIn: 'p' } );
 		} );
 
 		it( 'should return selected element', () => {
@@ -933,9 +935,9 @@ describe( 'Selection', () => {
 			model.schema.extend( 'blockquote', { allowIn: '$root' } );
 			model.schema.extend( '$block', { allowIn: 'blockquote' } );
 
-			model.schema.register( 'image' );
-			model.schema.extend( 'image', { allowIn: '$root' } );
-			model.schema.extend( 'image', { allowIn: '$block' } );
+			model.schema.register( 'image', {
+				allowIn: [ '$root', '$block' ]
+			} );
 			model.schema.extend( '$text', { allowIn: 'image' } );
 
 			// Special block which can contain another blocks.