소스 검색

Used schema method to filter dissalowed attributes by insert and delete content.

Oskar Wróbel 8 년 전
부모
커밋
0d077c85c1
2개의 변경된 파일6개의 추가작업 그리고 75개의 파일을 삭제
  1. 1 37
      packages/ckeditor5-engine/src/controller/deletecontent.js
  2. 5 38
      packages/ckeditor5-engine/src/controller/insertcontent.js

+ 1 - 37
packages/ckeditor5-engine/src/controller/deletecontent.js

@@ -74,7 +74,7 @@ export default function deleteContent( selection, batch, options = {} ) {
 		//
 		//
 		// e.g. bold is disallowed for <H1>
 		// e.g. bold is disallowed for <H1>
 		// <h1>Fo{o</h1><p>b}a<b>r</b><p> -> <h1>Fo{}a<b>r</b><h1> -> <h1>Fo{}ar<h1>.
 		// <h1>Fo{o</h1><p>b}a<b>r</b><p> -> <h1>Fo{}a<b>r</b><h1> -> <h1>Fo{}ar<h1>.
-		removeDisallowedAttributes( startPos.parent.getChildren(), startPos, batch );
+		batch.document.schema.removeDisallowedAttributes( startPos.parent.getChildren(), startPos, batch );
 	}
 	}
 
 
 	selection.setCollapsedAt( startPos );
 	selection.setCollapsedAt( startPos );
