node.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /**
  2. * @license Copyright (c) 2003-2018, 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 Node from '../../src/view/node';
  8. import DocumentFragment from '../../src/view/documentfragment';
  9. import RootEditableElement from '../../src/view/rooteditableelement';
  10. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  11. import createDocumentMock from '../../tests/view/_utils/createdocumentmock';
  12. describe( 'Node', () => {
  13. let root,
  14. one, two, three,
  15. 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( 'is()', () => {
  27. it( 'should return true for node', () => {
  28. const node = new Node();
  29. expect( node.is( 'node' ) ).to.be.true;
  30. } );
  31. } );
  32. describe( 'getNextSibling/getPreviousSibling()', () => {
  33. it( 'should return next sibling', () => {
  34. expect( root.nextSibling ).to.be.null;
  35. expect( one.nextSibling ).to.equal( two );
  36. expect( two.nextSibling ).to.equal( three );
  37. expect( three.nextSibling ).to.be.null;
  38. expect( charB.nextSibling ).to.equal( charA );
  39. expect( charA.nextSibling ).to.equal( img );
  40. expect( img.nextSibling ).to.equal( charR );
  41. expect( charR.nextSibling ).to.be.null;
  42. } );
  43. it( 'should return previous sibling', () => {
  44. expect( root.previousSibling ).to.be.null;
  45. expect( one.previousSibling ).to.be.null;
  46. expect( two.previousSibling ).to.equal( one );
  47. expect( three.previousSibling ).to.equal( two );
  48. expect( charB.previousSibling ).to.be.null;
  49. expect( charA.previousSibling ).to.equal( charB );
  50. expect( img.previousSibling ).to.equal( charA );
  51. expect( charR.previousSibling ).to.equal( img );
  52. } );
  53. } );
  54. describe( 'getAncestors()', () => {
  55. it( 'should return empty array for node without ancestors', () => {
  56. const result = root.getAncestors();
  57. expect( result ).to.be.an( 'array' );
  58. expect( result.length ).to.equal( 0 );
  59. } );
  60. it( 'should return array including node itself if requested', () => {
  61. const result = root.getAncestors( { includeSelf: true } );
  62. expect( result ).to.be.an( 'array' );
  63. expect( result.length ).to.equal( 1 );
  64. expect( result[ 0 ] ).to.equal( root );
  65. } );
  66. it( 'should return array of ancestors', () => {
  67. const result = charR.getAncestors();
  68. expect( result.length ).to.equal( 2 );
  69. expect( result[ 0 ] ).to.equal( root );
  70. expect( result[ 1 ] ).to.equal( two );
  71. const result2 = charR.getAncestors( { includeSelf: true } );
  72. expect( result2.length ).to.equal( 3 );
  73. expect( result2[ 0 ] ).to.equal( root );
  74. expect( result2[ 1 ] ).to.equal( two );
  75. expect( result2[ 2 ] ).to.equal( charR );
  76. } );
  77. it( 'should return array of ancestors starting from parent', () => {
  78. const result = charR.getAncestors( { parentFirst: true } );
  79. expect( result.length ).to.equal( 2 );
  80. expect( result[ 0 ] ).to.equal( two );
  81. expect( result[ 1 ] ).to.equal( root );
  82. const result2 = charR.getAncestors( { includeSelf: true, parentFirst: true } );
  83. expect( result2.length ).to.equal( 3 );
  84. expect( result2[ 2 ] ).to.equal( root );
  85. expect( result2[ 1 ] ).to.equal( two );
  86. expect( result2[ 0 ] ).to.equal( charR );
  87. } );
  88. it( 'should return ancestors including DocumentFragment', () => {
  89. const fragment = new DocumentFragment( root );
  90. const result = img.getAncestors();
  91. root._remove();
  92. expect( result.length ).to.equal( 3 );
  93. expect( result[ 0 ] ).to.equal( fragment );
  94. expect( result[ 1 ] ).to.equal( root );
  95. expect( result[ 2 ] ).to.equal( two );
  96. } );
  97. } );
  98. describe( 'getCommonAncestor()', () => {
  99. it( 'should return the parent element for the same node', () => {
  100. expect( img.getCommonAncestor( img ) ).to.equal( two );
  101. } );
  102. it( 'should return the given node for the same node if includeSelf is used', () => {
  103. expect( img.getCommonAncestor( img, { includeSelf: true } ) ).to.equal( img );
  104. } );
  105. it( 'should return null for detached subtrees', () => {
  106. const detached = new Element( 'foo' );
  107. expect( img.getCommonAncestor( detached ) ).to.be.null;
  108. expect( detached.getCommonAncestor( img ) ).to.be.null;
  109. expect( img.getCommonAncestor( detached, { includeSelf: true } ) ).to.be.null;
  110. expect( detached.getCommonAncestor( img, { includeSelf: true } ) ).to.be.null;
  111. } );
  112. it( 'should return null when one of the nodes is a tree root itself', () => {
  113. expect( root.getCommonAncestor( img ) ).to.be.null;
  114. expect( img.getCommonAncestor( root ) ).to.be.null;
  115. expect( root.getCommonAncestor( root ) ).to.be.null;
  116. } );
  117. it( 'should return root when one of the nodes is a tree root itself and includeSelf is used', () => {
  118. expect( root.getCommonAncestor( img, { includeSelf: true } ) ).to.equal( root );
  119. expect( img.getCommonAncestor( root, { includeSelf: true } ) ).to.equal( root );
  120. expect( root.getCommonAncestor( root, { includeSelf: true } ) ).to.equal( root );
  121. } );
  122. it( 'should return parent of the nodes at the same level', () => {
  123. expect( img.getCommonAncestor( charA ), 1 ).to.equal( two );
  124. expect( charB.getCommonAncestor( charA ), 2 ).to.equal( two );
  125. expect( img.getCommonAncestor( charA, { includeSelf: true } ), 3 ).to.equal( two );
  126. expect( charB.getCommonAncestor( charA, { includeSelf: true } ), 4 ).to.equal( two );
  127. } );
  128. it( 'should return proper element for nodes in different branches and on different levels', () => {
  129. const foo = new Text( 'foo' );
  130. const bar = new Text( 'bar' );
  131. const bom = new Text( 'bom' );
  132. const d = new Element( 'd', null, [ bar ] );
  133. const c = new Element( 'c', null, [ foo, d ] );
  134. const b = new Element( 'b', null, [ c ] );
  135. const e = new Element( 'e', null, [ bom ] );
  136. const a = new Element( 'a', null, [ b, e ] );
  137. // <a><b><c>foo<d>bar</d></c></b><e>bom</e></a>
  138. expect( bar.getCommonAncestor( foo ), 1 ).to.equal( c );
  139. expect( foo.getCommonAncestor( d ), 2 ).to.equal( c );
  140. expect( c.getCommonAncestor( b ), 3 ).to.equal( a );
  141. expect( bom.getCommonAncestor( d ), 4 ).to.equal( a );
  142. expect( b.getCommonAncestor( bom ), 5 ).to.equal( a );
  143. expect( b.getCommonAncestor( bar ), 6 ).to.equal( a );
  144. expect( bar.getCommonAncestor( foo, { includeSelf: true } ), 11 ).to.equal( c );
  145. expect( foo.getCommonAncestor( d, { includeSelf: true } ), 12 ).to.equal( c );
  146. expect( c.getCommonAncestor( b, { includeSelf: true } ), 13 ).to.equal( b );
  147. expect( bom.getCommonAncestor( d, { includeSelf: true } ), 14 ).to.equal( a );
  148. expect( b.getCommonAncestor( bom, { includeSelf: true } ), 15 ).to.equal( a );
  149. expect( b.getCommonAncestor( bar, { includeSelf: true } ), 16 ).to.equal( b );
  150. } );
  151. it( 'should return document fragment', () => {
  152. const foo = new Text( 'foo' );
  153. const bar = new Text( 'bar' );
  154. const df = new DocumentFragment( [ foo, bar ] );
  155. expect( foo.getCommonAncestor( bar ) ).to.equal( df );
  156. } );
  157. } );
  158. describe( 'getIndex()', () => {
  159. it( 'should return null if the parent is null', () => {
  160. expect( root.index ).to.be.null;
  161. } );
  162. it( 'should return index in the parent', () => {
  163. expect( one.index ).to.equal( 0 );
  164. expect( two.index ).to.equal( 1 );
  165. expect( three.index ).to.equal( 2 );
  166. expect( charB.index ).to.equal( 0 );
  167. expect( charA.index ).to.equal( 1 );
  168. expect( img.index ).to.equal( 2 );
  169. expect( charR.index ).to.equal( 3 );
  170. } );
  171. it( 'should throw an error if parent does not contain element', () => {
  172. const f = new Text( 'f' );
  173. const bar = new Element( 'bar', [], [] );
  174. f.parent = bar;
  175. expect(
  176. () => {
  177. f.index;
  178. }
  179. ).to.throw( CKEditorError, /view-node-not-found-in-parent/ );
  180. } );
  181. } );
  182. describe( 'getPath()', () => {
  183. it( 'should return empty array is the element is the root', () => {
  184. expect( root.getPath() ).to.deep.equal( [] );
  185. } );
  186. it( 'should return array with indices of given element and its ancestors starting from top-most one', () => {
  187. expect( one.getPath() ).to.deep.equal( [ 0 ] );
  188. expect( two.getPath() ).to.deep.equal( [ 1 ] );
  189. expect( img.getPath() ).to.deep.equal( [ 1, 2 ] );
  190. expect( charR.getPath() ).to.deep.equal( [ 1, 3 ] );
  191. expect( three.getPath() ).to.deep.equal( [ 2 ] );
  192. } );
  193. } );
  194. describe( 'getDocument()', () => {
  195. it( 'should return null if any parent has not set Document', () => {
  196. expect( charA.document ).to.be.null;
  197. } );
  198. it( 'should return Document attached to the parent element', () => {
  199. const docMock = createDocumentMock();
  200. const parent = new RootEditableElement( 'div' );
  201. parent._document = docMock;
  202. const child = new Element( 'p' );
  203. child.parent = parent;
  204. expect( parent.document ).to.equal( docMock );
  205. expect( child.document ).to.equal( docMock );
  206. } );
  207. it( 'should return null if element is inside DocumentFragment', () => {
  208. const child = new Element( 'p' );
  209. new DocumentFragment( [ child ] ); // eslint-disable-line no-new
  210. expect( child.document ).to.be.null;
  211. } );
  212. } );
  213. describe( 'getRoot()', () => {
  214. it( 'should return this element if it has no parent', () => {
  215. const child = new Element( 'p' );
  216. expect( child.root ).to.equal( child );
  217. } );
  218. it( 'should return root element', () => {
  219. const parent = new RootEditableElement( 'div' );
  220. parent._document = createDocumentMock();
  221. const child = new Element( 'p' );
  222. child.parent = parent;
  223. expect( parent.root ).to.equal( parent );
  224. expect( child.root ).to.equal( parent );
  225. } );
  226. } );
  227. describe( 'isBefore()', () => {
  228. // Model is: <root><one></one><two>ba<img></img>r</two><three></three>
  229. it( 'should return true if the element is before given element', () => {
  230. expect( one.isBefore( two ) ).to.be.true;
  231. expect( one.isBefore( img ) ).to.be.true;
  232. expect( two.isBefore( charB ) ).to.be.true;
  233. expect( two.isBefore( charR ) ).to.be.true;
  234. expect( two.isBefore( three ) ).to.be.true;
  235. expect( root.isBefore( one ) ).to.be.true;
  236. } );
  237. it( 'should return false if the element is after given element', () => {
  238. expect( two.isBefore( one ) ).to.be.false;
  239. expect( img.isBefore( one ) ).to.be.false;
  240. expect( charB.isBefore( two ) ).to.be.false;
  241. expect( charR.isBefore( two ) ).to.be.false;
  242. expect( three.isBefore( two ) ).to.be.false;
  243. expect( one.isBefore( root ) ).to.be.false;
  244. } );
  245. it( 'should return false if the same element is given', () => {
  246. expect( one.isBefore( one ) ).to.be.false;
  247. } );
  248. it( 'should return false if elements are in different roots', () => {
  249. const otherRoot = new Element( 'root' );
  250. const otherElement = new Element( 'element' );
  251. otherRoot._appendChild( otherElement );
  252. expect( otherElement.isBefore( three ) ).to.be.false;
  253. } );
  254. } );
  255. describe( 'isAfter()', () => {
  256. // Model is: <root><one></one><two>ba<img></img>r</two><three></three>
  257. it( 'should return true if the element is after given element', () => {
  258. expect( two.isAfter( one ) ).to.be.true;
  259. expect( img.isAfter( one ) ).to.be.true;
  260. expect( charB.isAfter( two ) ).to.be.true;
  261. expect( charR.isAfter( two ) ).to.be.true;
  262. expect( three.isAfter( two ) ).to.be.true;
  263. expect( one.isAfter( root ) ).to.be.true;
  264. } );
  265. it( 'should return false if the element is before given element', () => {
  266. expect( one.isAfter( two ) ).to.be.false;
  267. expect( one.isAfter( img ) ).to.be.false;
  268. expect( two.isAfter( charB ) ).to.be.false;
  269. expect( two.isAfter( charR ) ).to.be.false;
  270. expect( two.isAfter( three ) ).to.be.false;
  271. expect( root.isAfter( one ) ).to.be.false;
  272. } );
  273. it( 'should return false if the same element is given', () => {
  274. expect( one.isAfter( one ) ).to.be.false;
  275. } );
  276. it( 'should return false if elements are in different roots', () => {
  277. const otherRoot = new Element( 'root' );
  278. const otherElement = new Element( 'element' );
  279. otherRoot._appendChild( otherElement );
  280. expect( three.isAfter( otherElement ) ).to.be.false;
  281. } );
  282. } );
  283. describe( '_remove()', () => {
  284. it( 'should remove node from its parent', () => {
  285. const char = new Text( 'a' );
  286. const parent = new Element( 'p', null, [ char ] );
  287. char._remove();
  288. expect( parent.getChildIndex( char ) ).to.equal( -1 );
  289. } );
  290. it( 'uses parent._removeChildren method', () => {
  291. const char = new Text( 'a' );
  292. const parent = new Element( 'p', null, [ char ] );
  293. const _removeChildrenSpy = sinon.spy( parent, '_removeChildren' );
  294. const index = char.index;
  295. char._remove();
  296. _removeChildrenSpy.restore();
  297. sinon.assert.calledOnce( _removeChildrenSpy );
  298. sinon.assert.calledWithExactly( _removeChildrenSpy, index );
  299. } );
  300. } );
  301. describe( 'toJSON()', () => {
  302. it( 'should prevent circular reference when stringifying a node', () => {
  303. const char = new Text( 'a' );
  304. const parent = new Element( 'p', null );
  305. parent._appendChild( char );
  306. const json = JSON.stringify( char );
  307. const parsed = JSON.parse( json );
  308. expect( parsed ).to.deep.equal( {
  309. _textData: 'a'
  310. } );
  311. } );
  312. } );
  313. describe( 'change event', () => {
  314. let root, text, img, rootChangeSpy;
  315. before( () => {
  316. rootChangeSpy = sinon.spy();
  317. } );
  318. beforeEach( () => {
  319. text = new Text( 'foo' );
  320. img = new Element( 'img', { 'src': 'img.png' } );
  321. root = new Element( 'p', { renderer: { markToSync: rootChangeSpy } } );
  322. root._appendChild( [ text, img ] );
  323. root.on( 'change:children', ( evt, node ) => rootChangeSpy( 'children', node ) );
  324. root.on( 'change:attributes', ( evt, node ) => rootChangeSpy( 'attributes', node ) );
  325. root.on( 'change:text', ( evt, node ) => rootChangeSpy( 'text', node ) );
  326. rootChangeSpy.resetHistory();
  327. } );
  328. it( 'should be fired on the node', () => {
  329. const imgChangeSpy = sinon.spy();
  330. img.on( 'change:attributes', ( evt, node ) => {
  331. imgChangeSpy( 'attributes', node );
  332. } );
  333. img._setAttribute( 'width', 100 );
  334. sinon.assert.calledOnce( imgChangeSpy );
  335. sinon.assert.calledWith( imgChangeSpy, 'attributes', img );
  336. } );
  337. it( 'should be fired on the parent', () => {
  338. img._setAttribute( 'width', 100 );
  339. sinon.assert.calledOnce( rootChangeSpy );
  340. sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
  341. } );
  342. describe( '_setAttribute()', () => {
  343. it( 'should fire change event', () => {
  344. img._setAttribute( 'width', 100 );
  345. sinon.assert.calledOnce( rootChangeSpy );
  346. sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
  347. } );
  348. } );
  349. describe( '_removeAttribute()', () => {
  350. it( 'should fire change event', () => {
  351. img._removeAttribute( 'src' );
  352. sinon.assert.calledOnce( rootChangeSpy );
  353. sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
  354. } );
  355. } );
  356. describe( '_insertChild()', () => {
  357. it( 'should fire change event', () => {
  358. root._insertChild( 1, new Element( 'img' ) );
  359. sinon.assert.calledOnce( rootChangeSpy );
  360. sinon.assert.calledWith( rootChangeSpy, 'children', root );
  361. } );
  362. } );
  363. describe( '_appendChild()', () => {
  364. it( 'should fire change event', () => {
  365. root._appendChild( new Element( 'img' ) );
  366. sinon.assert.calledOnce( rootChangeSpy );
  367. sinon.assert.calledWith( rootChangeSpy, 'children', root );
  368. } );
  369. } );
  370. describe( '_removeChildren()', () => {
  371. it( 'should fire change event', () => {
  372. root._removeChildren( 1, 1 );
  373. sinon.assert.calledOnce( rootChangeSpy );
  374. sinon.assert.calledWith( rootChangeSpy, 'children', root );
  375. } );
  376. } );
  377. describe( 'setText', () => {
  378. it( 'should fire change event', () => {
  379. text._data = 'bar';
  380. sinon.assert.calledOnce( rootChangeSpy );
  381. sinon.assert.calledWith( rootChangeSpy, 'text', text );
  382. } );
  383. } );
  384. } );
  385. } );