8
0

node.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import Element from '/ckeditor5/engine/treeview/element.js';
  8. import Text from '/ckeditor5/engine/treeview/text.js';
  9. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  10. describe( 'Node', () => {
  11. let root;
  12. let one, two, three;
  13. let charB, charA, charR, img;
  14. before( () => {
  15. charB = new Text( 'b' );
  16. charA = new Text( 'a' );
  17. img = new Element( 'img' );
  18. charR = new Text( 'r' );
  19. one = new Element( 'one' );
  20. two = new Element( 'two', null, [ charB, charA, img, charR ] );
  21. three = new Element( 'three' );
  22. root = new Element( null, null, [ one, two, three ] );
  23. } );
  24. describe( 'getNextSibling/getPreviousSibling', () => {
  25. it( 'should return next sibling', () => {
  26. expect( root.getNextSibling() ).to.be.null;
  27. expect( one.getNextSibling() ).to.equal( two );
  28. expect( two.getNextSibling() ).to.equal( three );
  29. expect( three.getNextSibling() ).to.be.null;
  30. expect( charB.getNextSibling() ).to.equal( charA );
  31. expect( charA.getNextSibling() ).to.equal( img );
  32. expect( img.getNextSibling() ).to.equal( charR );
  33. expect( charR.getNextSibling() ).to.be.null;
  34. } );
  35. it( 'should return previous sibling', () => {
  36. expect( root.getPreviousSibling() ).to.be.null;
  37. expect( one.getPreviousSibling() ).to.be.null;
  38. expect( two.getPreviousSibling() ).to.equal( one );
  39. expect( three.getPreviousSibling() ).to.equal( two );
  40. expect( charB.getPreviousSibling() ).to.be.null;
  41. expect( charA.getPreviousSibling() ).to.equal( charB );
  42. expect( img.getPreviousSibling() ).to.equal( charA );
  43. expect( charR.getPreviousSibling() ).to.equal( img );
  44. } );
  45. } );
  46. describe( 'getAncestors', () => {
  47. it( 'should return empty array for node without ancestors', () => {
  48. const result = root.getAncestors();
  49. expect( result ).to.be.an( 'array' );
  50. expect( result.length ).to.equal( 0 );
  51. } );
  52. it( 'should return array including node itself if requested', () => {
  53. const result = root.getAncestors( { includeNode: true } );
  54. expect( result ).to.be.an( 'array' );
  55. expect( result.length ).to.equal( 1 );
  56. expect( result[ 0 ] ).to.equal( root );
  57. } );
  58. it( 'should return array of ancestors', () => {
  59. const result = charR.getAncestors();
  60. expect( result.length ).to.equal( 2 );
  61. expect( result[ 0 ] ).to.equal( root );
  62. expect( result[ 1 ] ).to.equal( two );
  63. const result2 = charR.getAncestors( { includeNode: true } );
  64. expect( result2.length ).to.equal( 3 );
  65. expect( result2[ 0 ] ).to.equal( root );
  66. expect( result2[ 1 ] ).to.equal( two );
  67. expect( result2[ 2 ] ).to.equal( charR );
  68. } );
  69. it( 'should return array of ancestors starting from parent', () => {
  70. const result = charR.getAncestors( { parentFirst: true } );
  71. expect( result.length ).to.equal( 2 );
  72. expect( result[ 0 ] ).to.equal( two );
  73. expect( result[ 1 ] ).to.equal( root );
  74. const result2 = charR.getAncestors( { includeNode: true, parentFirst: true } );
  75. expect( result2.length ).to.equal( 3 );
  76. expect( result2[ 2 ] ).to.equal( root );
  77. expect( result2[ 1 ] ).to.equal( two );
  78. expect( result2[ 0 ] ).to.equal( charR );
  79. } );
  80. } );
  81. describe( 'getIndex', () => {
  82. it( 'should return null if the parent is null', () => {
  83. expect( root.getIndex() ).to.be.null;
  84. } );
  85. it( 'should return index in the parent', () => {
  86. expect( one.getIndex() ).to.equal( 0 );
  87. expect( two.getIndex() ).to.equal( 1 );
  88. expect( three.getIndex() ).to.equal( 2 );
  89. expect( charB.getIndex() ).to.equal( 0 );
  90. expect( charA.getIndex() ).to.equal( 1 );
  91. expect( img.getIndex() ).to.equal( 2 );
  92. expect( charR.getIndex() ).to.equal( 3 );
  93. } );
  94. it( 'should throw an error if parent does not contain element', () => {
  95. let f = new Text( 'f' );
  96. let bar = new Element( 'bar', [], [] );
  97. f.parent = bar;
  98. expect(
  99. () => {
  100. f.getIndex();
  101. }
  102. ).to.throw( CKEditorError, /treeview-node-not-found-in-parent/ );
  103. } );
  104. } );
  105. describe( 'getDocument', () => {
  106. it( 'should return null if any parent has not set treeview', () => {
  107. expect( charA.getDocument() ).to.be.null;
  108. } );
  109. it( 'should return TreeView attached to the element', () => {
  110. const tvMock = {};
  111. const element = new Element( 'p' );
  112. element.setDocument( tvMock );
  113. expect( element.getDocument() ).to.equal( tvMock );
  114. } );
  115. it( 'should return Document attached to the parent element', () => {
  116. const docMock = {};
  117. const parent = new Element( 'div' );
  118. const child = new Element( 'p' );
  119. child.parent = parent;
  120. parent.setDocument( docMock );
  121. expect( parent.getDocument() ).to.equal( docMock );
  122. expect( child.getDocument() ).to.equal( docMock );
  123. } );
  124. } );
  125. describe( 'remove', () => {
  126. it( 'should remove node from its parent', () => {
  127. const char = new Text( 'a' );
  128. const parent = new Element( 'p', null, [ char ] );
  129. char.remove();
  130. expect( parent.getChildIndex( char ) ).to.equal( -1 );
  131. } );
  132. it( 'uses parent.removeChildren method', () => {
  133. const char = new Text( 'a' );
  134. const parent = new Element( 'p', null, [ char ] );
  135. const removeChildrenSpy = sinon.spy( parent, 'removeChildren' );
  136. const index = char.getIndex();
  137. char.remove();
  138. removeChildrenSpy.restore();
  139. sinon.assert.calledOnce( removeChildrenSpy );
  140. sinon.assert.calledWithExactly( removeChildrenSpy, index );
  141. } );
  142. } );
  143. describe( 'change event', () => {
  144. let root, text, img;
  145. let rootChangeSpy;
  146. before( () => {
  147. rootChangeSpy = sinon.spy();
  148. } );
  149. beforeEach( () => {
  150. text = new Text( 'foo' );
  151. img = new Element( 'img' );
  152. img.setAttribute( 'src', 'img.png' );
  153. root = new Element( 'p', { renderer: { markToSync: rootChangeSpy } } );
  154. root.appendChildren( [ text, img ] );
  155. root.on( 'change', ( evt, type, node ) => {
  156. rootChangeSpy( type, node );
  157. } );
  158. rootChangeSpy.reset();
  159. } );
  160. it( 'should be fired on the node', () => {
  161. const imgChangeSpy = sinon.spy();
  162. img.on( 'change', ( evt, type, node ) => {
  163. imgChangeSpy( type, node );
  164. } );
  165. img.setAttribute( 'width', 100 );
  166. sinon.assert.calledOnce( imgChangeSpy );
  167. sinon.assert.calledWith( imgChangeSpy, 'ATTRIBUTES', img );
  168. } );
  169. it( 'should be fired on the parent', () => {
  170. img.setAttribute( 'width', 100 );
  171. sinon.assert.calledOnce( rootChangeSpy );
  172. sinon.assert.calledWith( rootChangeSpy, 'ATTRIBUTES', img );
  173. } );
  174. describe( 'setAttr', () => {
  175. it( 'should fire change event', () => {
  176. img.setAttribute( 'width', 100 );
  177. sinon.assert.calledOnce( rootChangeSpy );
  178. sinon.assert.calledWith( rootChangeSpy, 'ATTRIBUTES', img );
  179. } );
  180. } );
  181. describe( 'removeAttr', () => {
  182. it( 'should fire change event', () => {
  183. img.removeAttribute( 'src' );
  184. sinon.assert.calledOnce( rootChangeSpy );
  185. sinon.assert.calledWith( rootChangeSpy, 'ATTRIBUTES', img );
  186. } );
  187. } );
  188. describe( 'insertChildren', () => {
  189. it( 'should fire change event', () => {
  190. root.insertChildren( 1, new Element( 'img' ) );
  191. sinon.assert.calledOnce( rootChangeSpy );
  192. sinon.assert.calledWith( rootChangeSpy, 'CHILDREN', root );
  193. } );
  194. } );
  195. describe( 'appendChildren', () => {
  196. it( 'should fire change event', () => {
  197. root.appendChildren( new Element( 'img' ) );
  198. sinon.assert.calledOnce( rootChangeSpy );
  199. sinon.assert.calledWith( rootChangeSpy, 'CHILDREN', root );
  200. } );
  201. } );
  202. describe( 'removeChildren', () => {
  203. it( 'should fire change event', () => {
  204. root.removeChildren( 1, 1 );
  205. sinon.assert.calledOnce( rootChangeSpy );
  206. sinon.assert.calledWith( rootChangeSpy, 'CHILDREN', root );
  207. } );
  208. } );
  209. describe( 'removeChildren', () => {
  210. it( 'should fire change event', () => {
  211. text.data = 'bar';
  212. sinon.assert.calledOnce( rootChangeSpy );
  213. sinon.assert.calledWith( rootChangeSpy, 'TEXT', text );
  214. } );
  215. } );
  216. } );
  217. } );