Procházet zdrojové kódy

Internal: Autoparagraphing checks and strips disallowed by a schema attributes.

Oskar Wróbel před 8 roky
rodič
revize
3548d046ca

+ 27 - 10
packages/ckeditor5-engine/src/controller/insertcontent.js

@@ -229,12 +229,7 @@ class Insertion {
 		// When disallowed node is a text but text is allowed in current parent it means that our node
 		// contains disallowed attributes and we have to remove them.
 		else if ( node.is( 'text' ) && this.schema.check( { name: '$text', inside: [ this.position.parent ] } ) ) {
-			for ( const attribute of node.getAttributeKeys() ) {
-				if ( !this.schema.check( { name: '$text', attributes: attribute, inside: [ this.position.parent ] } ) ) {
-					node.removeAttribute( attribute );
-				}
-			}
-
+			this._stripsDisallowedAttributes( node );
 			this._handleNode( node, context );
 		}
 		// Try autoparagraphing.
@@ -336,10 +331,17 @@ class Insertion {
 		// Do not autoparagraph if the paragraph won't be allowed there,
 		// cause that would lead to an infinite loop. The paragraph would be rejected in
 		// the next _handleNode() call and we'd be here again.
-		if ( this._getAllowedIn( paragraph, this.position.parent ) && this._checkIsAllowed( node, [ paragraph ] ) ) {
-			paragraph.appendChildren( node );
+		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 ] ) ) {
+				this._stripsDisallowedAttributes( node, [ paragraph ] );
+			}
 
-			this._handleNode( paragraph, context );
+			if ( this._checkIsAllowed( node, [ paragraph ] ) ) {
+				paragraph.appendChildren( node );
+				this._handleNode( paragraph, context );
+			}
 		}
 	}
 
@@ -420,7 +422,7 @@ class Insertion {
 	}
 
 	/**
-	 * Checks wether according to the schema this is an object type element.
+	 * Checks whether according to the schema this is an object type element.
 	 *
 	 * @param {module:engine/model/node~Node} node The node to check.
 	 */
@@ -429,6 +431,21 @@ class Insertion {
 	}
 
 	/**
+	 * Removes disallowed by schema attributes from given node.
+	 *
+	 * @private
+	 * @param {module:engine/model/node~Node} node
+	 * @param {module:engine/model/schema~SchemaPath} path
+	 */
+	_stripsDisallowedAttributes( node, path = [ this.position.parent ] ) {
+		for ( const attribute of node.getAttributeKeys() ) {
+			if ( !this.schema.check( { name: '$text', attributes: attribute, inside: [ path ] } ) ) {
+				node.removeAttribute( attribute );
+			}
+		}
+	}
+
+	/**
 	 * Gets a name under which we should check this node in the schema.
 	 *
 	 * @param {module:engine/model/node~Node} node The node.

+ 6 - 0
packages/ckeditor5-engine/tests/controller/insertcontent.js

@@ -618,6 +618,12 @@ describe( 'DataController', () => {
 				insertHelper( '<table><td>x<$text a="1" b="1">x</$text>x</td><td>y<$text a="1">y</$text>y</td></table>' );
 				expect( getData( doc ) ).to.equal( '<paragraph>fx<$text b="1">x</$text>xyyy[]oo</paragraph>' );
 			} );
+
+			it( 'filters out disallowed attributes when autoparagraphing', () => {
+				setData( doc, '<paragraph>f[]oo</paragraph>' );
+				insertHelper( '<paragraph>xxx</paragraph><$text a="1" b="1">yyy</$text>' );
+				expect( getData( doc ) ).to.equal( '<paragraph>fxxx</paragraph><paragraph><$text b="1">yyy[]</$text>oo</paragraph>' );
+			} );
 		} );
 	} );