浏览代码

Internal: `DataController#insertContent` strips disallowed attributes from Text.

Oskar Wróbel 8 年之前
父节点
当前提交
de5cb4a229

+ 11 - 0
packages/ckeditor5-engine/src/controller/insertcontent.js

@@ -226,6 +226,17 @@ class Insertion {
 		if ( node.is( 'element' ) ) {
 		if ( node.is( 'element' ) ) {
 			this.handleNodes( node.getChildren(), context );
 			this.handleNodes( node.getChildren(), context );
 		}
 		}
+		// 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._handleNode( node, context );
+		}
 		// Try autoparagraphing.
 		// Try autoparagraphing.
 		else {
 		else {
 			this._tryAutoparagraphing( node, context );
 			this._tryAutoparagraphing( node, context );

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

@@ -591,6 +591,8 @@ describe( 'DataController', () => {
 				schema.allow( { name: 'disallowedWidget', inside: '$clipboardHolder' } );
 				schema.allow( { name: 'disallowedWidget', inside: '$clipboardHolder' } );
 				schema.allow( { name: '$text', inside: 'disallowedWidget' } );
 				schema.allow( { name: '$text', inside: 'disallowedWidget' } );
 				schema.objects.add( 'disallowedWidget' );
 				schema.objects.add( 'disallowedWidget' );
+
+				schema.allow( { name: '$text', attributes: 'b', inside: 'paragraph' } );
 			} );
 			} );
 
 
 			it( 'filters out disallowed elements and leaves out the text', () => {
 			it( 'filters out disallowed elements and leaves out the text', () => {
@@ -610,6 +612,12 @@ describe( 'DataController', () => {
 				insertHelper( '<disallowedWidget>xxx</disallowedWidget>' );
 				insertHelper( '<disallowedWidget>xxx</disallowedWidget>' );
 				expect( getData( doc ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
 				expect( getData( doc ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
 			} );
 			} );
+
+			it( 'filters out disallowed attributes', () => {
+				setData( doc, '<paragraph>f[]oo</paragraph>' );
+				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>' );
+			} );
 		} );
 		} );
 	} );
 	} );