node.js 8.9 KB

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