8
0

node.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. remove() {
  146. this.parent.removeChildren( this.index );
  147. }
  148. /**
  149. * @param {module:engine/view/document~ChangeType} type Type of the change.
  150. * @param {module:engine/view/node~Node} node Changed node.
  151. * @fires change
  152. */
  153. _fireChange( type, node ) {
  154. this.fire( 'change:' + type, node );
  155. if ( this.parent ) {
  156. this.parent._fireChange( type, node );
  157. }
  158. }
  159. /**
  160. * Custom toJSON method to solve child-parent circular dependencies.
  161. *
  162. * @returns {Object} Clone of this object with the parent property removed.
  163. */
  164. toJSON() {
  165. const json = clone( this );
  166. // Due to circular references we need to remove parent reference.
  167. delete json.parent;
  168. return json;
  169. }
  170. /**
  171. * Clones this node.
  172. *
  173. * @method #clone
  174. * @returns {module:engine/view/node~Node} Clone of this node.
  175. */
  176. /**
  177. * Checks if provided node is similar to this node.
  178. *
  179. * @method #isSimilar
  180. * @returns {Boolean} True if nodes are similar.
  181. */
  182. /**
  183. * Checks whether given view tree object is of given type.
  184. *
  185. * This method is useful when processing view tree objects that are of unknown type. For example, a function
  186. * may return {@link module:engine/view/documentfragment~DocumentFragment} or {@link module:engine/view/node~Node}
  187. * that can be either text node or element. This method can be used to check what kind of object is returned.
  188. *
  189. * obj.is( 'node' ); // true for any node, false for document fragment
  190. * obj.is( 'documentFragment' ); // true for document fragment, false for any node
  191. * obj.is( 'element' ); // true for any element, false for text node or document fragment
  192. * obj.is( 'element', 'p' ); // true only for element which name is 'p'
  193. * obj.is( 'p' ); // shortcut for obj.is( 'element', 'p' )
  194. * obj.is( 'text' ); // true for text node, false for element and document fragment
  195. *
  196. * @method #is
  197. * @param {'element'|'containerElement'|'attributeElement'|'emptyElement'|'uiElement'|
  198. * 'rootElement'|'documentFragment'|'text'|'textProxy'} type
  199. * @returns {Boolean}
  200. */
  201. }
  202. /**
  203. * Fired when list of {@link module:engine/view/element~Element elements} children changes.
  204. *
  205. * Change event is bubbled – it is fired on all ancestors.
  206. *
  207. * @event change:children
  208. * @param {module:engine/view/node~Node} changedNode
  209. */
  210. /**
  211. * Fired when list of {@link module:engine/view/element~Element elements} attributes changes.
  212. *
  213. * Change event is bubbled – it is fired on all ancestors.
  214. *
  215. * @event change:attributes
  216. * @param {module:engine/view/node~Node} changedNode
  217. */
  218. /**
  219. * Fired when {@link module:engine/view/text~Text text nodes} data changes.
  220. *
  221. * Change event is bubbled – it is fired on all ancestors.
  222. *
  223. * @event change:text
  224. * @param {module:engine/view/node~Node} changedNode
  225. */
  226. /**
  227. * @event change
  228. */
  229. mix( Node, EmitterMixin );