Browse Source

Introduced new static method: treeView.dataController.ViewConsumable.createFromElement().

Szymon Kupś 9 years ago
parent
commit
d1f566feab

+ 36 - 0
packages/ckeditor5-engine/src/treecontroller/viewconsumable.js

@@ -7,6 +7,7 @@
 
 import isArray from '../../utils/lib/lodash/isArray.js';
 import CKEditorError from '../../utils/ckeditorerror.js';
+import ViewElement from '../treeview/element.js';
 import ViewText from '../treeview/text.js';
 import ViewDocumentFragment from '../treeview/documentfragment.js';
 
@@ -534,4 +535,39 @@ export default class ViewConsumable {
 
 		return consumables;
 	}
+
+	/**
+	 * Creates {@link engine.treeController.ViewConsumable ViewConsumable} instance from
+	 * {@link engine.treeView.Element element} or {@link engine.treeView.DocumentFragment document fragment}.
+	 * Instance will contain all elements, child nodes, attributes, styles and classes added for consumption.
+	 *
+	 * @static
+	 * @param {engine.treeView.Element|engine.treeView.DocumentFragment} root
+	 * @param {engine.treeController.ViewConsumable} [instance] Optionally this instance can be used to add all
+	 * consumables from provided root. It will be returned instead of new instance.
+	 */
+	static createFromElement( root, instance ) {
+		if ( !instance ) {
+			instance = new ViewConsumable();
+		}
+
+		// Add root itself if it is an element.
+		if ( root instanceof ViewElement ) {
+			instance.add( root, ViewConsumable.consumablesFromElement( root ) );
+		}
+
+		if ( root instanceof ViewDocumentFragment ) {
+			instance.add( root );
+		}
+
+		for ( let child of root.getChildren() ) {
+			if ( child instanceof ViewText ) {
+				instance.add( child );
+			} else {
+				instance = ViewConsumable.createFromElement( child, instance );
+			}
+		}
+
+		return instance;
+	}
 }

+ 35 - 0
packages/ckeditor5-engine/tests/treecontroller/viewconsumable.js

@@ -500,4 +500,39 @@ describe( 'ViewConsumable', () => {
 			expect( consumables.name ).to.be.true;
 		} );
 	} );
+
+	describe( 'createFromElement', () => {
+		it( 'should return new ViewConsumable instance', () => {
+			const newConsumable = ViewConsumable.createFromElement( el );
+
+			expect( newConsumable ).to.be.instanceof( ViewConsumable );
+			expect( newConsumable.test( el, { name: true } ) ).to.be.true;
+		} );
+
+		it( 'should return new ViewConsumable instance from document fragment', () => {
+			const fragment = new ViewDocumentFragment();
+			const newConsumable = ViewConsumable.createFromElement( fragment );
+
+			expect( newConsumable ).to.be.instanceof( ViewConsumable );
+			expect( newConsumable.test( fragment ) ).to.be.true;
+		} );
+
+		it( 'should add all child elements', () => {
+			const text1 = new ViewText( 'foo' );
+			const text2 = new ViewText( 'bar' );
+			const child1 = new ViewElement( 'p', { 'title': 'baz' }, [ text1 ] );
+			const child2 = new ViewElement( 'p' );
+			const child3 = new ViewElement( 'p', { 'style': 'top:10px;', 'class': 'qux bar' }, [ text2, child2 ] );
+			el.appendChildren( [ child1, child3 ] );
+
+			const newConsumable = ViewConsumable.createFromElement( el );
+
+			expect( newConsumable.test( el, { name: true } ) ).to.be.true;
+			expect( newConsumable.test( text1 ) ).to.be.true;
+			expect( newConsumable.test( text2 ) ).to.be.true;
+			expect( newConsumable.test( child1, { name: true, attribute: 'title' } ) ).to.be.true;
+			expect( newConsumable.test( child2, { name: true } ) ).to.be.true;
+			expect( newConsumable.test( child3, { name: true, style: 'top', class: [ 'qux', 'bar' ] } ) ).to.be.true;
+		} );
+	} );
 } );