Explorar o código

Merge pull request #565 from ckeditor/t/555

Increased code coverage of view and model utils.
Piotr Jasiun %!s(int64=9) %!d(string=hai) anos
pai
achega
1c0108de35

+ 16 - 2
packages/ckeditor5-engine/tests/_utils-tests/model.js

@@ -454,8 +454,22 @@ describe( 'model test utils', () => {
 
 		it( 'throws when invalid XML', () => {
 			expect( () => {
-				parse( '<a><b></a></b>' );
-			} ).to.throw( Error );
+				parse( '<a><b></a></b>', document.schema );
+			} ).to.throw( Error, /Parse error/ );
+		} );
+
+		it( 'throws when try to set element not registered in schema', () => {
+			expect( () => {
+				parse( '<xyz></xyz>', document.schema );
+			} ).to.throw( Error, `Element 'xyz' not allowed in context.` );
+		} );
+
+		it( 'throws when try to set text directly to $root without registering it', () => {
+			const doc = new Document();
+
+			expect( () => {
+				parse( 'text', doc.schema );
+			} ).to.throw( Error, `Element '$text' not allowed in context.` );
 		} );
 
 		describe( 'selection', () => {

+ 8 - 13
packages/ckeditor5-engine/tests/_utils-tests/view.js

@@ -16,6 +16,7 @@ import Text from '/ckeditor5/engine/view/text.js';
 import Selection from '/ckeditor5/engine/view/selection.js';
 import Range from '/ckeditor5/engine/view/range.js';
 import Document from '/ckeditor5/engine/view/document.js';
+import XmlDataProcessor from '/ckeditor5/engine/dataprocessor/xmldataprocessor.js';
 
 describe( 'view test utils', () => {
 	describe( 'getData, setData', () => {
@@ -586,22 +587,16 @@ describe( 'view test utils', () => {
 			} ).to.throw( Error );
 		} );
 
-		it( 'should throw when wrong tag name is provided #1', () => {
-			expect( () => {
-				parse( '<b:bar></b:bar>' );
-			} ).to.throw( Error );
-		} );
+		it( 'should throw when wrong type is provided', () => {
+			sinon.stub( XmlDataProcessor.prototype, 'toView', () => {
+				return new ContainerElement( 'invalidType:b' );
+			} );
 
-		it( 'should throw when wrong tag name is provided #2', () => {
 			expect( () => {
-				parse( '<container:b:bar></container:b:bar>' );
-			} ).to.throw( Error );
-		} );
+				parse( 'sth' );
+			} ).to.throw( Error, `Parse error - cannot parse element's name: invalidType:b.` );
 
-		it( 'should throw when wrong tag name is provided #3', () => {
-			expect( () => {
-				parse( '<container:b:10:test></container:b:10:test>' );
-			} ).to.throw( Error );
+			XmlDataProcessor.prototype.toView.restore();
 		} );
 
 		it( 'should use provided root element #1', () => {

+ 25 - 34
packages/ckeditor5-engine/tests/_utils/model.js

@@ -196,18 +196,18 @@ export function stringify( node, selectionOrPositionOrRange = null ) {
 	// Bind root elements.
 	mapper.bindElements( node.root, viewDocumentFragment );
 
-	modelToView.on( 'insert:$text', insertText(), 'lowest' );
+	modelToView.on( 'insert:$text', insertText() );
 	modelToView.on( 'addAttribute', wrap( ( value, data ) => {
 		if ( data.item instanceof ModelTextProxy ) {
 			return new ViewAttributeElement( 'model-text-with-attributes', { [ data.attributeKey ]: value } );
 		}
-	} ), 'lowest' );
-	modelToView.on( 'insert', insertElement( data => new ViewElement( data.item.name, data.item.getAttributes() )  ), 'lowest' );
-	modelToView.on( 'selection', convertRangeSelection(), 'lowest' );
-	modelToView.on( 'selection', convertCollapsedSelection(), 'lowest' );
+	} ) );
+	modelToView.on( 'insert', insertElement( data => new ViewElement( data.item.name, data.item.getAttributes() )  ) );
+	modelToView.on( 'selection', convertRangeSelection() );
+	modelToView.on( 'selection', convertCollapsedSelection() );
 	modelToView.on( 'selectionAttribute', convertSelectionAttribute( ( value, data ) => {
 		return new ViewAttributeElement( 'model-text-with-attributes', { [ data.key ]: value } );
-	} ), 'lowest' );
+	} ) );
 
 	// Convert model to view.
 	modelToView.convertInsertion( range );
@@ -263,10 +263,10 @@ export function parse( data, schema, options = {} ) {
 	// Setup view to model converter.
 	const viewToModel = new ViewConversionDispatcher( { schema, mapper } );
 
-	viewToModel.on( 'documentFragment', convertToModelFragment(), 'lowest' );
-	viewToModel.on( `element:model-text-with-attributes`, convertToModelText( true ), 'lowest' );
-	viewToModel.on( 'element', convertToModelElement(), 'lowest' );
-	viewToModel.on( 'text', convertToModelText(), 'lowest' );
+	viewToModel.on( 'documentFragment', convertToModelFragment() );
+	viewToModel.on( `element:model-text-with-attributes`, convertToModelText( true ) );
+	viewToModel.on( 'element', convertToModelElement() );
+	viewToModel.on( 'text', convertToModelText() );
 
 	// Convert view to model.
 	let model = viewToModel.convert( viewDocumentFragment.root, { context: [ '$root' ] } );
@@ -310,13 +310,10 @@ export function parse( data, schema, options = {} ) {
 
 function convertToModelFragment() {
 	return ( evt, data, consumable, conversionApi ) => {
-		// Second argument in `consumable.test` is discarded for ViewDocumentFragment but is needed for ViewElement.
-		if ( !data.output && consumable.test( data.input, { name: true } ) ) {
-			const convertedChildren = conversionApi.convertChildren( data.input, consumable, data );
+		const convertedChildren = conversionApi.convertChildren( data.input, consumable, data );
 
-			data.output = new ModelDocumentFragment( modelWriter.normalizeNodes( convertedChildren ) );
-			conversionApi.mapper.bindElements( data.output, data.input );
-		}
+		data.output = new ModelDocumentFragment( modelWriter.normalizeNodes( convertedChildren ) );
+		conversionApi.mapper.bindElements( data.output, data.input );
 
 		evt.stop();
 	};
@@ -333,14 +330,12 @@ function convertToModelElement() {
 			throw new Error( `Element '${ schemaQuery.name }' not allowed in context.` );
 		}
 
-		if ( consumable.consume( data.input, { name: true } ) ) {
-			data.output = new ModelElement( data.input.name, data.input.getAttributes() );
-			conversionApi.mapper.bindElements( data.output, data.input );
+		data.output = new ModelElement( data.input.name, data.input.getAttributes() );
+		conversionApi.mapper.bindElements( data.output, data.input );
 
-			data.context.push( data.output );
-			data.output.appendChildren( conversionApi.convertChildren( data.input, consumable, data ) );
-			data.context.pop();
-		}
+		data.context.push( data.output );
+		data.output.appendChildren( conversionApi.convertChildren( data.input, consumable, data ) );
+		data.context.pop();
 
 		evt.stop();
 	};
@@ -357,20 +352,16 @@ function convertToModelText( withAttributes = false ) {
 			throw new Error( `Element '${ schemaQuery.name }' not allowed in context.` );
 		}
 
-		if ( conversionApi.schema.check( schemaQuery ) ) {
-			if ( consumable.consume( data.input, { name: true } ) ) {
-				let node;
-
-				if ( withAttributes ) {
-					node = new ModelText( data.input.getChild( 0 ).data, data.input.getAttributes() );
-				} else {
-					node = new ModelText( data.input.data );
-				}
+		let node;
 
-				data.output = node;
-			}
+		if ( withAttributes ) {
+			node = new ModelText( data.input.getChild( 0 ).data, data.input.getAttributes() );
+		} else {
+			node = new ModelText( data.input.data );
 		}
 
+		data.output = node;
+
 		evt.stop();
 	};
 }

+ 9 - 13
packages/ckeditor5-engine/tests/_utils/view.js

@@ -888,22 +888,18 @@ function _convertElementNameAndPriority( viewElement ) {
 		};
 	}
 
-	if ( parts.length == 2 ) {
-		// Check if type and name: container:div.
-		const type = _convertType( parts[ 0 ] );
+	// Check if type and name: container:div.
+	const type = _convertType( parts[ 0 ] );
 
-		if ( type ) {
-			return {
-				name: parts[ 1 ],
-				type: type,
-				priority: priority
-			};
-		}
-
-		throw new Error( `Parse error - cannot parse element's name: ${ viewElement.name }.` );
+	if ( type ) {
+		return {
+			name: parts[ 1 ],
+			type: type,
+			priority: priority
+		};
 	}
 
-	throw new Error( `Parse error - cannot parse element's tag name: ${ viewElement.name }.` );
+	throw new Error( `Parse error - cannot parse element's name: ${ viewElement.name }.` );
 }
 
 // Checks if element's type is allowed. Returns `attribute`, `container` or `null`.