node.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 createDocumentMock from '../../tests/view/_utils/createdocumentmock';
  11. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  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. expectToThrowCKEditorError( () => {
  176. f.index;
  177. }, /view-node-not-found-in-parent/, bar );
  178. } );
  179. } );
  180. describe( 'getPath()', () => {
  181. it( 'should return empty array is the element is the root', () => {
  182. expect( root.getPath() ).to.deep.equal( [] );
  183. } );
  184. it( 'should return array with indices of given element and its ancestors starting from top-most one', () => {
  185. expect( one.getPath() ).to.deep.equal( [ 0 ] );
  186. expect( two.getPath() ).to.deep.equal( [ 1 ] );
  187. expect( img.getPath() ).to.deep.equal( [ 1, 2 ] );
  188. expect( charR.getPath() ).to.deep.equal( [ 1, 3 ] );
  189. expect( three.getPath() ).to.deep.equal( [ 2 ] );
  190. } );
  191. } );
  192. describe( 'getDocument()', () => {
  193. it( 'should return null if any parent has not set Document', () => {
  194. expect( charA.document ).to.be.null;
  195. } );
  196. it( 'should return Document attached to the parent element', () => {
  197. const docMock = createDocumentMock();
  198. const parent = new RootEditableElement( 'div' );
  199. parent._document = docMock;
  200. const child = new Element( 'p' );
  201. child.parent = parent;
  202. expect( parent.document ).to.equal( docMock );
  203. expect( child.document ).to.equal( docMock );
  204. } );
  205. it( 'should return null if element is inside DocumentFragment', () => {
  206. const child = new Element( 'p' );
  207. new DocumentFragment( [ child ] ); // eslint-disable-line no-new
  208. expect( child.document ).to.be.null;
  209. } );
  210. } );
  211. describe( 'getRoot()', () => {
  212. it( 'should return this element if it has no parent', () => {
  213. const child = new Element( 'p' );
  214. expect( child.root ).to.equal( child );
  215. } );
  216. it( 'should return root element', () => {
  217. const parent = new RootEditableElement( 'div' );
  218. parent._document = createDocumentMock();
  219. const child = new Element( 'p' );
  220. child.parent = parent;
  221. expect( parent.root ).to.equal( parent );
  222. expect( child.root ).to.equal( parent );
  223. } );
  224. } );
  225. describe( 'isBefore()', () => {
  226. // Model is: <root><one></one><two>ba<img></img>r</two><three></three>
  227. it( 'should return true if the element is before given element', () => {
  228. expect( one.isBefore( two ) ).to.be.true;
  229. expect( one.isBefore( img ) ).to.be.true;
  230. expect( two.isBefore( charB ) ).to.be.true;
  231. expect( two.isBefore( charR ) ).to.be.true;
  232. expect( two.isBefore( three ) ).to.be.true;
  233. expect( root.isBefore( one ) ).to.be.true;
  234. } );
  235. it( 'should return false if the element is after given element', () => {
  236. expect( two.isBefore( one ) ).to.be.false;
  237. expect( img.isBefore( one ) ).to.be.false;
  238. expect( charB.isBefore( two ) ).to.be.false;
  239. expect( charR.isBefore( two ) ).to.be.false;
  240. expect( three.isBefore( two ) ).to.be.false;
  241. expect( one.isBefore( root ) ).to.be.false;
  242. } );
  243. it( 'should return false if the same element is given', () => {
  244. expect( one.isBefore( one ) ).to.be.false;
  245. } );
  246. it( 'should return false if elements are in different roots', () => {
  247. const otherRoot = new Element( 'root' );
  248. const otherElement = new Element( 'element' );
  249. otherRoot._appendChild( otherElement );
  250. expect( otherElement.isBefore( three ) ).to.be.false;
  251. } );
  252. } );
  253. describe( 'isAfter()', () => {
  254. // Model is: <root><one></one><two>ba<img></img>r</two><three></three>
  255. it( 'should return true if the element is after given element', () => {
  256. expect( two.isAfter( one ) ).to.be.true;
  257. expect( img.isAfter( one ) ).to.be.true;
  258. expect( charB.isAfter( two ) ).to.be.true;
  259. expect( charR.isAfter( two ) ).to.be.true;
  260. expect( three.isAfter( two ) ).to.be.true;
  261. expect( one.isAfter( root ) ).to.be.true;
  262. } );
  263. it( 'should return false if the element is before given element', () => {
  264. expect( one.isAfter( two ) ).to.be.false;
  265. expect( one.isAfter( img ) ).to.be.false;
  266. expect( two.isAfter( charB ) ).to.be.false;
  267. expect( two.isAfter( charR ) ).to.be.false;
  268. expect( two.isAfter( three ) ).to.be.false;
  269. expect( root.isAfter( one ) ).to.be.false;
  270. } );
  271. it( 'should return false if the same element is given', () => {
  272. expect( one.isAfter( one ) ).to.be.false;
  273. } );
  274. it( 'should return false if elements are in different roots', () => {
  275. const otherRoot = new Element( 'root' );
  276. const otherElement = new Element( 'element' );
  277. otherRoot._appendChild( otherElement );
  278. expect( three.isAfter( otherElement ) ).to.be.false;
  279. } );
  280. } );
  281. describe( '_remove()', () => {
  282. it( 'should remove node from its parent', () => {
  283. const char = new Text( 'a' );
  284. const parent = new Element( 'p', null, [ char ] );
  285. char._remove();
  286. expect( parent.getChildIndex( char ) ).to.equal( -1 );
  287. } );
  288. it( 'uses parent._removeChildren method', () => {
  289. const char = new Text( 'a' );
  290. const parent = new Element( 'p', null, [ char ] );
  291. const _removeChildrenSpy = sinon.spy( parent, '_removeChildren' );
  292. const index = char.index;
  293. char._remove();
  294. _removeChildrenSpy.restore();
  295. sinon.assert.calledOnce( _removeChildrenSpy );
  296. sinon.assert.calledWithExactly( _removeChildrenSpy, index );
  297. } );
  298. } );
  299. describe( 'toJSON()', () => {
  300. it( 'should prevent circular reference when stringifying a node', () => {
  301. const char = new Text( 'a' );
  302. const parent = new Element( 'p', null );
  303. parent._appendChild( char );
  304. const json = JSON.stringify( char );
  305. const parsed = JSON.parse( json );
  306. expect( parsed ).to.deep.equal( {
  307. _textData: 'a'
  308. } );
  309. } );
  310. } );
  311. describe( 'change event', () => {
  312. let root, text, img, rootChangeSpy;
  313. before( () => {
  314. rootChangeSpy = sinon.spy();
  315. } );
  316. beforeEach( () => {
  317. text = new Text( 'foo' );
  318. img = new Element( 'img', { 'src': 'img.png' } );
  319. root = new Element( 'p', { renderer: { markToSync: rootChangeSpy } } );
  320. root._appendChild( [ text, img ] );
  321. root.on( 'change:children', ( evt, node ) => rootChangeSpy( 'children', node ) );
  322. root.on( 'change:attributes', ( evt, node ) => rootChangeSpy( 'attributes', node ) );
  323. root.on( 'change:text', ( evt, node ) => rootChangeSpy( 'text', node ) );
  324. rootChangeSpy.resetHistory();
  325. } );
  326. it( 'should be fired on the node', () => {
  327. const imgChangeSpy = sinon.spy();
  328. img.on( 'change:attributes', ( evt, node ) => {
  329. imgChangeSpy( 'attributes', node );
  330. } );
  331. img._setAttribute( 'width', 100 );
  332. sinon.assert.calledOnce( imgChangeSpy );
  333. sinon.assert.calledWith( imgChangeSpy, 'attributes', img );
  334. } );
  335. it( 'should be fired on the parent', () => {
  336. img._setAttribute( 'width', 100 );
  337. sinon.assert.calledOnce( rootChangeSpy );
  338. sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
  339. } );
  340. describe( '_setAttribute()', () => {
  341. it( 'should fire change event', () => {
  342. img._setAttribute( 'width', 100 );
  343. sinon.assert.calledOnce( rootChangeSpy );
  344. sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
  345. } );
  346. } );
  347. describe( '_removeAttribute()', () => {
  348. it( 'should fire change event', () => {
  349. img._removeAttribute( 'src' );
  350. sinon.assert.calledOnce( rootChangeSpy );
  351. sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
  352. } );
  353. } );
  354. describe( '_insertChild()', () => {
  355. it( 'should fire change event', () => {
  356. root._insertChild( 1, new Element( 'img' ) );
  357. sinon.assert.calledOnce( rootChangeSpy );
  358. sinon.assert.calledWith( rootChangeSpy, 'children', root );
  359. } );
  360. } );
  361. describe( '_appendChild()', () => {
  362. it( 'should fire change event', () => {
  363. root._appendChild( new Element( 'img' ) );
  364. sinon.assert.calledOnce( rootChangeSpy );
  365. sinon.assert.calledWith( rootChangeSpy, 'children', root );
  366. } );
  367. } );
  368. describe( '_removeChildren()', () => {
  369. it( 'should fire change event', () => {
  370. root._removeChildren( 1, 1 );
  371. sinon.assert.calledOnce( rootChangeSpy );
  372. sinon.assert.calledWith( rootChangeSpy, 'children', root );
  373. } );
  374. } );
  375. describe( 'setText', () => {
  376. it( 'should fire change event', () => {
  377. text._data = 'bar';
  378. sinon.assert.calledOnce( rootChangeSpy );
  379. sinon.assert.calledWith( rootChangeSpy, 'text', text );
  380. } );
  381. } );
  382. } );
  383. } );