8
0

node.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Element from '../../src/view/element';
  6. import Text from '../../src/view/text';
  7. import Node from '../../src/view/node';
  8. import DocumentFragment from '../../src/view/documentfragment';
  9. import RootEditableElement from '../../src/view/rooteditableelement';
  10. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  11. import createDocumentMock from '../../tests/view/_utils/createdocumentmock';
  12. describe( 'Node', () => {
  13. let root,
  14. one, two, three,
  15. charB, charA, charR, img;
  16. before( () => {
  17. charB = new Text( 'b' );
  18. charA = new Text( 'a' );
  19. img = new Element( 'img' );
  20. charR = new Text( 'r' );
  21. one = new Element( 'one' );
  22. two = new Element( 'two', null, [ charB, charA, img, charR ] );
  23. three = new Element( 'three' );
  24. root = new Element( null, null, [ one, two, three ] );
  25. } );
  26. describe( 'is()', () => {
  27. it( 'should return true for node', () => {
  28. const node = new Node();
  29. expect( node.is( 'node' ) ).to.be.true;
  30. } );
  31. } );
  32. describe( 'getNextSibling/getPreviousSibling()', () => {
  33. it( 'should return next sibling', () => {
  34. expect( root.nextSibling ).to.be.null;
  35. expect( one.nextSibling ).to.equal( two );
  36. expect( two.nextSibling ).to.equal( three );
  37. expect( three.nextSibling ).to.be.null;
  38. expect( charB.nextSibling ).to.equal( charA );
  39. expect( charA.nextSibling ).to.equal( img );
  40. expect( img.nextSibling ).to.equal( charR );
  41. expect( charR.nextSibling ).to.be.null;
  42. } );
  43. it( 'should return previous sibling', () => {
  44. expect( root.previousSibling ).to.be.null;
  45. expect( one.previousSibling ).to.be.null;
  46. expect( two.previousSibling ).to.equal( one );
  47. expect( three.previousSibling ).to.equal( two );
  48. expect( charB.previousSibling ).to.be.null;
  49. expect( charA.previousSibling ).to.equal( charB );
  50. expect( img.previousSibling ).to.equal( charA );
  51. expect( charR.previousSibling ).to.equal( img );
  52. } );
  53. } );
  54. describe( 'getAncestors()', () => {
  55. it( 'should return empty array for node without ancestors', () => {
  56. const result = root.getAncestors();
  57. expect( result ).to.be.an( 'array' );
  58. expect( result.length ).to.equal( 0 );
  59. } );
  60. it( 'should return array including node itself if requested', () => {
  61. const result = root.getAncestors( { includeSelf: true } );
  62. expect( result ).to.be.an( 'array' );
  63. expect( result.length ).to.equal( 1 );
  64. expect( result[ 0 ] ).to.equal( root );
  65. } );
  66. it( 'should return array of ancestors', () => {
  67. const result = charR.getAncestors();
  68. expect( result.length ).to.equal( 2 );
  69. expect( result[ 0 ] ).to.equal( root );
  70. expect( result[ 1 ] ).to.equal( two );
  71. const result2 = charR.getAncestors( { includeSelf: true } );
  72. expect( result2.length ).to.equal( 3 );
  73. expect( result2[ 0 ] ).to.equal( root );
  74. expect( result2[ 1 ] ).to.equal( two );
  75. expect( result2[ 2 ] ).to.equal( charR );
  76. } );
  77. it( 'should return array of ancestors starting from parent', () => {
  78. const result = charR.getAncestors( { parentFirst: true } );
  79. expect( result.length ).to.equal( 2 );
  80. expect( result[ 0 ] ).to.equal( two );
  81. expect( result[ 1 ] ).to.equal( root );
  82. const result2 = charR.getAncestors( { includeSelf: true, parentFirst: true } );
  83. expect( result2.length ).to.equal( 3 );
  84. expect( result2[ 2 ] ).to.equal( root );
  85. expect( result2[ 1 ] ).to.equal( two );
  86. expect( result2[ 0 ] ).to.equal( charR );
  87. } );
  88. it( 'should return ancestors including DocumentFragment', () => {
  89. const fragment = new DocumentFragment( root );
  90. const result = img.getAncestors();
  91. root._remove();
  92. expect( result.length ).to.equal( 3 );
  93. expect( result[ 0 ] ).to.equal( fragment );
  94. expect( result[ 1 ] ).to.equal( root );
  95. expect( result[ 2 ] ).to.equal( two );
  96. } );
  97. } );
  98. describe( 'getCommonAncestor()', () => {
  99. it( 'should return the parent element for the same node', () => {
  100. expect( img.getCommonAncestor( img ) ).to.equal( two );
  101. } );
  102. it( 'should return the given node for the same node if includeSelf is used', () => {
  103. expect( img.getCommonAncestor( img, { includeSelf: true } ) ).to.equal( img );
  104. } );
  105. it( 'should return null for detached subtrees', () => {
  106. const detached = new Element( 'foo' );
  107. expect( img.getCommonAncestor( detached ) ).to.be.null;
  108. expect( detached.getCommonAncestor( img ) ).to.be.null;
  109. expect( img.getCommonAncestor( detached, { includeSelf: true } ) ).to.be.null;
  110. expect( detached.getCommonAncestor( img, { includeSelf: true } ) ).to.be.null;
  111. } );
  112. it( 'should return null when one of the nodes is a tree root itself', () => {
  113. expect( root.getCommonAncestor( img ) ).to.be.null;
  114. expect( img.getCommonAncestor( root ) ).to.be.null;
  115. expect( root.getCommonAncestor( root ) ).to.be.null;
  116. } );
  117. it( 'should return root when one of the nodes is a tree root itself and includeSelf is used', () => {
  118. expect( root.getCommonAncestor( img, { includeSelf: true } ) ).to.equal( root );
  119. expect( img.getCommonAncestor( root, { includeSelf: true } ) ).to.equal( root );
  120. expect( root.getCommonAncestor( root, { includeSelf: true } ) ).to.equal( root );
  121. } );
  122. it( 'should return parent of the nodes at the same level', () => {
  123. expect( img.getCommonAncestor( charA ), 1 ).to.equal( two );
  124. expect( charB.getCommonAncestor( charA ), 2 ).to.equal( two );
  125. expect( img.getCommonAncestor( charA, { includeSelf: true } ), 3 ).to.equal( two );
  126. expect( charB.getCommonAncestor( charA, { includeSelf: true } ), 4 ).to.equal( two );
  127. } );
  128. it( 'should return proper element for nodes in different branches and on different levels', () => {
  129. const foo = new Text( 'foo' );
  130. const bar = new Text( 'bar' );
  131. const bom = new Text( 'bom' );
  132. const d = new Element( 'd', null, [ bar ] );
  133. const c = new Element( 'c', null, [ foo, d ] );
  134. const b = new Element( 'b', null, [ c ] );
  135. const e = new Element( 'e', null, [ bom ] );
  136. const a = new Element( 'a', null, [ b, e ] );
  137. // <a><b><c>foo<d>bar</d></c></b><e>bom</e></a>
  138. expect( bar.getCommonAncestor( foo ), 1 ).to.equal( c );
  139. expect( foo.getCommonAncestor( d ), 2 ).to.equal( c );
  140. expect( c.getCommonAncestor( b ), 3 ).to.equal( a );
  141. expect( bom.getCommonAncestor( d ), 4 ).to.equal( a );
  142. expect( b.getCommonAncestor( bom ), 5 ).to.equal( a );
  143. expect( b.getCommonAncestor( bar ), 6 ).to.equal( a );
  144. expect( bar.getCommonAncestor( foo, { includeSelf: true } ), 11 ).to.equal( c );
  145. expect( foo.getCommonAncestor( d, { includeSelf: true } ), 12 ).to.equal( c );
  146. expect( c.getCommonAncestor( b, { includeSelf: true } ), 13 ).to.equal( b );
  147. expect( bom.getCommonAncestor( d, { includeSelf: true } ), 14 ).to.equal( a );
  148. expect( b.getCommonAncestor( bom, { includeSelf: true } ), 15 ).to.equal( a );
  149. expect( b.getCommonAncestor( bar, { includeSelf: true } ), 16 ).to.equal( b );
  150. } );
  151. it( 'should return document fragment', () => {
  152. const foo = new Text( 'foo' );
  153. const bar = new Text( 'bar' );
  154. const df = new DocumentFragment( [ foo, bar ] );
  155. expect( foo.getCommonAncestor( bar ) ).to.equal( df );
  156. } );
  157. } );
  158. describe( 'getIndex()', () => {
  159. it( 'should return null if the parent is null', () => {
  160. expect( root.index ).to.be.null;
  161. } );
  162. it( 'should return index in the parent', () => {
  163. expect( one.index ).to.equal( 0 );
  164. expect( two.index ).to.equal( 1 );
  165. expect( three.index ).to.equal( 2 );
  166. expect( charB.index ).to.equal( 0 );
  167. expect( charA.index ).to.equal( 1 );
  168. expect( img.index ).to.equal( 2 );
  169. expect( charR.index ).to.equal( 3 );
  170. } );
  171. it( 'should throw an error if parent does not contain element', () => {
  172. const f = new Text( 'f' );
  173. const bar = new Element( 'bar', [], [] );
  174. f.parent = bar;
  175. expect(
  176. () => {
  177. f.index;
  178. }
  179. ).to.throw( CKEditorError, /view-node-not-found-in-parent/ );
  180. } );
  181. } );
  182. describe( 'getPath()', () => {
  183. it( 'should return empty array is the element is the root', () => {
  184. expect( root.getPath() ).to.deep.equal( [] );
  185. } );
  186. it( 'should return array with indices of given element and its ancestors starting from top-most one', () => {
  187. expect( one.getPath() ).to.deep.equal( [ 0 ] );
  188. expect( two.getPath() ).to.deep.equal( [ 1 ] );
  189. expect( img.getPath() ).to.deep.equal( [ 1, 2 ] );
  190. expect( charR.getPath() ).to.deep.equal( [ 1, 3 ] );
  191. expect( three.getPath() ).to.deep.equal( [ 2 ] );
  192. } );
  193. } );
  194. describe( 'getDocument()', () => {
  195. it( 'should return null if any parent has not set Document', () => {
  196. expect( charA.document ).to.be.null;
  197. } );
  198. it( 'should return Document attached to the parent element', () => {
  199. const docMock = createDocumentMock();
  200. const parent = new RootEditableElement( 'div' );
  201. parent._document = docMock;
  202. const child = new Element( 'p' );
  203. child.parent = parent;
  204. expect( parent.document ).to.equal( docMock );
  205. expect( child.document ).to.equal( docMock );
  206. } );
  207. it( 'should return null if element is inside DocumentFragment', () => {
  208. const child = new Element( 'p' );
  209. new DocumentFragment( [ child ] ); // eslint-disable-line no-new
  210. expect( child.document ).to.be.null;
  211. } );
  212. } );
  213. describe( 'getRoot()', () => {
  214. it( 'should return this element if it has no parent', () => {
  215. const child = new Element( 'p' );
  216. expect( child.root ).to.equal( child );
  217. } );
  218. it( 'should return root element', () => {
  219. const parent = new RootEditableElement( 'div' );
  220. parent._document = createDocumentMock();
  221. const child = new Element( 'p' );
  222. child.parent = parent;
  223. expect( parent.root ).to.equal( parent );
  224. expect( child.root ).to.equal( parent );
  225. } );
  226. } );
  227. describe( '_remove()', () => {
  228. it( 'should remove node from its parent', () => {
  229. const char = new Text( 'a' );
  230. const parent = new Element( 'p', null, [ char ] );
  231. char._remove();
  232. expect( parent.getChildIndex( char ) ).to.equal( -1 );
  233. } );
  234. it( 'uses parent._removeChildren method', () => {
  235. const char = new Text( 'a' );
  236. const parent = new Element( 'p', null, [ char ] );
  237. const _removeChildrenSpy = sinon.spy( parent, '_removeChildren' );
  238. const index = char.index;
  239. char._remove();
  240. _removeChildrenSpy.restore();
  241. sinon.assert.calledOnce( _removeChildrenSpy );
  242. sinon.assert.calledWithExactly( _removeChildrenSpy, index );
  243. } );
  244. } );
  245. describe( 'toJSON()', () => {
  246. it( 'should prevent circular reference when stringifying a node', () => {
  247. const char = new Text( 'a' );
  248. const parent = new Element( 'p', null );
  249. parent._appendChildren( char );
  250. const json = JSON.stringify( char );
  251. const parsed = JSON.parse( json );
  252. expect( parsed ).to.deep.equal( {
  253. _textData: 'a'
  254. } );
  255. } );
  256. } );
  257. describe( 'change event', () => {
  258. let root, text, img, rootChangeSpy;
  259. before( () => {
  260. rootChangeSpy = sinon.spy();
  261. } );
  262. beforeEach( () => {
  263. text = new Text( 'foo' );
  264. img = new Element( 'img', { 'src': 'img.png' } );
  265. root = new Element( 'p', { renderer: { markToSync: rootChangeSpy } } );
  266. root._appendChildren( [ text, img ] );
  267. root.on( 'change:children', ( evt, node ) => rootChangeSpy( 'children', node ) );
  268. root.on( 'change:attributes', ( evt, node ) => rootChangeSpy( 'attributes', node ) );
  269. root.on( 'change:text', ( evt, node ) => rootChangeSpy( 'text', node ) );
  270. rootChangeSpy.reset();
  271. } );
  272. it( 'should be fired on the node', () => {
  273. const imgChangeSpy = sinon.spy();
  274. img.on( 'change:attributes', ( evt, node ) => {
  275. imgChangeSpy( 'attributes', node );
  276. } );
  277. img._setAttribute( 'width', 100 );
  278. sinon.assert.calledOnce( imgChangeSpy );
  279. sinon.assert.calledWith( imgChangeSpy, 'attributes', img );
  280. } );
  281. it( 'should be fired on the parent', () => {
  282. img._setAttribute( 'width', 100 );
  283. sinon.assert.calledOnce( rootChangeSpy );
  284. sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
  285. } );
  286. describe( '_setAttribute()', () => {
  287. it( 'should fire change event', () => {
  288. img._setAttribute( 'width', 100 );
  289. sinon.assert.calledOnce( rootChangeSpy );
  290. sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
  291. } );
  292. } );
  293. describe( '_removeAttribute()', () => {
  294. it( 'should fire change event', () => {
  295. img._removeAttribute( 'src' );
  296. sinon.assert.calledOnce( rootChangeSpy );
  297. sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
  298. } );
  299. } );
  300. describe( '_insertChildren()', () => {
  301. it( 'should fire change event', () => {
  302. root._insertChildren( 1, new Element( 'img' ) );
  303. sinon.assert.calledOnce( rootChangeSpy );
  304. sinon.assert.calledWith( rootChangeSpy, 'children', root );
  305. } );
  306. } );
  307. describe( '_appendChildren()', () => {
  308. it( 'should fire change event', () => {
  309. root._appendChildren( new Element( 'img' ) );
  310. sinon.assert.calledOnce( rootChangeSpy );
  311. sinon.assert.calledWith( rootChangeSpy, 'children', root );
  312. } );
  313. } );
  314. describe( '_removeChildren()', () => {
  315. it( 'should fire change event', () => {
  316. root._removeChildren( 1, 1 );
  317. sinon.assert.calledOnce( rootChangeSpy );
  318. sinon.assert.calledWith( rootChangeSpy, 'children', root );
  319. } );
  320. } );
  321. describe( 'setText', () => {
  322. it( 'should fire change event', () => {
  323. text._data = 'bar';
  324. sinon.assert.calledOnce( rootChangeSpy );
  325. sinon.assert.calledWith( rootChangeSpy, 'text', text );
  326. } );
  327. } );
  328. } );
  329. } );