documentfragment.js 5.6 KB

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