node.js 8.8 KB

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