documentfragment.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/view/documentfragment
  7. */
  8. import Text from './text';
  9. import mix from 'ckeditor5-utils/src/mix';
  10. import isIterable from 'ckeditor5-utils/src/isiterable';
  11. import EmitterMixin from 'ckeditor5-utils/src/emittermixin';
  12. /**
  13. * DocumentFragment class.
  14. */
  15. export default class DocumentFragment {
  16. /**
  17. * Creates new DocumentFragment instance.
  18. *
  19. * @param {module:engine/view/node~Node|Iterable.<module:engine/view/node~Node>} [children] List of nodes to be inserted into
  20. * created document fragment.
  21. */
  22. constructor( children ) {
  23. /**
  24. * Array of child nodes.
  25. *
  26. * @protected
  27. * @member {Array.<module:engine/view/element~Element>} module:engine/view/documentfragment~DocumentFragment#_children
  28. */
  29. this._children = [];
  30. if ( children ) {
  31. this.insertChildren( 0, children );
  32. }
  33. }
  34. /**
  35. * Iterates over nodes added to this DocumentFragment.
  36. */
  37. [ Symbol.iterator ]() {
  38. return this._children[ Symbol.iterator ]();
  39. }
  40. /**
  41. * Number of child nodes in this document fragment.
  42. *
  43. * @readonly
  44. * @type {Number}
  45. */
  46. get childCount() {
  47. return this._children.length;
  48. }
  49. /**
  50. * Is `true` if there are no nodes inside this document fragment, `false` otherwise.
  51. *
  52. * @readonly
  53. * @type {Boolean}
  54. */
  55. get isEmpty() {
  56. return this.childCount === 0;
  57. }
  58. /**
  59. * Artificial root of `DocumentFragment`. Returns itself. Added for compatibility reasons.
  60. *
  61. * @readonly
  62. * @type {module:engine/model/documentfragment~DocumentFragment}
  63. */
  64. get root() {
  65. return this;
  66. }
  67. /**
  68. * Artificial parent of `DocumentFragment`. Returns `null`. Added for compatibility reasons.
  69. *
  70. * @readonly
  71. * @type {null}
  72. */
  73. get parent() {
  74. return null;
  75. }
  76. /**
  77. * Returns ancestor elements of `DocumentFragment`, which is an empty array. Added for compatibility reasons.
  78. *
  79. * @returns {Array}
  80. */
  81. getAncestors() {
  82. return [];
  83. }
  84. /**
  85. * {@link module:engine/view/documentfragment~DocumentFragment#insertChildren Insert} a child node or a list of child nodes at the end
  86. * and sets the parent of these nodes to this fragment.
  87. *
  88. * @param {module:engine/view/node~Node|Iterable.<module:engine/view/node~Node>} nodes Node or the list of nodes to be inserted.
  89. * @returns {Number} Number of appended nodes.
  90. */
  91. appendChildren( nodes ) {
  92. return this.insertChildren( this.childCount, nodes );
  93. }
  94. /**
  95. * Gets child at the given index.
  96. *
  97. * @param {Number} index Index of child.
  98. * @returns {module:engine/view/node~Node} Child node.
  99. */
  100. getChild( index ) {
  101. return this._children[ index ];
  102. }
  103. /**
  104. * Gets index of the given child node. Returns `-1` if child node is not found.
  105. *
  106. * @param {module:engine/view/node~Node} node Child node.
  107. * @returns {Number} Index of the child node.
  108. */
  109. getChildIndex( node ) {
  110. return this._children.indexOf( node );
  111. }
  112. /**
  113. * Gets child nodes iterator.
  114. *
  115. * @returns {Iterable.<module:engine/view/node~Node>} Child nodes iterator.
  116. */
  117. getChildren() {
  118. return this._children[ Symbol.iterator ]();
  119. }
  120. /**
  121. * Inserts a child node or a list of child nodes on the given index and sets the parent of these nodes to
  122. * this fragment.
  123. *
  124. * @param {Number} index Position where nodes should be inserted.
  125. * @param {module:engine/view/node~Node|Iterable.<module:engine/view/node~Node>} nodes Node or list of nodes to be inserted.
  126. * @returns {Number} Number of inserted nodes.
  127. */
  128. insertChildren( index, nodes ) {
  129. this._fireChange( 'children', this );
  130. let count = 0;
  131. nodes = normalize( nodes );
  132. for ( let node of nodes ) {
  133. node.parent = this;
  134. this._children.splice( index, 0, node );
  135. index++;
  136. count++;
  137. }
  138. return count;
  139. }
  140. /**
  141. * Removes number of child nodes starting at the given index and set the parent of these nodes to `null`.
  142. *
  143. * @param {Number} index Number of the first node to remove.
  144. * @param {Number} [howMany=1] Number of nodes to remove.
  145. * @returns {Array.<module:engine/view/node~Node>} The array of removed nodes.
  146. */
  147. removeChildren( index, howMany = 1 ) {
  148. this._fireChange( 'children', this );
  149. for ( let i = index; i < index + howMany; i++ ) {
  150. this._children[ i ].parent = null;
  151. }
  152. return this._children.splice( index, howMany );
  153. }
  154. /**
  155. * Fires `change` event with given type of the change.
  156. *
  157. * @private
  158. * @param {module:engine/view/document~ChangeType} type Type of the change.
  159. * @param {module:engine/view/node~Node} node Changed node.
  160. * @fires module:engine/view/node~Node#change
  161. */
  162. _fireChange( type, node ) {
  163. this.fire( 'change:' + type, node );
  164. }
  165. }
  166. mix( DocumentFragment, EmitterMixin );
  167. // Converts strings to Text and non-iterables to arrays.
  168. //
  169. // @param {String|module:engine/view/node~Node|Iterable.<String|module:engine/view/node~Node>}
  170. // @return {Iterable.<module:engine/view/node~Node>}
  171. function normalize( nodes ) {
  172. // Separate condition because string is iterable.
  173. if ( typeof nodes == 'string' ) {
  174. return [ new Text( nodes ) ];
  175. }
  176. if ( !isIterable( nodes ) ) {
  177. nodes = [ nodes ];
  178. }
  179. // Array.from to enable .map() on non-arrays.
  180. return Array.from( nodes ).map( ( node ) => typeof node == 'string' ? new Text( node ) : node );
  181. }