Parcourir la source

Converted schema.check() to schema.checkChild() and schema.checkAttribute().

Piotrek Koszuliński il y a 8 ans
Parent
commit
ffded6191d

+ 2 - 14
packages/ckeditor5-engine/src/conversion/buildviewconverter.js

@@ -291,10 +291,7 @@ class ViewConverterBuilder {
 						continue;
 					}
 
-					// Check whether generated structure is okay with `Schema`.
-					const keys = Array.from( modelElement.getAttributeKeys() );
-
-					if ( !conversionApi.schema.check( { name: modelElement.name, attributes: keys, inside: data.context } ) ) {
+					if ( !conversionApi.schema.checkChild( data.context, modelElement ) ) {
 						continue;
 					}
 
@@ -518,16 +515,7 @@ function setAttributeOn( toChange, attribute, data, conversionApi ) {
 		return;
 	}
 
-	const keys = Array.from( toChange.getAttributeKeys() );
-	keys.push( attribute.key );
-
-	const schemaQuery = {
-		name: toChange.name || '$text',
-		attributes: keys,
-		inside: data.context
-	};
-
-	if ( conversionApi.schema.check( schemaQuery ) ) {
+	if ( conversionApi.schema.checkAttribute( toChange, attribute.key ) ) {
 		conversionApi.writer.setAttribute( attribute.key, attribute.value, toChange );
 	}
 }

+ 1 - 6
packages/ckeditor5-engine/src/conversion/view-to-model-converters.js

@@ -41,12 +41,7 @@ export function convertToModelFragment() {
  */
 export function convertText() {
 	return ( evt, data, consumable, conversionApi ) => {
-		const schemaQuery = {
-			name: '$text',
-			inside: data.context
-		};
-
-		if ( conversionApi.schema.check( schemaQuery ) ) {
+		if ( conversionApi.schema.checkChild( data.context, '$text' ) ) {
 			if ( consumable.consume( data.input ) ) {
 				data.output = conversionApi.writer.createText( data.input.data );
 			}

+ 3 - 6
packages/ckeditor5-engine/src/conversion/viewconversiondispatcher.js

@@ -50,12 +50,8 @@ import log from '@ckeditor/ckeditor5-utils/src/log';
  *		// Converter for paragraphs (<p>).
  *		viewDispatcher.on( 'element:p', ( evt, data, consumable, conversionApi ) => {
  *			const paragraph = new ModelElement( 'paragraph' );
- *			const schemaQuery = {
- *				name: 'paragraph',
- *				inside: data.context
- *			};
  *
- *			if ( conversionApi.schema.check( schemaQuery ) ) {
+ *			if ( conversionApi.schema.checkChild( data.context, paragraph ) ) {
  *				if ( !consumable.consume( data.input, { name: true } ) ) {
  *					// Before converting this paragraph's children we have to update their context by this paragraph.
  *					data.context.push( paragraph );
@@ -81,7 +77,8 @@ import log from '@ckeditor/ckeditor5-utils/src/log';
  *						inside: data.context
  *					};
  *
- *					if ( conversionApi.schema.check( schemaQuery ) ) {
+ *					// TODO it was impossible for me to understand wheter `data.context` contains `$text`.
+ *					if ( conversionApi.schema.checkAttribute( TODO ) ) {
  *						item.setAttribute( 'link', data.input.getAttribute( 'href' ) );
  *					}
  *				}

+ 6 - 15
packages/ckeditor5-engine/src/dev-utils/model.js

@@ -329,14 +329,10 @@ function convertToModelFragment() {
 
 function convertToModelElement() {
 	return ( evt, data, consumable, conversionApi ) => {
-		const schemaQuery = {
-			name: data.input.name,
-			attributes: Array.from( data.input.getAttributeKeys() ),
-			inside: data.context
-		};
-
-		if ( !conversionApi.schema.check( schemaQuery ) ) {
-			throw new Error( `Element '${ schemaQuery.name }' not allowed in context ${ JSON.stringify( data.context ) }.` );
+		const elementName = data.input.name;
+
+		if ( !conversionApi.schema.checkChild( data.context, elementName ) ) {
+			throw new Error( `Element '${ elementName }' was not allowed in context ${ JSON.stringify( data.context ) }.` );
 		}
 
 		// View attribute value is a string so we want to typecast it to the original type.
@@ -356,13 +352,8 @@ function convertToModelElement() {
 
 function convertToModelText( withAttributes = false ) {
 	return ( evt, data, consumable, conversionApi ) => {
-		const schemaQuery = {
-			name: '$text',
-			inside: data.context
-		};
-
-		if ( !conversionApi.schema.check( schemaQuery ) ) {
-			throw new Error( `Element '${ schemaQuery.name }' not allowed in context ${ JSON.stringify( data.context ) }.` );
+		if ( !conversionApi.schema.checkChild( data.context, '$text' ) ) {
+			throw new Error( `Text was not allowed in context ${ JSON.stringify( data.context ) }.` );
 		}
 
 		let node;

+ 2 - 2
packages/ckeditor5-engine/src/model/document.js

@@ -248,7 +248,7 @@ export default class Document {
 		const schema = this.model.schema;
 
 		// Return collapsed range if provided position is valid.
-		if ( schema.check( { name: '$text', inside: position } ) ) {
+		if ( schema.checkChild( position, '$text' ) ) {
 			return new Range( position );
 		}
 
@@ -270,7 +270,7 @@ export default class Document {
 				return Range.createOn( value.item );
 			}
 
-			if ( schema.check( { name: '$text', inside: value.nextPosition } ) ) {
+			if ( schema.checkChild( value.nextPosition, '$text' ) ) {
 				return new Range( value.nextPosition );
 			}
 		}

+ 3 - 3
packages/ckeditor5-engine/src/model/utils/deletecontent.js

@@ -157,8 +157,8 @@ function mergeBranches( writer, startPos, endPos ) {
 }
 
 function shouldAutoparagraph( schema, position ) {
-	const isTextAllowed = schema.check( { name: '$text', inside: position } );
-	const isParagraphAllowed = schema.check( { name: 'paragraph', inside: position } );
+	const isTextAllowed = schema.checkChild( position, '$text' );
+	const isParagraphAllowed = schema.check( position, 'paragraph' );
 
 	return !isTextAllowed && isParagraphAllowed;
 }
@@ -212,5 +212,5 @@ function shouldEntireContentBeReplacedWithParagraph( schema, selection ) {
 		return false;
 	}
 
-	return schema.check( { name: 'paragraph', inside: limitElement.name } );
+	return schema.checkChild( limitElement, 'paragraph' );
 }

+ 7 - 29
packages/ckeditor5-engine/src/model/utils/insertcontent.js

@@ -224,7 +224,7 @@ class Insertion {
 		}
 		// If the node is a text and bare text is allowed in current position it means that the node
 		// contains disallowed attributes and we have to remove them.
-		else if ( this.schema.check( { name: '$text', inside: this.position } ) ) {
+		else if ( this.schema.checkChild( this.position, '$text' ) ) {
 			this.schema.removeDisallowedAttributes( [ node ], this.position, this.writer );
 			this._handleNode( node, context );
 		}
@@ -239,7 +239,7 @@ class Insertion {
 	 */
 	_insert( node ) {
 		/* istanbul ignore if */
-		if ( !this._checkIsAllowed( node, this.position ) ) {
+		if ( !this.schema.checkChild( this.position, node ) ) {
 			// Algorithm's correctness check. We should never end up here but it's good to know that we did.
 			// Note that it would often be a silent issue if we insert node in a place where it's not allowed.
 			log.error(
@@ -258,7 +258,7 @@ class Insertion {
 		livePos.detach();
 
 		// The last inserted object should be selected because we can't put a collapsed selection after it.
-		if ( this._checkIsObject( node ) && !this.schema.check( { name: '$text', inside: this.position } ) ) {
+		if ( this._checkIsObject( node ) && !this.schema.checkChild( this.position, '$text' ) ) {
 			this.nodeToSelect = node;
 		} else {
 			this.nodeToSelect = null;
@@ -345,11 +345,11 @@ class Insertion {
 		if ( this._getAllowedIn( paragraph, this.position.parent ) ) {
 			// When node is a text and is disallowed by schema it means that contains disallowed attributes
 			// and we need to remove them.
-			if ( node.is( 'text' ) && !this._checkIsAllowed( node, [ paragraph ] ) ) {
+			if ( node.is( 'text' ) && !this.schema.checkChild( paragraph, node ) ) {
 				this.schema.removeDisallowedAttributes( [ node ], [ paragraph ], this.writer );
 			}
 
-			if ( this._checkIsAllowed( node, [ paragraph ] ) ) {
+			if ( this.schema.checkChild( paragraph, node ) ) {
 				paragraph.appendChildren( node );
 				this._handleNode( paragraph, context );
 			}
@@ -407,7 +407,7 @@ class Insertion {
 	 * @returns {module:engine/model/element~Element|null}
 	 */
 	_getAllowedIn( node, element ) {
-		if ( this._checkIsAllowed( node, [ element ] ) ) {
+		if ( this.schema.checkChild( element, node ) ) {
 			return element;
 		}
 
@@ -419,33 +419,11 @@ class Insertion {
 	}
 
 	/**
-	 * Check whether the given node is allowed in the specified schema path.
-	 *
-	 * @param {module:engine/model/node~Node} node
-	 * @param {module:engine/model/schema~SchemaPath} path
-	 */
-	_checkIsAllowed( node, path ) {
-		return this.schema.check( {
-			name: getNodeSchemaName( node ),
-			attributes: Array.from( node.getAttributeKeys() ),
-			inside: path
-		} );
-	}
-
-	/**
 	 * Checks whether according to the schema this is an object type element.
 	 *
 	 * @param {module:engine/model/node~Node} node The node to check.
 	 */
 	_checkIsObject( node ) {
-		return this.schema.isObject( getNodeSchemaName( node ) );
+		return this.schema.isObject( node.is( 'text' ) ? '$text' : node.name );
 	}
 }
-
-// Gets a name under which we should check this node in the schema.
-//
-// @param {module:engine/model/node~Node} node The node.
-// @returns {String} Node name.
-function getNodeSchemaName( node ) {
-	return node.is( 'text' ) ? '$text' : node.name;
-}

+ 2 - 2
packages/ckeditor5-engine/src/model/utils/modifyselection.js

@@ -87,7 +87,7 @@ function tryExtendingTo( data, value ) {
 		}
 
 		// If text allowed on this position, extend to this place.
-		if ( data.schema.check( { name: '$text', inside: value.nextPosition } ) ) {
+		if ( data.schema.checkChild( value.nextPosition, '$text' ) ) {
 			return value.nextPosition;
 		}
 	}
@@ -102,7 +102,7 @@ function tryExtendingTo( data, value ) {
 		}
 
 		// If text allowed on this position, extend to this place.
-		if ( data.schema.check( { name: '$text', inside: value.nextPosition } ) ) {
+		if ( data.schema.checkChild( value.nextPosition, '$text' ) ) {
 			return value.nextPosition;
 		}
 	}