From 41cdd9c585ba814cfbec7c3e7f4e9357b0a53855 Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Mon, 24 Apr 2017 18:19:42 +0200 Subject: [PATCH 1/2] Changed: `model.Element#clone` now does not clone children when passed `false` and clones children deeply when passed `true`. --- src/model/element.js | 7 +++---- src/view/element.js | 4 ++-- tests/model/element.js | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/model/element.js b/src/model/element.js index 36919c069..a2ebc1229 100644 --- a/src/model/element.js +++ b/src/model/element.js @@ -153,12 +153,11 @@ export default class Element extends Node { * 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. * - * @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 children. */ 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 ); } diff --git a/src/view/element.js b/src/view/element.js index 3bbc24ae1..63cb08aba 100644 --- a/src/view/element.js +++ b/src/view/element.js @@ -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 ) { diff --git a/tests/model/element.js b/tests/model/element.js index 3500ad8be..ec3b17b0a 100644 --- a/tests/model/element.js +++ b/tests/model/element.js @@ -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', () => { From 6df1c21648af74fc0bd5650f5dca74a360c7ebbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotrek=20Koszuli=C5=84ski?= Date: Mon, 24 Apr 2017 19:03:04 +0200 Subject: [PATCH 2/2] API docs fixes. [skip ci] --- src/model/element.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/model/element.js b/src/model/element.js index a2ebc1229..9e316ca95 100644 --- a/src/model/element.js +++ b/src/model/element.js @@ -149,12 +149,11 @@ 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] If set to `true` clones element and all its children recursively. When set to `false`, - * element will be cloned without any children. + * element will be cloned without any child. */ clone( deep = false ) { const children = deep ? Array.from( this._children ).map( ( node ) => node.clone( true ) ) : null;