node.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. 'use strict';
  7. import Element from '/ckeditor5/engine/view/element.js';
  8. import Text from '/ckeditor5/engine/view/text.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.getNextSibling() ).to.be.null;
  29. expect( one.getNextSibling() ).to.equal( two );
  30. expect( two.getNextSibling() ).to.equal( three );
  31. expect( three.getNextSibling() ).to.be.null;
  32. expect( charB.getNextSibling() ).to.equal( charA );
  33. expect( charA.getNextSibling() ).to.equal( img );
  34. expect( img.getNextSibling() ).to.equal( charR );
  35. expect( charR.getNextSibling() ).to.be.null;
  36. } );
  37. it( 'should return previous sibling', () => {
  38. expect( root.getPreviousSibling() ).to.be.null;
  39. expect( one.getPreviousSibling() ).to.be.null;
  40. expect( two.getPreviousSibling() ).to.equal( one );
  41. expect( three.getPreviousSibling() ).to.equal( two );
  42. expect( charB.getPreviousSibling() ).to.be.null;
  43. expect( charA.getPreviousSibling() ).to.equal( charB );
  44. expect( img.getPreviousSibling() ).to.equal( charA );
  45. expect( charR.getPreviousSibling() ).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. } );
  83. describe( 'getIndex', () => {
  84. it( 'should return null if the parent is null', () => {
  85. expect( root.getIndex() ).to.be.null;
  86. } );
  87. it( 'should return index in the parent', () => {
  88. expect( one.getIndex() ).to.equal( 0 );
  89. expect( two.getIndex() ).to.equal( 1 );
  90. expect( three.getIndex() ).to.equal( 2 );
  91. expect( charB.getIndex() ).to.equal( 0 );
  92. expect( charA.getIndex() ).to.equal( 1 );
  93. expect( img.getIndex() ).to.equal( 2 );
  94. expect( charR.getIndex() ).to.equal( 3 );
  95. } );
  96. it( 'should throw an error if parent does not contain element', () => {
  97. let f = new Text( 'f' );
  98. let bar = new Element( 'bar', [], [] );
  99. f.parent = bar;
  100. expect(
  101. () => {
  102. f.getIndex();
  103. }
  104. ).to.throw( CKEditorError, /view-node-not-found-in-parent/ );
  105. } );
  106. } );
  107. describe( 'getDocument', () => {
  108. it( 'should return null if any parent has not set Document', () => {
  109. expect( charA.getDocument() ).to.be.null;
  110. } );
  111. it( 'should return Document attached to the parent element', () => {
  112. const docMock = createDocumentMock();
  113. const parent = new RootEditableElement( docMock, 'div' );
  114. const child = new Element( 'p' );
  115. child.parent = parent;
  116. expect( parent.getDocument() ).to.equal( docMock );
  117. expect( child.getDocument() ).to.equal( docMock );
  118. } );
  119. } );
  120. describe( 'getRoot', () => {
  121. it( 'should return this element if it has no parent', () => {
  122. const child = new Element( 'p' );
  123. expect( child.getRoot() ).to.equal( child );
  124. } );
  125. it( 'should return root element', () => {
  126. const parent = new RootEditableElement( createDocumentMock(), 'div' );
  127. const child = new Element( 'p' );
  128. child.parent = parent;
  129. expect( parent.getRoot() ).to.equal( parent );
  130. expect( child.getRoot() ).to.equal( parent );
  131. } );
  132. } );
  133. describe( 'remove', () => {
  134. it( 'should remove node from its parent', () => {
  135. const char = new Text( 'a' );
  136. const parent = new Element( 'p', null, [ char ] );
  137. char.remove();
  138. expect( parent.getChildIndex( char ) ).to.equal( -1 );
  139. } );
  140. it( 'uses parent.removeChildren method', () => {
  141. const char = new Text( 'a' );
  142. const parent = new Element( 'p', null, [ char ] );
  143. const removeChildrenSpy = sinon.spy( parent, 'removeChildren' );
  144. const index = char.getIndex();
  145. char.remove();
  146. removeChildrenSpy.restore();
  147. sinon.assert.calledOnce( removeChildrenSpy );
  148. sinon.assert.calledWithExactly( removeChildrenSpy, index );
  149. } );
  150. } );
  151. describe( 'change event', () => {
  152. let root, text, img;
  153. let rootChangeSpy;
  154. before( () => {
  155. rootChangeSpy = sinon.spy();
  156. } );
  157. beforeEach( () => {
  158. text = new Text( 'foo' );
  159. img = new Element( 'img' );
  160. img.setAttribute( 'src', 'img.png' );
  161. root = new Element( 'p', { renderer: { markToSync: rootChangeSpy } } );
  162. root.appendChildren( [ text, img ] );
  163. root.on( 'change:children', ( evt, node ) => rootChangeSpy( 'children', node ) );
  164. root.on( 'change:attributes', ( evt, node ) => rootChangeSpy( 'attributes', node ) );
  165. root.on( 'change:text', ( evt, node ) => rootChangeSpy( 'text', node ) );
  166. rootChangeSpy.reset();
  167. } );
  168. it( 'should be fired on the node', () => {
  169. const imgChangeSpy = sinon.spy();
  170. img.on( 'change:attributes', ( evt, node ) => {
  171. imgChangeSpy( 'attributes', node );
  172. } );
  173. img.setAttribute( 'width', 100 );
  174. sinon.assert.calledOnce( imgChangeSpy );
  175. sinon.assert.calledWith( imgChangeSpy, 'attributes', img );
  176. } );
  177. it( 'should be fired on the parent', () => {
  178. img.setAttribute( 'width', 100 );
  179. sinon.assert.calledOnce( rootChangeSpy );
  180. sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
  181. } );
  182. describe( 'setAttr', () => {
  183. it( 'should fire change event', () => {
  184. img.setAttribute( 'width', 100 );
  185. sinon.assert.calledOnce( rootChangeSpy );
  186. sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
  187. } );
  188. } );
  189. describe( 'removeAttr', () => {
  190. it( 'should fire change event', () => {
  191. img.removeAttribute( 'src' );
  192. sinon.assert.calledOnce( rootChangeSpy );
  193. sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
  194. } );
  195. } );
  196. describe( 'insertChildren', () => {
  197. it( 'should fire change event', () => {
  198. root.insertChildren( 1, new Element( 'img' ) );
  199. sinon.assert.calledOnce( rootChangeSpy );
  200. sinon.assert.calledWith( rootChangeSpy, 'children', root );
  201. } );
  202. } );
  203. describe( 'appendChildren', () => {
  204. it( 'should fire change event', () => {
  205. root.appendChildren( new Element( 'img' ) );
  206. sinon.assert.calledOnce( rootChangeSpy );
  207. sinon.assert.calledWith( rootChangeSpy, 'children', root );
  208. } );
  209. } );
  210. describe( 'removeChildren', () => {
  211. it( 'should fire change event', () => {
  212. root.removeChildren( 1, 1 );
  213. sinon.assert.calledOnce( rootChangeSpy );
  214. sinon.assert.calledWith( rootChangeSpy, 'children', root );
  215. } );
  216. } );
  217. describe( 'removeChildren', () => {
  218. it( 'should fire change event', () => {
  219. text.data = 'bar';
  220. sinon.assert.calledOnce( rootChangeSpy );
  221. sinon.assert.calledWith( rootChangeSpy, 'text', text );
  222. } );
  223. } );
  224. } );
  225. } );