Skip to content

Commit

Permalink
Introduced CKEDITOR.dom.nodeList#toArray method.
Browse files Browse the repository at this point in the history
  • Loading branch information
mlewand committed Aug 9, 2017
1 parent eee5112 commit 18ac322
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/dom/nodelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,16 @@ CKEDITOR.dom.nodeList.prototype = {

var $node = this.$[ index ];
return $node ? new CKEDITOR.dom.node( $node ) : null;
},

/**
* Return node list as an array.
*
* @returns {CKEDITOR.dom.node[]}
*/
toArray: function() {
return CKEDITOR.tools.array.map( this.$, function( nativeEl ) {
return new CKEDITOR.dom.node( nativeEl );
} );
}
};
6 changes: 6 additions & 0 deletions tests/core/dom/nodeList.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div id="playground">
<span id="el1">foo</span>
<span id="el2">foo</span>
<span id="el3">foo</span>
<span>foo</span>
</div>
27 changes: 27 additions & 0 deletions tests/core/dom/nodeList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* bender-tags: editor,dom */

( function() {
'use strict';

bender.test( {
'test nodeList.toArray()': function() {
var list = CKEDITOR.document.find( '#playground span[id]' ),
ret = list.toArray();

assert.isArray( ret );
assert.areSame( 3, ret.length, 'Array length' );

for ( var i = 0; i < ret.length; i++ ) {
assert.areSame( list.getItem( i ), ret[ i ], 'Item ' + i );
}
},

'test nodeList.toArray() empty list': function() {
var ret = CKEDITOR.document.find( '#nonExistingId__' ).toArray();

assert.isArray( ret );
assert.areSame( 0, ret.length, 'Array length' );
}
} );

} )();

0 comments on commit 18ac322

Please sign in to comment.