@@ -217,39 +217,3 @@ function shouldEntireContentBeReplacedWithParagraph( schema, selection ) {
 
 
 	return schema.check( { name: 'paragraph', inside: limitElement.name } );
 	return schema.check( { name: 'paragraph', inside: limitElement.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;
-}
-
-// Creates AttributeDeltas that removes attributes that are disallowed by schema on given node and its children.
-//
-// @param {Array<module:engine/model/node~Node>} nodes Nodes that will be filtered.
-// @param {module:engine/model/schema~SchemaPath} inside Path inside which schema will be checked.
-// @param {module:engine/model/batch~Batch} batch Batch to which the deltas will be added.
-function removeDisallowedAttributes( nodes, inside, batch ) {
-	const schema = batch.document.schema;
-
-	for ( const node of nodes ) {
-		const name = getNodeSchemaName( node );
-
-		// When node with attributes is not allowed in current position.
-		if ( !schema.check( { name, inside, attributes: Array.from( node.getAttributeKeys() ) } ) ) {
-			// Let's remove attributes one by one.
-			// This should be improved to check all combination of attributes.
-			for ( const attribute of node.getAttributeKeys() ) {
-				if ( !schema.check( { name, inside, attributes: attribute } ) ) {
-					batch.removeAttribute( node, attribute );
-				}
-			}
-		}
-
-		if ( node.is( 'element' ) ) {
-			removeDisallowedAttributes( node.getChildren(), Position.createAt( node ), batch );
-		}
-	}
-}

+ 5 - 38
packages/ckeditor5-engine/src/controller/insertcontent.js

@@ -229,7 +229,7 @@ class Insertion {
 		// If the node is a text and bare text is allowed in current position it means that the node
 		// 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.
 		// contains disallowed attributes and we have to remove them.
 		else if ( this.schema.check( { name: '$text', inside: this.position } ) ) {
 		else if ( this.schema.check( { name: '$text', inside: this.position } ) ) {
-			removeDisallowedAttributes( [ node ], this.position, this.schema );
+			this.schema.removeDisallowedAttributes( [ node ], this.position );
 			this._handleNode( node, context );
 			this._handleNode( node, context );
 		}
 		}
 		// If text is not allowed, try autoparagraphing.
 		// If text is not allowed, try autoparagraphing.
@@ -291,7 +291,7 @@ class Insertion {
 			// We need to check and strip disallowed attributes in all nested nodes because after merge
 			// We need to check and strip disallowed attributes in all nested nodes because after merge
 			// some attributes could end up in a path where are disallowed.
 			// some attributes could end up in a path where are disallowed.
 			const parent = position.nodeBefore;
 			const parent = position.nodeBefore;
-			removeDisallowedAttributes( parent.getChildren(), Position.createAt( parent ), this.schema, this.batch );
+			this.schema.removeDisallowedAttributes( parent.getChildren(), Position.createAt( parent ), this.batch );
 
 
 			this.position = Position.createFromPosition( position );
 			this.position = Position.createFromPosition( position );
 			position.detach();
 			position.detach();
@@ -318,7 +318,7 @@ class Insertion {
 
 
 			// We need to check and strip disallowed attributes in all nested nodes because after merge
 			// We need to check and strip disallowed attributes in all nested nodes because after merge
 			// some attributes could end up in a place where are disallowed.
 			// some attributes could end up in a place where are disallowed.
-			removeDisallowedAttributes( position.parent.getChildren(), position, this.schema, this.batch );
+			this.schema.removeDisallowedAttributes( position.parent.getChildren(), position, this.batch );
 
 
 			this.position = Position.createFromPosition( position );
 			this.position = Position.createFromPosition( position );
 			position.detach();
 			position.detach();
@@ -330,7 +330,7 @@ class Insertion {
 		// When there was no merge we need to check and strip disallowed attributes in all nested nodes of
 		// When there was no merge we need to check and strip disallowed attributes in all nested nodes of
 		// just inserted node because some attributes could end up in a place where are disallowed.
 		// just inserted node because some attributes could end up in a place where are disallowed.
 		if ( !mergeLeft && !mergeRight ) {
 		if ( !mergeLeft && !mergeRight ) {
-			removeDisallowedAttributes( node.getChildren(), Position.createAt( node ), this.schema, this.batch );
+			this.schema.removeDisallowedAttributes( node.getChildren(), Position.createAt( node ), this.batch );
 		}
 		}
 	}
 	}
 
 
@@ -350,7 +350,7 @@ class Insertion {
 			// When node is a text and is disallowed by schema it means that contains disallowed attributes
 			// When node is a text and is disallowed by schema it means that contains disallowed attributes
 			// and we need to remove them.
 			// and we need to remove them.
 			if ( node.is( 'text' ) && !this._checkIsAllowed( node, [ paragraph ] ) ) {
 			if ( node.is( 'text' ) && !this._checkIsAllowed( node, [ paragraph ] ) ) {
-				removeDisallowedAttributes( [ node ], [ paragraph ], this.schema );
+				this.schema.removeDisallowedAttributes( [ node ], [ paragraph ] );
 			}
 			}
 
 
 			if ( this._checkIsAllowed( node, [ paragraph ] ) ) {
 			if ( this._checkIsAllowed( node, [ paragraph ] ) ) {
@@ -453,36 +453,3 @@ class Insertion {
 function getNodeSchemaName( node ) {
 function getNodeSchemaName( node ) {
 	return node.is( 'text' ) ? '$text' : node.name;
 	return node.is( 'text' ) ? '$text' : node.name;
 }
 }
-
-// Removes disallowed by schema attributes from given nodes. When batch parameter is provided then
-// attributes will be removed by creating AttributeDeltas otherwise attributes will be removed
-// directly from provided nodes.
-//
-// @param {Array<module:engine/model/node~Node>} nodes Nodes that will be filtered.
-// @param {module:engine/model/schema~SchemaPath} inside Path inside which schema will be checked.
-// @param {module:engine/model/schema~Schema} schema Schema instance uses for element validation.
-// @param {module:engine/model/batch~Batch} [batch] Batch to which the deltas will be added.
-function removeDisallowedAttributes( nodes, inside, schema, batch ) {
-	for ( const node of nodes ) {
-		const name = getNodeSchemaName( node );
-
-		// When node with attributes is not allowed in current position.
-		if ( !schema.check( { name, inside, attributes: Array.from( node.getAttributeKeys() ) } ) ) {
-			// Let's remove attributes one by one.
-			// This should be improved to check all combination of attributes.
-			for ( const attribute of node.getAttributeKeys() ) {
-				if ( !schema.check( { name, inside, attributes: attribute } ) ) {
-					if ( batch ) {
-						batch.removeAttribute( node, attribute );
-					} else {
-						node.removeAttribute( attribute );
-					}
-				}
-			}
-		}
-
-		if ( node.is( 'element' ) ) {
-			removeDisallowedAttributes( node.getChildren(), Position.createAt( node ), schema, batch );
-		}
-	}
-}