nodelist.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. CKEDITOR.define( [
  7. 'document/character',
  8. 'document/text',
  9. 'document/node',
  10. 'utils'
  11. ], function( Character, Text, Node, utils ) {
  12. /**
  13. * List of nodes. It is used to represent multiple nodes with a given order, for example children of
  14. * {@link document.Element} object or nodes inserted using {@link document.InsertOperation}.
  15. *
  16. * Thanks to the constructor, which accepts various arguments, this class lets you easily create desired list of nodes.
  17. *
  18. * It also may internally compress nodes.
  19. *
  20. * @class document.NodeList
  21. */
  22. class NodeList {
  23. /**
  24. * Constructor let you create a list of nodes in many ways. See examples:
  25. *
  26. * var nodeList = new NodeList( [ new Element( p1 ), new Element( p1 ) ] );
  27. * nodeList.length; // 2
  28. *
  29. * var nodeList = new NodeList( new Element( p ) );
  30. * nodeList.length; // 1
  31. *
  32. * var nodeList = new NodeList( [ 'foo', new Element( p ), 'bar' ] );
  33. * nodeList.length; // 7
  34. *
  35. * var nodeList = new NodeList( 'foo' );
  36. * nodeList.length; // 3
  37. *
  38. * var nodeList = new NodeList( new Text( 'foo', [ new Attribute( 'bar', 'bom' ) ] ) );
  39. * nodeList.length; // 3
  40. * nodeList.get( 0 ).getAttr( 'bar' ); // 'bom'
  41. * nodeList.get( 1 ).getAttr( 'bar' ); // 'bom'
  42. * nodeList.get( 2 ).getAttr( 'bar' ); // 'bom'
  43. *
  44. * var nodeListA = new NodeList( 'foo' );
  45. * var nodeListB = new NodeList( nodeListA );
  46. * nodeListA === nodeListB // true
  47. * nodeListB.length // 3
  48. *
  49. * @param {document.Node|document.Text|document.NodeList|String|Iterable} nodes List of nodes.
  50. * @constructor
  51. */
  52. constructor( nodes ) {
  53. if ( nodes instanceof NodeList ) {
  54. // We do not clone anything.
  55. return nodes;
  56. }
  57. /**
  58. * Internal array to store nodes.
  59. *
  60. * @private
  61. * @property {Array} _nodes
  62. */
  63. this._nodes = [];
  64. if ( nodes ) {
  65. var node, character;
  66. if ( !utils.isIterable( nodes ) ) {
  67. nodes = [ nodes ];
  68. }
  69. for ( node of nodes ) {
  70. // Node.
  71. if ( node instanceof Node ) {
  72. this._nodes.push( node );
  73. }
  74. // Text.
  75. else if ( node instanceof Text ) {
  76. for ( character of node.text ) {
  77. this._nodes.push( new Character( character, node.attrs ) );
  78. }
  79. }
  80. // String.
  81. else {
  82. for ( character of node ) {
  83. this._nodes.push( new Character( character ) );
  84. }
  85. }
  86. }
  87. }
  88. }
  89. /**
  90. * Returns node at the given index.
  91. *
  92. * @param {Number} index Node index.
  93. * @returns {document.Node} Node at given index.
  94. */
  95. get( index ) {
  96. return this._nodes[ index ];
  97. }
  98. /**
  99. * Inserts nodes from the given node list into this node list at the given index.
  100. *
  101. * @param {Number} index Position where nodes should be inserted.
  102. * @param {document.NodeList} nodeList List of nodes to insert.
  103. */
  104. insert( index, nodeList ) {
  105. this._nodes.splice.apply( this._nodes, [ index, 0 ].concat( nodeList._nodes ) );
  106. }
  107. /**
  108. * Removes number of nodes starting at the given index.
  109. *
  110. * @param {Number} index Position of the first node to remove.
  111. * @param {Number} number Number of nodes to remove.
  112. * @returns {document.NodeList} List of removed nodes.
  113. */
  114. remove( index, number ) {
  115. return new NodeList( this._nodes.splice( index, number ) );
  116. }
  117. /**
  118. * Search for the node in the node list.
  119. *
  120. * @param {document.Node} node Node to found.
  121. * @returns {Number} Number representing the position where the specified search value occurs for the first time,
  122. * or -1 if it never occurs.
  123. */
  124. indexOf( node ) {
  125. return this._nodes.indexOf( node );
  126. }
  127. /**
  128. * Number of nodes in the node list.
  129. *
  130. * @readonly
  131. * @property {Number} length
  132. */
  133. get length() {
  134. return this._nodes.length;
  135. }
  136. /**
  137. * Node list iterator.
  138. */
  139. [ Symbol.iterator ]() {
  140. return this._nodes[ Symbol.iterator ]();
  141. }
  142. }
  143. return NodeList;
  144. } );