node.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/view/node
  7. */
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  10. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  11. import clone from '@ckeditor/ckeditor5-utils/src/lib/lodash/clone';
  12. /**
  13. * Abstract tree view node class.
  14. *
  15. * @abstract
  16. */
  17. export default class Node {
  18. /**
  19. * Creates a tree view node.
  20. *
  21. * This is an abstract class, so this constructor should not be used directly.
  22. */
  23. constructor() {
  24. /**
  25. * Parent element. Null by default. Set by {@link module:engine/view/element~Element#_insertChildren}.
  26. *
  27. * @readonly
  28. * @member {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment|null}
  29. */
  30. this.parent = null;
  31. }
  32. /**
  33. * Index of the node in the parent element or null if the node has no parent.
  34. *
  35. * Accessing this property throws an error if this node's parent element does not contain it.
  36. * This means that view tree got broken.
  37. *
  38. * @readonly
  39. * @type {Number|null}
  40. */
  41. get index() {
  42. let pos;
  43. if ( !this.parent ) {
  44. return null;
  45. }
  46. // No parent or child doesn't exist in parent's children.
  47. if ( ( pos = this.parent.getChildIndex( this ) ) == -1 ) {
  48. /**
  49. * The node's parent does not contain this node. It means that the document tree is corrupted.
  50. *
  51. * @error view-node-not-found-in-parent
  52. */
  53. throw new CKEditorError( 'view-node-not-found-in-parent: The node\'s parent does not contain this node.' );
  54. }
  55. return pos;
  56. }
  57. /**
  58. * Node's next sibling, or `null` if it is the last child.
  59. *
  60. * @readonly
  61. * @type {module:engine/view/node~Node|null}
  62. */
  63. get nextSibling() {
  64. const index = this.index;
  65. return ( index !== null && this.parent.getChild( index + 1 ) ) || null;
  66. }
  67. /**
  68. * Node's previous sibling, or `null` if it is the first child.
  69. *
  70. * @readonly
  71. * @type {module:engine/view/node~Node|null}
  72. */
  73. get previousSibling() {
  74. const index = this.index;
  75. return ( index !== null && this.parent.getChild( index - 1 ) ) || null;
  76. }
  77. /**
  78. * Top-most ancestor of the node. If the node has no parent it is the root itself.
  79. *
  80. * @readonly
  81. * @type {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment}
  82. */
  83. get root() {
  84. let root = this; // eslint-disable-line consistent-this
  85. while ( root.parent ) {
  86. root = root.parent;
  87. }
  88. return root;
  89. }
  90. /**
  91. * {@link module:engine/view/document~Document View document} that owns this node, or `null` if the node is inside
  92. * {@link module:engine/view/documentfragment~DocumentFragment document fragment}.
  93. *
  94. * @readonly
  95. * @type {module:engine/view/document~Document|null}
  96. */
  97. get document() {
  98. // Parent might be Node, null or DocumentFragment.
  99. if ( this.parent instanceof Node ) {
  100. return this.parent.document;
  101. } else {
  102. return null;
  103. }
  104. }
  105. /**
  106. * Returns ancestors array of this node.
  107. *
  108. * @param {Object} options Options object.
  109. * @param {Boolean} [options.includeSelf=false] When set to `true` this node will be also included in parent's array.
  110. * @param {Boolean} [options.parentFirst=false] When set to `true`, array will be sorted from node's parent to root element,
  111. * otherwise root element will be the first item in the array.
  112. * @returns {Array} Array with ancestors.
  113. */
  114. getAncestors( options = { includeSelf: false, parentFirst: false } ) {
  115. const ancestors = [];
  116. let parent = options.includeSelf ? this : this.parent;
  117. while ( parent ) {
  118. ancestors[ options.parentFirst ? 'push' : 'unshift' ]( parent );
  119. parent = parent.parent;
  120. }
  121. return ancestors;
  122. }
  123. /**
  124. * Returns a {@link module:engine/view/element~Element} or {@link module:engine/view/documentfragment~DocumentFragment}
  125. * which is a common ancestor of both nodes.
  126. *
  127. * @param {module:engine/view/node~Node} node The second node.
  128. * @param {Object} options Options object.
  129. * @param {Boolean} [options.includeSelf=false] When set to `true` both nodes will be considered "ancestors" too.
  130. * Which means that if e.g. node A is inside B, then their common ancestor will be B.
  131. * @returns {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment|null}
  132. */
  133. getCommonAncestor( node, options = {} ) {
  134. const ancestorsA = this.getAncestors( options );
  135. const ancestorsB = node.getAncestors( options );
  136. let i = 0;
  137. while ( ancestorsA[ i ] == ancestorsB[ i ] && ancestorsA[ i ] ) {
  138. i++;
  139. }
  140. return i === 0 ? null : ancestorsA[ i - 1 ];
  141. }
  142. /**
  143. * Removes node from parent.
  144. *
  145. * @protected
  146. */
  147. _remove() {
  148. this.parent._removeChildren( this.index );
  149. }
  150. /**
  151. * @param {module:engine/view/document~ChangeType} type Type of the change.
  152. * @param {module:engine/view/node~Node} node Changed node.
  153. * @fires change
  154. */
  155. _fireChange( type, node ) {
  156. this.fire( 'change:' + type, node );
  157. if ( this.parent ) {
  158. this.parent._fireChange( type, node );
  159. }
  160. }
  161. /**
  162. * Custom toJSON method to solve child-parent circular dependencies.
  163. *
  164. * @returns {Object} Clone of this object with the parent property removed.
  165. */
  166. toJSON() {
  167. const json = clone( this );
  168. // Due to circular references we need to remove parent reference.
  169. delete json.parent;
  170. return json;
  171. }
  172. /**
  173. * Checks whether given view tree object is of given type.
  174. *
  175. * This method is useful when processing view tree objects that are of unknown type. For example, a function
  176. * may return {@link module:engine/view/documentfragment~DocumentFragment} or {@link module:engine/view/node~Node}
  177. * that can be either text node or element. This method can be used to check what kind of object is returned.
  178. *
  179. * obj.is( 'node' ); // true for any node, false for document fragment and text fragment
  180. * obj.is( 'documentFragment' ); // true for document fragment, false for any node
  181. * obj.is( 'element' ); // true for any element, false for text node or document fragment
  182. * obj.is( 'element', 'p' ); // true only for element which name is 'p'
  183. * obj.is( 'p' ); // shortcut for obj.is( 'element', 'p' )
  184. * obj.is( 'text' ); // true for text node, false for element and document fragment
  185. *
  186. * @method #is
  187. * @param {'element'|'containerElement'|'attributeElement'|'emptyElement'|'uiElement'|
  188. * 'rootElement'|'documentFragment'|'text'|'textProxy'} type
  189. * @returns {Boolean}
  190. */
  191. is( type ) {
  192. return type == 'node';
  193. }
  194. /**
  195. * Clones this node.
  196. *
  197. * @method #clone
  198. * @returns {module:engine/view/node~Node} Clone of this node.
  199. */
  200. /**
  201. * Checks if provided node is similar to this node.
  202. *
  203. * @method #isSimilar
  204. * @returns {Boolean} True if nodes are similar.
  205. */
  206. }
  207. /**
  208. * Fired when list of {@link module:engine/view/element~Element elements} children changes.
  209. *
  210. * Change event is bubbled – it is fired on all ancestors.
  211. *
  212. * @event change:children
  213. * @param {module:engine/view/node~Node} changedNode
  214. */
  215. /**
  216. * Fired when list of {@link module:engine/view/element~Element elements} attributes changes.
  217. *
  218. * Change event is bubbled – it is fired on all ancestors.
  219. *
  220. * @event change:attributes
  221. * @param {module:engine/view/node~Node} changedNode
  222. */
  223. /**
  224. * Fired when {@link module:engine/view/text~Text text nodes} data changes.
  225. *
  226. * Change event is bubbled – it is fired on all ancestors.
  227. *
  228. * @event change:text
  229. * @param {module:engine/view/node~Node} changedNode
  230. */
  231. /**
  232. * @event change
  233. */
  234. mix( Node, EmitterMixin );