Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #934 from ckeditor/t/689
Browse files Browse the repository at this point in the history
Other: `model.Element#clone()` now does not clone children when passed `false` and recursively clones children when passed `true`. Closes #689.

BREAKING CHANGE: `model.Element#clone()` does not clone children when not in the `deep` mode. See #689.
  • Loading branch information
Reinmar committed Apr 24, 2017
2 parents 83509df + 6df1c21 commit ccb0659
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
12 changes: 5 additions & 7 deletions src/model/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,14 @@ export default class Element extends Node {
}

/**
* Creates a copy of this element and returns it. Created element has same name and attributes as original element.
* If clone is not deep, children of copied element are references to the same nodes as in original element.
* If clone is deep, original element's children are also cloned.
* 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.
*
* @param {Boolean} [deep=false] Decides whether children of this element should also be cloned (`true`) or not (`false`).
* @param {Boolean} [deep=false] If set to `true` clones element and all its children recursively. When set to `false`,
* element will be cloned without any child.
*/
clone( deep = false ) {
const children = deep ?
Array.from( this._children ).map( ( node ) => node.clone() ) :
Array.from( this._children );
const children = deep ? Array.from( this._children ).map( ( node ) => node.clone( true ) ) : null;

return new Element( this.name, this.getAttributes(), children );
}
Expand Down
4 changes: 2 additions & 2 deletions src/view/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ export default class Element extends Node {
/**
* Clones provided element.
*
* @param {Boolean} deep If set to `true` clones element and all its children recursively. When set to `false`,
* @param {Boolean} [deep=false] If set to `true` clones element and all its children recursively. When set to `false`,
* element will be cloned without any children.
* @returns {module:engine/view/element~Element} Clone of this element.
*/
clone( deep ) {
clone( deep = false ) {
const childrenClone = [];

if ( deep ) {
Expand Down
2 changes: 1 addition & 1 deletion tests/model/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe( 'Element', () => {

expect( copy.name ).to.equal( 'elem' );
expect( Array.from( copy.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ], [ 'italic', true ] ] );
expect( Array.from( copy.getChildren() ) ).to.deep.equal( [ p, foo ] );
expect( Array.from( copy.getChildren() ) ).to.deep.equal( [] );
} );

it( 'should clone children, if clone is deep', () => {
Expand Down

0 comments on commit ccb0659

Please sign in to comment.