node.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 DocumentFragment from '../../src/view/documentfragment';
  8. import RootEditableElement from '../../src/view/rooteditableelement';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. import createDocumentMock from '../../tests/view/_utils/createdocumentmock';
  11. describe( 'Node', () => {
  12. let root,
  13. one, two, three,
  14. 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( 'getCommonAncestor()', () => {
  92. it( 'should return the parent element for the same node', () => {
  93. expect( img.getCommonAncestor( img ) ).to.equal( two );
  94. } );
  95. it( 'should return null for detached subtrees', () => {
  96. const detached = new Element( 'foo' );
  97. expect( img.getCommonAncestor( detached ) ).to.be.null;
  98. expect( detached.getCommonAncestor( img ) ).to.be.null;
  99. } );
  100. it( 'should return null when one of the nodes is a tree root itself', () => {
  101. expect( root.getCommonAncestor( img ) ).to.be.null;
  102. expect( img.getCommonAncestor( root ) ).to.be.null;
  103. expect( root.getCommonAncestor( root ) ).to.be.null;
  104. } );
  105. it( 'should return parent of the nodes at the same level', () => {
  106. expect( img.getCommonAncestor( charA ) ).to.equal( two );
  107. expect( charB.getCommonAncestor( charA ) ).to.equal( two );
  108. } );
  109. it( 'should return proper element for nodes in different branches and on different levels', () => {
  110. const foo = new Text( 'foo' );
  111. const bar = new Text( 'bar' );
  112. const bom = new Text( 'bom' );
  113. const d = new Element( 'd', null, [ bar ] );
  114. const c = new Element( 'c', null, [ foo, d ] );
  115. const b = new Element( 'b', null, [ c ] );
  116. const e = new Element( 'e', null, [ bom ] );
  117. const a = new Element( 'a', null, [ b, e ] );
  118. // <a><b><c>foo<d>bar</d></c></b><e>bom</e></a>
  119. expect( bar.getCommonAncestor( foo ), 1 ).to.equal( c );
  120. expect( foo.getCommonAncestor( d ), 2 ).to.equal( c );
  121. expect( c.getCommonAncestor( b ), 3 ).to.equal( a );
  122. expect( bom.getCommonAncestor( d ), 4 ).to.equal( a );
  123. expect( b.getCommonAncestor( bom ), 5 ).to.equal( a );
  124. } );
  125. it( 'should return document fragment', () => {
  126. const foo = new Text( 'foo' );
  127. const bar = new Text( 'bar' );
  128. const df = new DocumentFragment( [ foo, bar ] );
  129. expect( foo.getCommonAncestor( bar ) ).to.equal( df );
  130. } );
  131. } );
  132. describe( 'getIndex()', () => {
  133. it( 'should return null if the parent is null', () => {
  134. expect( root.index ).to.be.null;
  135. } );
  136. it( 'should return index in the parent', () => {
  137. expect( one.index ).to.equal( 0 );
  138. expect( two.index ).to.equal( 1 );
  139. expect( three.index ).to.equal( 2 );
  140. expect( charB.index ).to.equal( 0 );
  141. expect( charA.index ).to.equal( 1 );
  142. expect( img.index ).to.equal( 2 );
  143. expect( charR.index ).to.equal( 3 );
  144. } );
  145. it( 'should throw an error if parent does not contain element', () => {
  146. const f = new Text( 'f' );
  147. const bar = new Element( 'bar', [], [] );
  148. f.parent = bar;
  149. expect(
  150. () => {
  151. f.index;
  152. }
  153. ).to.throw( CKEditorError, /view-node-not-found-in-parent/ );
  154. } );
  155. } );
  156. describe( 'getDocument()', () => {
  157. it( 'should return null if any parent has not set Document', () => {
  158. expect( charA.document ).to.be.null;
  159. } );
  160. it( 'should return Document attached to the parent element', () => {
  161. const docMock = createDocumentMock();
  162. const parent = new RootEditableElement( 'div' );
  163. parent.document = docMock;
  164. const child = new Element( 'p' );
  165. child.parent = parent;
  166. expect( parent.document ).to.equal( docMock );
  167. expect( child.document ).to.equal( docMock );
  168. } );
  169. it( 'should return null if element is inside DocumentFragment', () => {
  170. const child = new Element( 'p' );
  171. new DocumentFragment( [ child ] ); // eslint-disable-line no-new
  172. expect( child.document ).to.be.null;
  173. } );
  174. } );
  175. describe( 'getRoot()', () => {
  176. it( 'should return this element if it has no parent', () => {
  177. const child = new Element( 'p' );
  178. expect( child.root ).to.equal( child );
  179. } );
  180. it( 'should return root element', () => {
  181. const parent = new RootEditableElement( 'div' );
  182. parent.document = createDocumentMock();
  183. const child = new Element( 'p' );
  184. child.parent = parent;
  185. expect( parent.root ).to.equal( parent );
  186. expect( child.root ).to.equal( parent );
  187. } );
  188. } );
  189. describe( 'remove()', () => {
  190. it( 'should remove node from its parent', () => {
  191. const char = new Text( 'a' );
  192. const parent = new Element( 'p', null, [ char ] );
  193. char.remove();
  194. expect( parent.getChildIndex( char ) ).to.equal( -1 );
  195. } );
  196. it( 'uses parent.removeChildren method', () => {
  197. const char = new Text( 'a' );
  198. const parent = new Element( 'p', null, [ char ] );
  199. const removeChildrenSpy = sinon.spy( parent, 'removeChildren' );
  200. const index = char.index;
  201. char.remove();
  202. removeChildrenSpy.restore();
  203. sinon.assert.calledOnce( removeChildrenSpy );
  204. sinon.assert.calledWithExactly( removeChildrenSpy, index );
  205. } );
  206. } );
  207. describe( 'change event', () => {
  208. let root, text, img, rootChangeSpy;
  209. before( () => {
  210. rootChangeSpy = sinon.spy();
  211. } );
  212. beforeEach( () => {
  213. text = new Text( 'foo' );
  214. img = new Element( 'img' );
  215. img.setAttribute( 'src', 'img.png' );
  216. root = new Element( 'p', { renderer: { markToSync: rootChangeSpy } } );
  217. root.appendChildren( [ text, img ] );
  218. root.on( 'change:children', ( evt, node ) => rootChangeSpy( 'children', node ) );
  219. root.on( 'change:attributes', ( evt, node ) => rootChangeSpy( 'attributes', node ) );
  220. root.on( 'change:text', ( evt, node ) => rootChangeSpy( 'text', node ) );
  221. rootChangeSpy.reset();
  222. } );
  223. it( 'should be fired on the node', () => {
  224. const imgChangeSpy = sinon.spy();
  225. img.on( 'change:attributes', ( evt, node ) => {
  226. imgChangeSpy( 'attributes', node );
  227. } );
  228. img.setAttribute( 'width', 100 );
  229. sinon.assert.calledOnce( imgChangeSpy );
  230. sinon.assert.calledWith( imgChangeSpy, 'attributes', img );
  231. } );
  232. it( 'should be fired on the parent', () => {
  233. img.setAttribute( 'width', 100 );
  234. sinon.assert.calledOnce( rootChangeSpy );
  235. sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
  236. } );
  237. describe( 'setAttribute()', () => {
  238. it( 'should fire change event', () => {
  239. img.setAttribute( 'width', 100 );
  240. sinon.assert.calledOnce( rootChangeSpy );
  241. sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
  242. } );
  243. } );
  244. describe( 'removeAttribute()', () => {
  245. it( 'should fire change event', () => {
  246. img.removeAttribute( 'src' );
  247. sinon.assert.calledOnce( rootChangeSpy );
  248. sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
  249. } );
  250. } );
  251. describe( 'insertChildren()', () => {
  252. it( 'should fire change event', () => {
  253. root.insertChildren( 1, new Element( 'img' ) );
  254. sinon.assert.calledOnce( rootChangeSpy );
  255. sinon.assert.calledWith( rootChangeSpy, 'children', root );
  256. } );
  257. } );
  258. describe( 'appendChildren()', () => {
  259. it( 'should fire change event', () => {
  260. root.appendChildren( new Element( 'img' ) );
  261. sinon.assert.calledOnce( rootChangeSpy );
  262. sinon.assert.calledWith( rootChangeSpy, 'children', root );
  263. } );
  264. } );
  265. describe( 'removeChildren()', () => {
  266. it( 'should fire change event', () => {
  267. root.removeChildren( 1, 1 );
  268. sinon.assert.calledOnce( rootChangeSpy );
  269. sinon.assert.calledWith( rootChangeSpy, 'children', root );
  270. } );
  271. } );
  272. describe( 'removeChildren()', () => {
  273. it( 'should fire change event', () => {
  274. text.data = 'bar';
  275. sinon.assert.calledOnce( rootChangeSpy );
  276. sinon.assert.calledWith( rootChangeSpy, 'text', text );
  277. } );
  278. } );
  279. } );
  280. } );