documentfragment.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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. * Document fragment.
  15. *
  16. * To create a new document fragment instance use the
  17. * {@link module:engine/view/upcastwriter~UpcastWriter#createDocumentFragment `UpcastWriter#createDocumentFragment()`}
  18. * method.
  19. */
  20. export default class DocumentFragment {
  21. /**
  22. * Creates new DocumentFragment instance.
  23. *
  24. * @protected
  25. * @param {module:engine/view/node~Node|Iterable.<module:engine/view/node~Node>} [children]
  26. * A list of nodes to be inserted into the created document fragment.
  27. */
  28. constructor( children ) {
  29. /**
  30. * Array of child nodes.
  31. *
  32. * @protected
  33. * @member {Array.<module:engine/view/element~Element>} module:engine/view/documentfragment~DocumentFragment#_children
  34. */
  35. this._children = [];
  36. if ( children ) {
  37. this._insertChild( 0, children );
  38. }
  39. }
  40. /**
  41. * Iterable interface.
  42. *
  43. * Iterates over nodes added to this document fragment.
  44. *
  45. * @returns {Iterable.<module:engine/view/node~Node>}
  46. */
  47. [ Symbol.iterator ]() {
  48. return this._children[ Symbol.iterator ]();
  49. }
  50. /**
  51. * Number of child nodes in this document fragment.
  52. *
  53. * @readonly
  54. * @type {Number}
  55. */
  56. get childCount() {
  57. return this._children.length;
  58. }
  59. /**
  60. * Is `true` if there are no nodes inside this document fragment, `false` otherwise.
  61. *
  62. * @readonly
  63. * @type {Boolean}
  64. */
  65. get isEmpty() {
  66. return this.childCount === 0;
  67. }
  68. /**
  69. * Artificial root of `DocumentFragment`. Returns itself. Added for compatibility reasons.
  70. *
  71. * @readonly
  72. * @type {module:engine/model/documentfragment~DocumentFragment}
  73. */
  74. get root() {
  75. return this;
  76. }
  77. /**
  78. * Artificial parent of `DocumentFragment`. Returns `null`. Added for compatibility reasons.
  79. *
  80. * @readonly
  81. * @type {null}
  82. */
  83. get parent() {
  84. return null;
  85. }
  86. /**
  87. * Checks whether this object is of the given type.
  88. *
  89. * docFrag.is( 'documentFragment' ); // -> true
  90. * docFrag.is( 'view:documentFragment' ); // -> true
  91. *
  92. * docFrag.is( 'model:documentFragment' ); // -> false
  93. * docFrag.is( 'element' ); // -> false
  94. * docFrag.is( 'node' ); // -> false
  95. *
  96. * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
  97. *
  98. * @param {String} type
  99. * @returns {Boolean}
  100. */
  101. is( type ) {
  102. return type == 'documentFragment' || type == 'view:documentFragment';
  103. }
  104. /**
  105. * {@link module:engine/view/documentfragment~DocumentFragment#_insertChild Insert} a child node or a list of child nodes at the end
  106. * and sets the parent of these nodes to this fragment.
  107. *
  108. * @param {module:engine/view/item~Item|Iterable.<module:engine/view/item~Item>} items Items to be inserted.
  109. * @returns {Number} Number of appended nodes.
  110. */
  111. _appendChild( items ) {
  112. return this._insertChild( this.childCount, items );
  113. }
  114. /**
  115. * Gets child at the given index.
  116. *
  117. * @param {Number} index Index of child.
  118. * @returns {module:engine/view/node~Node} Child node.
  119. */
  120. getChild( index ) {
  121. return this._children[ index ];
  122. }
  123. /**
  124. * Gets index of the given child node. Returns `-1` if child node is not found.
  125. *
  126. * @param {module:engine/view/node~Node} node Child node.
  127. * @returns {Number} Index of the child node.
  128. */
  129. getChildIndex( node ) {
  130. return this._children.indexOf( node );
  131. }
  132. /**
  133. * Gets child nodes iterator.
  134. *
  135. * @returns {Iterable.<module:engine/view/node~Node>} Child nodes iterator.
  136. */
  137. getChildren() {
  138. return this._children[ Symbol.iterator ]();
  139. }
  140. /**
  141. * Inserts a child node or a list of child nodes on the given index and sets the parent of these nodes to
  142. * this fragment.
  143. *
  144. * @param {Number} index Position where nodes should be inserted.
  145. * @param {module:engine/view/item~Item|Iterable.<module:engine/view/item~Item>} items Items to be inserted.
  146. * @returns {Number} Number of inserted nodes.
  147. */
  148. _insertChild( index, items ) {
  149. this._fireChange( 'children', this );
  150. let count = 0;
  151. const nodes = normalize( items );
  152. for ( const node of nodes ) {
  153. // If node that is being added to this element is already inside another element, first remove it from the old parent.
  154. if ( node.parent !== null ) {
  155. node._remove();
  156. }
  157. node.parent = this;
  158. this._children.splice( index, 0, node );
  159. index++;
  160. count++;
  161. }
  162. return count;
  163. }
  164. /**
  165. * Removes number of child nodes starting at the given index and set the parent of these nodes to `null`.
  166. *
  167. * @param {Number} index Number of the first node to remove.
  168. * @param {Number} [howMany=1] Number of nodes to remove.
  169. * @returns {Array.<module:engine/view/node~Node>} The array of removed nodes.
  170. */
  171. _removeChildren( index, howMany = 1 ) {
  172. this._fireChange( 'children', this );
  173. for ( let i = index; i < index + howMany; i++ ) {
  174. this._children[ i ].parent = null;
  175. }
  176. return this._children.splice( index, howMany );
  177. }
  178. /**
  179. * Fires `change` event with given type of the change.
  180. *
  181. * @private
  182. * @param {module:engine/view/document~ChangeType} type Type of the change.
  183. * @param {module:engine/view/node~Node} node Changed node.
  184. * @fires module:engine/view/node~Node#change
  185. */
  186. _fireChange( type, node ) {
  187. this.fire( 'change:' + type, node );
  188. }
  189. // @if CK_DEBUG_ENGINE // printTree() {
  190. // @if CK_DEBUG_ENGINE // let string = 'ViewDocumentFragment: [';
  191. // @if CK_DEBUG_ENGINE // for ( const child of this.getChildren() ) {
  192. // @if CK_DEBUG_ENGINE // if ( child.is( 'text' ) ) {
  193. // @if CK_DEBUG_ENGINE // string += '\n' + '\t'.repeat( 1 ) + child.data;
  194. // @if CK_DEBUG_ENGINE // } else {
  195. // @if CK_DEBUG_ENGINE // string += '\n' + child.printTree( 1 );
  196. // @if CK_DEBUG_ENGINE // }
  197. // @if CK_DEBUG_ENGINE // }
  198. // @if CK_DEBUG_ENGINE // string += '\n]';
  199. // @if CK_DEBUG_ENGINE // return string;
  200. // @if CK_DEBUG_ENGINE // }
  201. // @if CK_DEBUG_ENGINE // logTree() {
  202. // @if CK_DEBUG_ENGINE // console.log( this.printTree() );
  203. // @if CK_DEBUG_ENGINE // }
  204. }
  205. mix( DocumentFragment, EmitterMixin );
  206. // Converts strings to Text and non-iterables to arrays.
  207. //
  208. // @param {String|module:engine/view/item~Item|Iterable.<String|module:engine/view/item~Item>}
  209. // @returns {Iterable.<module:engine/view/node~Node>}
  210. function normalize( nodes ) {
  211. // Separate condition because string is iterable.
  212. if ( typeof nodes == 'string' ) {
  213. return [ new Text( nodes ) ];
  214. }
  215. if ( !isIterable( nodes ) ) {
  216. nodes = [ nodes ];
  217. }
  218. // Array.from to enable .map() on non-arrays.
  219. return Array.from( nodes )
  220. .map( node => {
  221. if ( typeof node == 'string' ) {
  222. return new Text( node );
  223. }
  224. if ( node instanceof TextProxy ) {
  225. return new Text( node.data );
  226. }
  227. return node;
  228. } );
  229. }