浏览代码

Added Writer#cloneElement().

Kuba Niegowski 5 年之前
父节点
当前提交
26c95ae963

+ 1 - 1
packages/ckeditor5-engine/src/model/element.js

@@ -232,7 +232,7 @@ export default class Element extends Node {
 
 	/**
 	 * Creates a copy of this element and returns it. Created element has the same name and attributes as the original element.
-	 * If clone is deep, the original element's children are also cloned. If not, then empty element is removed.
+	 * If clone is deep, the original element's children are also cloned. If not, then empty element is returned.
 	 *
 	 * @protected
 	 * @param {Boolean} [deep=false] If set to `true` clones element and all its children recursively. When set to `false`,

+ 12 - 0
packages/ckeditor5-engine/src/model/writer.js

@@ -117,6 +117,18 @@ export default class Writer {
 	}
 
 	/**
+	 * Creates a copy of the element and returns it. Created element has the same name and attributes as the original element.
+	 * If clone is deep, the original element's children are also cloned. If not, then empty element is returned.
+	 *
+	 * @param {module:engine/model/element~Element} element The element to clone.
+	 * @param {Boolean} [deep=true] If set to `true` clones element and all its children recursively. When set to `false`,
+	 * element will be cloned without any child.
+	 */
+	cloneElement( element, deep = true ) {
+		return element._clone( deep );
+	}
+
+	/**
 	 * Inserts item on given position.
 	 *
 	 *		const paragraph = writer.createElement( 'paragraph' );

+ 35 - 0
packages/ckeditor5-engine/tests/model/writer.js

@@ -86,6 +86,35 @@ describe( 'Writer', () => {
 		} );
 	} );
 
+	describe( 'cloneElement()', () => {
+		it( 'should make deep clone of element', () => {
+			const element = createElement( 'foo', { 'abc': '123' } );
+
+			insertElement( createElement( 'bar', { 'xyz': '789' } ), element );
+
+			const clonedElement = cloneElement( element );
+
+			expect( clonedElement ).to.not.equal( element );
+			expect( clonedElement.getChild( 0 ) ).to.not.equal( element.getChild( 0 ) );
+			expect( clonedElement.toJSON() ).to.deep.equal( element.toJSON() );
+		} );
+
+		it( 'should make shallow copy of element', () => {
+			const element = createElement( 'foo', { 'abc': '123' } );
+
+			insertElement( createElement( 'bar', { 'xyz': '789' } ), element );
+
+			const elementJson = element.toJSON();
+			delete elementJson.children;
+
+			const clonedElement = cloneElement( element, false );
+
+			expect( clonedElement ).to.not.equal( element );
+			expect( clonedElement.childCount ).to.equal( 0 );
+			expect( clonedElement.toJSON() ).to.deep.equal( elementJson );
+		} );
+	} );
+
 	describe( 'insert()', () => {
 		it( 'should insert node at given position', () => {
 			const parent = createDocumentFragment();
@@ -2902,6 +2931,12 @@ describe( 'Writer', () => {
 		} );
 	}
 
+	function cloneElement( element, deep = true ) {
+		return model.change( writer => {
+			return writer.cloneElement( element, deep );
+		} );
+	}
+
 	function insert( item, itemOrPosition, offset ) {
 		model.enqueueChange( batch, writer => {
 			writer.insert( item, itemOrPosition, offset );

+ 1 - 1
packages/ckeditor5-table/src/tableclipboard.js

@@ -295,7 +295,7 @@ function replaceSelectedCellsWithPasted( pastedTable, pastedDimensions, selected
 
 		// Clone cell to insert (to duplicate its attributes and children).
 		// Cloning is required to support repeating pasted table content when inserting to a bigger selection.
-		const cellToInsert = pastedCell._clone( true );
+		const cellToInsert = writer.cloneElement( pastedCell );
 
 		// Trim the cell if it's row/col-spans would exceed selection area.
 		trimTableCellIfNeeded( cellToInsert, row, column, lastRowOfSelection, lastColumnOfSelection, writer );

+ 1 - 1
packages/ckeditor5-table/src/utils/structure.js

@@ -75,7 +75,7 @@ export function cropTableToDimensions( sourceTable, cropDimensions, writer ) {
 		}
 		// Otherwise clone the cell with all children and trim if it exceeds cropped area.
 		else {
-			const tableCellCopy = tableCell._clone( true );
+			const tableCellCopy = writer.cloneElement( tableCell );
 
 			writer.append( tableCellCopy, row );