瀏覽代碼

Handle limits in insertContent().

Piotrek Koszuliński 8 年之前
父節點
當前提交
511797c548

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

@@ -330,6 +330,8 @@ class Insertion {
 
 	/**
 	 * @param {module:engine/model/node~Node} node
+	 * @returns {Boolean} Whether an allowed position was found.
+	 * `false` is returned if the node isn't allowed at any position up in the tree, `true` if was.
 	 */
 	_checkAndSplitToAllowedPosition( node ) {
 		const allowedIn = this._getAllowedIn( node, this.position.parent );
@@ -339,6 +341,11 @@ class Insertion {
 		}
 
 		while ( allowedIn != this.position.parent ) {
+			// If a parent which we'd need to leave is a limit element, break.
+			if ( this.schema.limits.has( this.position.parent.name ) ) {
+				return false;
+			}
+
 			if ( this.position.isAtStart ) {
 				const parent = this.position.parent;
 				this.position = Position.createBefore( parent );

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

@@ -558,6 +558,8 @@ describe( 'DataController', () => {
 
 			schema.registerItem( 'disallowedElement' );
 			schema.allow( { name: 'disallowedElement', inside: '$clipboardHolder' } );
+
+			schema.registerItem( 'paragraph', '$block' );
 		} );
 
 		it( 'should insert limit element', () => {
@@ -586,6 +588,20 @@ describe( 'DataController', () => {
 
 			expect( getData( doc ) ).to.equal( '<limit>[]</limit>' );
 		} );
+
+		it( 'should not leave the limit element when inserting at the end', () => {
+			setData( doc, '<limit>foo[]</limit>' );
+			insertHelper( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
+
+			expect( getData( doc ) ).to.equal( '<limit>fooab[]</limit>' );
+		} );
+
+		it( 'should not leave the limit element when inserting at the beginning', () => {
+			setData( doc, '<limit>[]foo</limit>' );
+			insertHelper( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
+
+			expect( getData( doc ) ).to.equal( '<limit>ab[]foo</limit>' );
+		} );
 	} );
 
 	// @param {module:engine/model/item~Item|String} content