node.js 12 KB

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