node.js 17 KB

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