node.js 16 KB

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