node.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 createDocumentMock from '../../tests/view/_utils/createdocumentmock';
  11. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. import Document from '../../src/view/document';
  13. import { StylesProcessor } from '../../src/view/stylesmap';
  14. describe( 'Node', () => {
  15. let root, document,
  16. one, two, three,
  17. charB, charA, charR, img,
  18. stylesProcessor;
  19. before( () => {
  20. stylesProcessor = new StylesProcessor();
  21. document = new Document( stylesProcessor );
  22. charB = new Text( document, 'b' );
  23. charA = new Text( document, 'a' );
  24. img = new Element( document, 'img' );
  25. charR = new Text( document, 'r' );
  26. one = new Element( document, 'one' );
  27. two = new Element( document, 'two', null, [ charB, charA, img, charR ] );
  28. three = new Element( document, 'three' );
  29. root = new Element( document, null, null, [ one, two, three ] );
  30. } );
  31. describe( 'is()', () => {
  32. let node;
  33. beforeEach( () => {
  34. node = new Node();
  35. } );
  36. it( 'should return true for node', () => {
  37. expect( node.is( 'node' ) ).to.be.true;
  38. expect( node.is( 'view:node' ) ).to.be.true;
  39. } );
  40. it( 'should return false for other accept values', () => {
  41. expect( node.is( 'rootElement' ) ).to.be.false;
  42. expect( node.is( 'containerElement' ) ).to.be.false;
  43. expect( node.is( 'element' ) ).to.be.false;
  44. expect( node.is( 'p' ) ).to.be.false;
  45. expect( node.is( 'text' ) ).to.be.false;
  46. expect( node.is( 'textProxy' ) ).to.be.false;
  47. expect( node.is( 'attributeElement' ) ).to.be.false;
  48. expect( node.is( 'uiElement' ) ).to.be.false;
  49. expect( node.is( 'emptyElement' ) ).to.be.false;
  50. expect( node.is( 'documentFragment' ) ).to.be.false;
  51. } );
  52. } );
  53. describe( 'getNextSibling/getPreviousSibling()', () => {
  54. it( 'should return next sibling', () => {
  55. expect( root.nextSibling ).to.be.null;
  56. expect( one.nextSibling ).to.equal( two );
  57. expect( two.nextSibling ).to.equal( three );
  58. expect( three.nextSibling ).to.be.null;
  59. expect( charB.nextSibling ).to.equal( charA );
  60. expect( charA.nextSibling ).to.equal( img );
  61. expect( img.nextSibling ).to.equal( charR );
  62. expect( charR.nextSibling ).to.be.null;
  63. } );
  64. it( 'should return previous sibling', () => {
  65. expect( root.previousSibling ).to.be.null;
  66. expect( one.previousSibling ).to.be.null;
  67. expect( two.previousSibling ).to.equal( one );
  68. expect( three.previousSibling ).to.equal( two );
  69. expect( charB.previousSibling ).to.be.null;
  70. expect( charA.previousSibling ).to.equal( charB );
  71. expect( img.previousSibling ).to.equal( charA );
  72. expect( charR.previousSibling ).to.equal( img );
  73. } );
  74. } );
  75. describe( 'getAncestors()', () => {
  76. it( 'should return empty array for node without ancestors', () => {
  77. const result = root.getAncestors();
  78. expect( result ).to.be.an( 'array' );
  79. expect( result.length ).to.equal( 0 );
  80. } );
  81. it( 'should return array including node itself if requested', () => {
  82. const result = root.getAncestors( { includeSelf: true } );
  83. expect( result ).to.be.an( 'array' );
  84. expect( result.length ).to.equal( 1 );
  85. expect( result[ 0 ] ).to.equal( root );
  86. } );
  87. it( 'should return array of ancestors', () => {
  88. const result = charR.getAncestors();
  89. expect( result.length ).to.equal( 2 );
  90. expect( result[ 0 ] ).to.equal( root );
  91. expect( result[ 1 ] ).to.equal( two );
  92. const result2 = charR.getAncestors( { includeSelf: true } );
  93. expect( result2.length ).to.equal( 3 );
  94. expect( result2[ 0 ] ).to.equal( root );
  95. expect( result2[ 1 ] ).to.equal( two );
  96. expect( result2[ 2 ] ).to.equal( charR );
  97. } );
  98. it( 'should return array of ancestors starting from parent', () => {
  99. const result = charR.getAncestors( { parentFirst: true } );
  100. expect( result.length ).to.equal( 2 );
  101. expect( result[ 0 ] ).to.equal( two );
  102. expect( result[ 1 ] ).to.equal( root );
  103. const result2 = charR.getAncestors( { includeSelf: true, parentFirst: true } );
  104. expect( result2.length ).to.equal( 3 );
  105. expect( result2[ 2 ] ).to.equal( root );
  106. expect( result2[ 1 ] ).to.equal( two );
  107. expect( result2[ 0 ] ).to.equal( charR );
  108. } );
  109. it( 'should return ancestors including DocumentFragment', () => {
  110. const fragment = new DocumentFragment( document, root );
  111. const result = img.getAncestors();
  112. root._remove();
  113. expect( result.length ).to.equal( 3 );
  114. expect( result[ 0 ] ).to.equal( fragment );
  115. expect( result[ 1 ] ).to.equal( root );
  116. expect( result[ 2 ] ).to.equal( two );
  117. } );
  118. } );
  119. describe( 'getCommonAncestor()', () => {
  120. it( 'should return the parent element for the same node', () => {
  121. expect( img.getCommonAncestor( img ) ).to.equal( two );
  122. } );
  123. it( 'should return the given node for the same node if includeSelf is used', () => {
  124. expect( img.getCommonAncestor( img, { includeSelf: true } ) ).to.equal( img );
  125. } );
  126. it( 'should return null for detached subtrees', () => {
  127. const detached = new Element( document, 'foo' );
  128. expect( img.getCommonAncestor( detached ) ).to.be.null;
  129. expect( detached.getCommonAncestor( img ) ).to.be.null;
  130. expect( img.getCommonAncestor( detached, { includeSelf: true } ) ).to.be.null;
  131. expect( detached.getCommonAncestor( img, { includeSelf: true } ) ).to.be.null;
  132. } );
  133. it( 'should return null when one of the nodes is a tree root itself', () => {
  134. expect( root.getCommonAncestor( img ) ).to.be.null;
  135. expect( img.getCommonAncestor( root ) ).to.be.null;
  136. expect( root.getCommonAncestor( root ) ).to.be.null;
  137. } );
  138. it( 'should return root when one of the nodes is a tree root itself and includeSelf is used', () => {
  139. expect( root.getCommonAncestor( img, { includeSelf: true } ) ).to.equal( root );
  140. expect( img.getCommonAncestor( root, { includeSelf: true } ) ).to.equal( root );
  141. expect( root.getCommonAncestor( root, { includeSelf: true } ) ).to.equal( root );
  142. } );
  143. it( 'should return parent of the nodes at the same level', () => {
  144. expect( img.getCommonAncestor( charA ), 1 ).to.equal( two );
  145. expect( charB.getCommonAncestor( charA ), 2 ).to.equal( two );
  146. expect( img.getCommonAncestor( charA, { includeSelf: true } ), 3 ).to.equal( two );
  147. expect( charB.getCommonAncestor( charA, { includeSelf: true } ), 4 ).to.equal( two );
  148. } );
  149. it( 'should return proper element for nodes in different branches and on different levels', () => {
  150. const foo = new Text( 'foo' );
  151. const bar = new Text( 'bar' );
  152. const bom = new Text( 'bom' );
  153. const d = new Element( document, 'd', null, [ bar ] );
  154. const c = new Element( document, 'c', null, [ foo, d ] );
  155. const b = new Element( document, 'b', null, [ c ] );
  156. const e = new Element( document, 'e', null, [ bom ] );
  157. const a = new Element( document, 'a', null, [ b, e ] );
  158. // <a><b><c>foo<d>bar</d></c></b><e>bom</e></a>
  159. expect( bar.getCommonAncestor( foo ), 1 ).to.equal( c );
  160. expect( foo.getCommonAncestor( d ), 2 ).to.equal( c );
  161. expect( c.getCommonAncestor( b ), 3 ).to.equal( a );
  162. expect( bom.getCommonAncestor( d ), 4 ).to.equal( a );
  163. expect( b.getCommonAncestor( bom ), 5 ).to.equal( a );
  164. expect( b.getCommonAncestor( bar ), 6 ).to.equal( a );
  165. expect( bar.getCommonAncestor( foo, { includeSelf: true } ), 11 ).to.equal( c );
  166. expect( foo.getCommonAncestor( d, { includeSelf: true } ), 12 ).to.equal( c );
  167. expect( c.getCommonAncestor( b, { includeSelf: true } ), 13 ).to.equal( b );
  168. expect( bom.getCommonAncestor( d, { includeSelf: true } ), 14 ).to.equal( a );
  169. expect( b.getCommonAncestor( bom, { includeSelf: true } ), 15 ).to.equal( a );
  170. expect( b.getCommonAncestor( bar, { includeSelf: true } ), 16 ).to.equal( b );
  171. } );
  172. it( 'should return document fragment', () => {
  173. const foo = new Text( 'foo' );
  174. const bar = new Text( 'bar' );
  175. const df = new DocumentFragment( document, [ foo, bar ] );
  176. expect( foo.getCommonAncestor( bar ) ).to.equal( df );
  177. } );
  178. } );
  179. describe( 'getIndex()', () => {
  180. it( 'should return null if the parent is null', () => {
  181. expect( root.index ).to.be.null;
  182. } );
  183. it( 'should return index in the parent', () => {
  184. expect( one.index ).to.equal( 0 );
  185. expect( two.index ).to.equal( 1 );
  186. expect( three.index ).to.equal( 2 );
  187. expect( charB.index ).to.equal( 0 );
  188. expect( charA.index ).to.equal( 1 );
  189. expect( img.index ).to.equal( 2 );
  190. expect( charR.index ).to.equal( 3 );
  191. } );
  192. it( 'should throw an error if parent does not contain element', () => {
  193. const f = new Text( 'f' );
  194. const bar = new Element( document, 'bar', [], [] );
  195. f.parent = bar;
  196. expectToThrowCKEditorError( () => {
  197. f.index;
  198. }, /view-node-not-found-in-parent/, bar );
  199. } );
  200. } );
  201. describe( 'getPath()', () => {
  202. it( 'should return empty array is the element is the root', () => {
  203. expect( root.getPath() ).to.deep.equal( [] );
  204. } );
  205. it( 'should return array with indices of given element and its ancestors starting from top-most one', () => {
  206. expect( one.getPath() ).to.deep.equal( [ 0 ] );
  207. expect( two.getPath() ).to.deep.equal( [ 1 ] );
  208. expect( img.getPath() ).to.deep.equal( [ 1, 2 ] );
  209. expect( charR.getPath() ).to.deep.equal( [ 1, 3 ] );
  210. expect( three.getPath() ).to.deep.equal( [ 2 ] );
  211. } );
  212. } );
  213. describe( 'getRoot()', () => {
  214. it( 'should return this element if it has no parent', () => {
  215. const child = new Element( document, 'p' );
  216. expect( child.root ).to.equal( child );
  217. } );
  218. it( 'should return root element', () => {
  219. const parent = new RootEditableElement( document, 'div' );
  220. parent._document = createDocumentMock();
  221. const child = new Element( document, '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( document, 'root' );
  250. const otherElement = new Element( document, '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( document, 'root' );
  278. const otherElement = new Element( document, '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( document, '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( document, '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( document, 'a' );
  304. const parent = new Element( document, 'p', null );
  305. parent._appendChild( char );
  306. sinon.stub( char, 'document' ).value( 'view.Document()' );
  307. const json = JSON.stringify( char );
  308. const parsed = JSON.parse( json );
  309. expect( parsed ).to.deep.equal( {
  310. _textData: 'a',
  311. document: 'view.Document()'
  312. } );
  313. } );
  314. } );
  315. describe( 'change event', () => {
  316. let root, text, img, rootChangeSpy;
  317. before( () => {
  318. rootChangeSpy = sinon.spy();
  319. } );
  320. beforeEach( () => {
  321. text = new Text( 'foo' );
  322. img = new Element( document, 'img', { 'src': 'img.png' } );
  323. root = new Element( document, 'p', { renderer: { markToSync: rootChangeSpy } } );
  324. root._appendChild( [ text, img ] );
  325. root.on( 'change:children', ( evt, node ) => rootChangeSpy( 'children', node ) );
  326. root.on( 'change:attributes', ( evt, node ) => rootChangeSpy( 'attributes', node ) );
  327. root.on( 'change:text', ( evt, node ) => rootChangeSpy( 'text', node ) );
  328. rootChangeSpy.resetHistory();
  329. } );
  330. it( 'should be fired on the node', () => {
  331. const imgChangeSpy = sinon.spy();
  332. img.on( 'change:attributes', ( evt, node ) => {
  333. imgChangeSpy( 'attributes', node );
  334. } );
  335. img._setAttribute( 'width', 100 );
  336. sinon.assert.calledOnce( imgChangeSpy );
  337. sinon.assert.calledWith( imgChangeSpy, 'attributes', img );
  338. } );
  339. it( 'should be fired on the parent', () => {
  340. img._setAttribute( 'width', 100 );
  341. sinon.assert.calledOnce( rootChangeSpy );
  342. sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
  343. } );
  344. describe( '_setAttribute()', () => {
  345. it( 'should fire change event', () => {
  346. img._setAttribute( 'width', 100 );
  347. sinon.assert.calledOnce( rootChangeSpy );
  348. sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
  349. } );
  350. } );
  351. describe( '_removeAttribute()', () => {
  352. it( 'should fire change event', () => {
  353. img._removeAttribute( 'src' );
  354. sinon.assert.calledOnce( rootChangeSpy );
  355. sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
  356. } );
  357. } );
  358. describe( '_insertChild()', () => {
  359. it( 'should fire change event', () => {
  360. root._insertChild( 1, new Element( document, 'img' ) );
  361. sinon.assert.calledOnce( rootChangeSpy );
  362. sinon.assert.calledWith( rootChangeSpy, 'children', root );
  363. } );
  364. } );
  365. describe( '_appendChild()', () => {
  366. it( 'should fire change event', () => {
  367. root._appendChild( new Element( document, 'img' ) );
  368. sinon.assert.calledOnce( rootChangeSpy );
  369. sinon.assert.calledWith( rootChangeSpy, 'children', root );
  370. } );
  371. } );
  372. describe( '_removeChildren()', () => {
  373. it( 'should fire change event', () => {
  374. root._removeChildren( 1, 1 );
  375. sinon.assert.calledOnce( rootChangeSpy );
  376. sinon.assert.calledWith( rootChangeSpy, 'children', root );
  377. } );
  378. } );
  379. describe( 'setText', () => {
  380. it( 'should fire change event', () => {
  381. text._data = 'bar';
  382. sinon.assert.calledOnce( rootChangeSpy );
  383. sinon.assert.calledWith( rootChangeSpy, 'text', text );
  384. } );
  385. } );
  386. } );
  387. } );