node.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. import compareArrays from '@ckeditor/ckeditor5-utils/src/comparearrays';
  13. /**
  14. * Abstract tree view node class.
  15. *
  16. * @abstract
  17. */
  18. export default class Node {
  19. /**
  20. * Creates a tree view node.
  21. *
  22. * This is an abstract class, so this constructor should not be used directly.
  23. */
  24. constructor() {
  25. /**
  26. * Parent element. Null by default. Set by {@link module:engine/view/element~Element#_insertChildren}.
  27. *
  28. * @readonly
  29. * @member {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment|null}
  30. */
  31. this.parent = null;
  32. }
  33. /**
  34. * Index of the node in the parent element or null if the node has no parent.
  35. *
  36. * Accessing this property throws an error if this node's parent element does not contain it.
  37. * This means that view tree got broken.
  38. *
  39. * @readonly
  40. * @type {Number|null}
  41. */
  42. get index() {
  43. let pos;
  44. if ( !this.parent ) {
  45. return null;
  46. }
  47. // No parent or child doesn't exist in parent's children.
  48. if ( ( pos = this.parent.getChildIndex( this ) ) == -1 ) {
  49. /**
  50. * The node's parent does not contain this node. It means that the document tree is corrupted.
  51. *
  52. * @error view-node-not-found-in-parent
  53. */
  54. throw new CKEditorError( 'view-node-not-found-in-parent: The node\'s parent does not contain this node.' );
  55. }
  56. return pos;
  57. }
  58. /**
  59. * Node's next sibling, or `null` if it is the last child.
  60. *
  61. * @readonly
  62. * @type {module:engine/view/node~Node|null}
  63. */
  64. get nextSibling() {
  65. const index = this.index;
  66. return ( index !== null && this.parent.getChild( index + 1 ) ) || null;
  67. }
  68. /**
  69. * Node's previous sibling, or `null` if it is the first child.
  70. *
  71. * @readonly
  72. * @type {module:engine/view/node~Node|null}
  73. */
  74. get previousSibling() {
  75. const index = this.index;
  76. return ( index !== null && this.parent.getChild( index - 1 ) ) || null;
  77. }
  78. /**
  79. * Top-most ancestor of the node. If the node has no parent it is the root itself.
  80. *
  81. * @readonly
  82. * @type {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment}
  83. */
  84. get root() {
  85. let root = this; // eslint-disable-line consistent-this
  86. while ( root.parent ) {
  87. root = root.parent;
  88. }
  89. return root;
  90. }
  91. /**
  92. * {@link module:engine/view/document~Document View document} that owns this node, or `null` if the node is inside
  93. * {@link module:engine/view/documentfragment~DocumentFragment document fragment}.
  94. *
  95. * @readonly
  96. * @type {module:engine/view/document~Document|null}
  97. */
  98. get document() {
  99. // Parent might be Node, null or DocumentFragment.
  100. if ( this.parent instanceof Node ) {
  101. return this.parent.document;
  102. } else {
  103. return null;
  104. }
  105. }
  106. /**
  107. * Gets a path to the node. The path is an array containing indices of consecutive ancestors of this node,
  108. * beginning from {@link module:engine/view/node~Node#root root}, down to this node's index.
  109. *
  110. * const abc = new Text( 'abc' );
  111. * const foo = new Text( 'foo' );
  112. * const h1 = new Element( 'h1', null, new Text( 'header' ) );
  113. * const p = new Element( 'p', null, [ abc, foo ] );
  114. * const div = new Element( 'div', null, [ h1, p ] );
  115. * foo.getPath(); // Returns [ 1, 3 ]. `foo` is in `p` which is in `div`. `p` starts at offset 1, while `foo` at 3.
  116. * h1.getPath(); // Returns [ 0 ].
  117. * div.getPath(); // Returns [].
  118. *
  119. * @returns {Array.<Number>} The path.
  120. */
  121. getPath() {
  122. const path = [];
  123. let node = this; // eslint-disable-line consistent-this
  124. while ( node.parent ) {
  125. path.unshift( node.index );
  126. node = node.parent;
  127. }
  128. return path;
  129. }
  130. /**
  131. * Returns ancestors array of this node.
  132. *
  133. * @param {Object} options Options object.
  134. * @param {Boolean} [options.includeSelf=false] When set to `true` this node will be also included in parent's array.
  135. * @param {Boolean} [options.parentFirst=false] When set to `true`, array will be sorted from node's parent to root element,
  136. * otherwise root element will be the first item in the array.
  137. * @returns {Array} Array with ancestors.
  138. */
  139. getAncestors( options = { includeSelf: false, parentFirst: false } ) {
  140. const ancestors = [];
  141. let parent = options.includeSelf ? this : this.parent;
  142. while ( parent ) {
  143. ancestors[ options.parentFirst ? 'push' : 'unshift' ]( parent );
  144. parent = parent.parent;
  145. }
  146. return ancestors;
  147. }
  148. /**
  149. * Returns a {@link module:engine/view/element~Element} or {@link module:engine/view/documentfragment~DocumentFragment}
  150. * which is a common ancestor of both nodes.
  151. *
  152. * @param {module:engine/view/node~Node} node The second node.
  153. * @param {Object} options Options object.
  154. * @param {Boolean} [options.includeSelf=false] When set to `true` both nodes will be considered "ancestors" too.
  155. * Which means that if e.g. node A is inside B, then their common ancestor will be B.
  156. * @returns {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment|null}
  157. */
  158. getCommonAncestor( node, options = {} ) {
  159. const ancestorsA = this.getAncestors( options );
  160. const ancestorsB = node.getAncestors( options );
  161. let i = 0;
  162. while ( ancestorsA[ i ] == ancestorsB[ i ] && ancestorsA[ i ] ) {
  163. i++;
  164. }
  165. return i === 0 ? null : ancestorsA[ i - 1 ];
  166. }
  167. /**
  168. * Returns whether this node is before given node. `false` is returned if nodes are in different trees (for example,
  169. * in different {@link module:engine/view/documentfragment~DocumentFragment}s).
  170. *
  171. * @param {module:engine/view/node~Node} node Node to compare with.
  172. * @returns {Boolean}
  173. */
  174. isBefore( node ) {
  175. // Given node is not before this node if they are same.
  176. if ( this == node ) {
  177. return false;
  178. }
  179. // Return `false` if it is impossible to compare nodes.
  180. if ( this.root !== node.root ) {
  181. return false;
  182. }
  183. const thisPath = this.getPath();
  184. const nodePath = node.getPath();
  185. const result = compareArrays( thisPath, nodePath );
  186. switch ( result ) {
  187. case 'prefix':
  188. return true;
  189. case 'extension':
  190. return false;
  191. default:
  192. return thisPath[ result ] < nodePath[ result ];
  193. }
  194. }
  195. /**
  196. * Returns whether this node is after given node. `false` is returned if nodes are in different trees (for example,
  197. * in different {@link module:engine/view/documentfragment~DocumentFragment}s).
  198. *
  199. * @param {module:engine/view/node~Node} node Node to compare with.
  200. * @returns {Boolean}
  201. */
  202. isAfter( node ) {
  203. // Given node is not before this node if they are same.
  204. if ( this == node ) {
  205. return false;
  206. }
  207. // Return `false` if it is impossible to compare nodes.
  208. if ( this.root !== node.root ) {
  209. return false;
  210. }
  211. // In other cases, just check if the `node` is before, and return the opposite.
  212. return !this.isBefore( node );
  213. }
  214. /**
  215. * Removes node from parent.
  216. *
  217. * @protected
  218. */
  219. _remove() {
  220. this.parent._removeChildren( this.index );
  221. }
  222. /**
  223. * @param {module:engine/view/document~ChangeType} type Type of the change.
  224. * @param {module:engine/view/node~Node} node Changed node.
  225. * @fires change
  226. */
  227. _fireChange( type, node ) {
  228. this.fire( 'change:' + type, node );
  229. if ( this.parent ) {
  230. this.parent._fireChange( type, node );
  231. }
  232. }
  233. /**
  234. * Custom toJSON method to solve child-parent circular dependencies.
  235. *
  236. * @returns {Object} Clone of this object with the parent property removed.
  237. */
  238. toJSON() {
  239. const json = clone( this );
  240. // Due to circular references we need to remove parent reference.
  241. delete json.parent;
  242. return json;
  243. }
  244. /**
  245. * Checks whether given view tree object is of given type.
  246. *
  247. * This method is useful when processing view tree objects that are of unknown type. For example, a function
  248. * may return {@link module:engine/view/documentfragment~DocumentFragment} or {@link module:engine/view/node~Node}
  249. * that can be either text node or element. This method can be used to check what kind of object is returned.
  250. *
  251. * obj.is( 'node' ); // true for any node, false for document fragment and text fragment
  252. * obj.is( 'documentFragment' ); // true for document fragment, false for any node
  253. * obj.is( 'element' ); // true for any element, false for text node or document fragment
  254. * obj.is( 'element', 'p' ); // true only for element which name is 'p'
  255. * obj.is( 'p' ); // shortcut for obj.is( 'element', 'p' )
  256. * obj.is( 'text' ); // true for text node, false for element and document fragment
  257. *
  258. * @method #is
  259. * @param {'element'|'containerElement'|'attributeElement'|'emptyElement'|'uiElement'|
  260. * 'rootElement'|'documentFragment'|'text'|'textProxy'} type
  261. * @returns {Boolean}
  262. */
  263. is( type ) {
  264. return type == 'node';
  265. }
  266. /**
  267. * Clones this node.
  268. *
  269. * @protected
  270. * @method #_clone
  271. * @returns {module:engine/view/node~Node} Clone of this node.
  272. */
  273. /**
  274. * Checks if provided node is similar to this node.
  275. *
  276. * @method #isSimilar
  277. * @returns {Boolean} True if nodes are similar.
  278. */
  279. }
  280. /**
  281. * Fired when list of {@link module:engine/view/element~Element elements} children changes.
  282. *
  283. * Change event is bubbled – it is fired on all ancestors.
  284. *
  285. * @event change:children
  286. * @param {module:engine/view/node~Node} changedNode
  287. */
  288. /**
  289. * Fired when list of {@link module:engine/view/element~Element elements} attributes changes.
  290. *
  291. * Change event is bubbled – it is fired on all ancestors.
  292. *
  293. * @event change:attributes
  294. * @param {module:engine/view/node~Node} changedNode
  295. */
  296. /**
  297. * Fired when {@link module:engine/view/text~Text text nodes} data changes.
  298. *
  299. * Change event is bubbled – it is fired on all ancestors.
  300. *
  301. * @event change:text
  302. * @param {module:engine/view/node~Node} changedNode
  303. */
  304. /**
  305. * @event change
  306. */
  307. mix( Node, EmitterMixin );