Explorar o código

Use iterables instead of arrays.

Piotr Jasiun %!s(int64=10) %!d(string=hai) anos
pai
achega
c2889d81f0

+ 1 - 0
packages/ckeditor5-engine/src/document/character.js

@@ -16,6 +16,7 @@ CKEDITOR.define( [ 'document/node' ], function( Node ) {
 		 * Creates character linear item.
 		 *
 		 * @param {String} character Described character.
+		 * @param {Iterable} attrs Iterable collection of {@link document.Attribute attributes}.
 		 * @constructor
 		 */
 		constructor( character, attrs ) {

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

@@ -18,7 +18,7 @@ CKEDITOR.define( [ 'document/node', 'document/nodelist' ], function( Node, NodeL
 		 * This constructor should be used only internally by the document.
 		 *
 		 * @param {String} name Node name.
-		 * @param {Array} attrs Array of {@link document.Attribute attributes}.
+		 * @param {Iterable} attrs Iterable collection of {@link document.Attribute attributes}.
 		 * @param {document.Node|document.Text|document.NodeList|String|Array} nodes List of nodes to be inserted.
 		 * List of nodes can be any type accepted by the {@link document.NodeList} constructor.
 		 * @constructor

+ 2 - 2
packages/ckeditor5-engine/src/document/node.js

@@ -19,7 +19,7 @@ CKEDITOR.define( [ 'document/attribute', 'utils' ], function( Attribute, utils )
 		 *
 		 * Created node has no parent. Parent of the node is set when it is attached to the {@link document.Element}.
 		 *
-		 * @param {Array} attrs Array of attributes.
+		 * @param {Iterable} attrs Iterable collection of {@link document.Attribute attributes}.
 		 * @constructor
 		 */
 		constructor( attrs ) {
@@ -36,7 +36,7 @@ CKEDITOR.define( [ 'document/attribute', 'utils' ], function( Attribute, utils )
 			 * Attributes of nodes attached to the document can be changed only be the {@link document.ChangeOpeation}.
 			 *
 			 * @private
-			 * @property {Array} attr
+			 * @property {Set} _attrs
 			 */
 			this._attrs = new Set( attrs );
 		}

+ 7 - 9
packages/ckeditor5-engine/src/document/nodelist.js

@@ -68,29 +68,27 @@ CKEDITOR.define( [
 			this._nodes = [];
 
 			if ( nodes ) {
-				var node, i, j, nodeLen, nodesLen;
+				var node, i, nodeLen;
 
-				if ( !utils.isArray( nodes ) ) {
+				if ( !utils.isIterable( nodes ) ) {
 					nodes = [ nodes ];
 				}
 
-				for ( i = 0, nodesLen = nodes.length; i < nodesLen; i++ ) {
-					node = nodes[ i ];
-
+				for ( node of nodes ) {
 					// Node.
 					if ( node instanceof Node ) {
 						this._nodes.push( node );
 					}
 					// Text.
 					else if ( node instanceof Text ) {
-						for ( j = 0, nodeLen = node.text.length; j < nodeLen; j++ ) {
-							this._nodes.push( new Character( node.text[ j ], node.attrs ) );
+						for ( i = 0, nodeLen = node.text.length; i < nodeLen; i++ ) {
+							this._nodes.push( new Character( node.text[ i ], node.attrs ) );
 						}
 					}
 					// String.
 					else {
-						for ( j = 0, nodeLen = node.length; j < nodeLen; j++ ) {
-							this._nodes.push( new Character( node[ j ] ) );
+						for ( i = 0, nodeLen = node.length; i < nodeLen; i++ ) {
+							this._nodes.push( new Character( node[ i ] ) );
 						}
 					}
 				}

+ 4 - 4
packages/ckeditor5-engine/src/document/text.js

@@ -16,8 +16,8 @@ CKEDITOR.define( [], function() {
 		/**
 		 * Creates text with attributes.
 		 *
-		 * @param {String} text Described character.
-		 * @param {Array} attrs Array of attributes.
+		 * @param {String} text Described text.
+		 * @param {Iterable} attrs Iterable collection of {@link document.Attribute attributes}.
 		 * @constructor
 		 */
 		constructor( text, attrs ) {
@@ -30,9 +30,9 @@ CKEDITOR.define( [], function() {
 			this.text = text;
 
 			/**
-			 * Array of attributes.
+			 * Iterable collection of {@link document.Attribute attributes}.
 			 *
-			 * @property {Array} attr
+			 * @property {Iterable} attr
 			 */
 			this.attrs = attrs;
 		}

+ 11 - 1
packages/ckeditor5-engine/src/utils.js

@@ -43,7 +43,17 @@ CKEDITOR.define( [ 'utils-lodash', 'lib/lodash/lodash-ckeditor' ], function( lod
 			return function() {
 				return next++;
 			};
-		} )()
+		} )(),
+
+		/**
+		 * Checks if value implements iterator interface.
+		 *
+		 * @param {Mixed} value The value to check.
+		 * @returns {Boolean} True if value implements iterator interface.
+		 */
+		isIterable( value ) {
+			return !!( value && value[ Symbol.iterator ] );
+		}
 	};
 
 	// Extend "utils" with Lo-Dash methods.

+ 50 - 0
packages/ckeditor5-engine/tests/utils/utils.js

@@ -73,6 +73,56 @@ describe( 'uid', function() {
 	} );
 } );
 
+describe( 'isIterable', function() {
+	it( 'should be true for string', function() {
+		var utils = modules.utils;
+
+		var string = 'foo';
+
+		expect( utils.isIterable( string ) ).to.be.true;
+	} );
+
+	it( 'should be true for arrays', function() {
+		var utils = modules.utils;
+
+		var array = [ 1, 2, 3 ];
+
+		expect( utils.isIterable( array ) ).to.be.true;
+	} );
+
+	it( 'should be true for iterable classes', function() {
+		var utils = modules.utils;
+
+		class IterableClass {
+			constructor() {
+				this.array = [ 1, 2, 3 ];
+			}
+
+			[ Symbol.iterator ]() {
+				return this.array[ Symbol.iterator ]();
+			}
+		}
+
+		var instance = new IterableClass();
+
+		expect( utils.isIterable( instance ) ).to.be.true;
+	} );
+
+	it( 'should be false for not iterable objects', function() {
+		var utils = modules.utils;
+
+		var notIterable = { foo: 'bar' };
+
+		expect( utils.isIterable( notIterable ) ).to.be.false;
+	} );
+
+	it( 'should be false for undefined', function() {
+		var utils = modules.utils;
+
+		expect( utils.isIterable() ).to.be.false;
+	} );
+} );
+
 describe( 'Lo-Dash extensions', function() {
 	// Ensures that the required Lo-Dash extensions are available in `utils`.
 	it( 'should be exposed in utils', function() {