8
0

nodelist.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 NodeList from '../../src/model/nodelist';
  6. import Element from '../../src/model/element';
  7. import Text from '../../src/model/text';
  8. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  9. describe( 'NodeList', () => {
  10. let nodes, p, foo, img;
  11. beforeEach( () => {
  12. p = new Element( 'p' );
  13. foo = new Text( 'foo' );
  14. img = new Element( 'image' );
  15. nodes = new NodeList( [ p, foo, img ] );
  16. } );
  17. describe( 'constructor()', () => {
  18. it( 'should create empty node list', () => {
  19. expect( new NodeList().length ).to.equal( 0 );
  20. } );
  21. it( 'should create node list with passed nodes', () => {
  22. expect( nodes.length ).to.equal( 3 );
  23. expect( nodes.getNode( 0 ) ).to.equal( p );
  24. expect( nodes.getNode( 1 ) ).to.equal( foo );
  25. expect( nodes.getNode( 2 ) ).to.equal( img );
  26. } );
  27. } );
  28. describe( 'iterator', () => {
  29. it( 'should iterate over all nodes from node list', () => {
  30. expect( Array.from( nodes ) ).to.deep.equal( [ p, foo, img ] );
  31. } );
  32. } );
  33. describe( 'getMaxOffset', () => {
  34. it( 'should be equal to the sum of offsetSize of all nodes in node list', () => {
  35. expect( nodes.maxOffset ).to.equal( 5 );
  36. } );
  37. } );
  38. describe( 'getNode', () => {
  39. it( 'should return null for wrong index', () => {
  40. expect( nodes.getNode( -1 ) ).to.be.null;
  41. expect( nodes.getNode( 3 ) ).to.be.null;
  42. } );
  43. } );
  44. describe( 'getNodeIndex', () => {
  45. it( 'should return an index at which given node is stored', () => {
  46. expect( nodes.getNodeIndex( p ) ).to.equal( 0 );
  47. expect( nodes.getNodeIndex( foo ) ).to.equal( 1 );
  48. expect( nodes.getNodeIndex( img ) ).to.equal( 2 );
  49. } );
  50. it( 'should return null if node is not in the node list', () => {
  51. expect( nodes.getNodeIndex( new Element( 'p' ) ) ).to.be.null;
  52. } );
  53. } );
  54. describe( 'getNodeStartOffset', () => {
  55. it( 'should return offset at which given node starts', () => {
  56. expect( nodes.getNodeStartOffset( p ) ).to.equal( 0 );
  57. expect( nodes.getNodeStartOffset( foo ) ).to.equal( 1 );
  58. expect( nodes.getNodeStartOffset( img ) ).to.equal( 4 );
  59. } );
  60. it( 'should return null if node is not in the node list', () => {
  61. expect( nodes.getNodeStartOffset( new Element( 'p' ) ) ).to.be.null;
  62. } );
  63. } );
  64. describe( 'indexToOffset', () => {
  65. it( 'should return starting offset of a node stored at given index', () => {
  66. expect( nodes.indexToOffset( 0 ) ).to.equal( 0 );
  67. expect( nodes.indexToOffset( 1 ) ).to.equal( 1 );
  68. expect( nodes.indexToOffset( 2 ) ).to.equal( 4 );
  69. } );
  70. it( 'should throw if given offset is too high or too low', () => {
  71. expectToThrowCKEditorError( () => {
  72. nodes.indexToOffset( -1 );
  73. }, /model-nodelist-index-out-of-bounds/, nodes );
  74. expectToThrowCKEditorError( () => {
  75. nodes.indexToOffset( 99 );
  76. }, /model-nodelist-index-out-of-bounds/, nodes );
  77. } );
  78. it( 'should return length if given offset is equal to maxOffset', () => {
  79. expect( nodes.indexToOffset( 3 ) ).to.equal( 5 );
  80. } );
  81. } );
  82. describe( 'offsetToIndex', () => {
  83. it( 'should return index of a node that occupies given offset', () => {
  84. expect( nodes.offsetToIndex( 0 ) ).to.equal( 0 );
  85. expect( nodes.offsetToIndex( 1 ) ).to.equal( 1 );
  86. expect( nodes.offsetToIndex( 2 ) ).to.equal( 1 );
  87. expect( nodes.offsetToIndex( 3 ) ).to.equal( 1 );
  88. expect( nodes.offsetToIndex( 4 ) ).to.equal( 2 );
  89. } );
  90. it( 'should throw if given offset is too high or too low', () => {
  91. expectToThrowCKEditorError( () => {
  92. nodes.offsetToIndex( -1 );
  93. }, /nodelist-offset-out-of-bounds/, nodes );
  94. expectToThrowCKEditorError( () => {
  95. nodes.offsetToIndex( 55 );
  96. }, /nodelist-offset-out-of-bounds/, nodes );
  97. } );
  98. it( 'should return length if given offset is equal to maxOffset', () => {
  99. expect( nodes.offsetToIndex( 5 ) ).to.equal( 3 );
  100. } );
  101. } );
  102. describe( '_insertNodes', () => {
  103. it( 'should insert nodes at given index', () => {
  104. const newImg = new Element( 'image' );
  105. nodes._insertNodes( 1, [ newImg ] );
  106. const bar = new Text( 'bar', { bold: true } );
  107. const xyz = new Text( 'xyz' );
  108. nodes._insertNodes( 4, [ bar, xyz ] );
  109. expect( nodes.length ).to.equal( 6 );
  110. expect( nodes.maxOffset ).to.equal( 12 );
  111. expect( Array.from( nodes ) ).to.deep.equal( [ p, newImg, foo, img, bar, xyz ] );
  112. expect( nodes.getNode( 0 ) ).to.equal( p );
  113. expect( nodes.getNode( 1 ) ).to.equal( newImg );
  114. expect( nodes.getNode( 2 ) ).to.equal( foo );
  115. expect( nodes.getNode( 3 ) ).to.equal( img );
  116. expect( nodes.getNode( 4 ) ).to.equal( bar );
  117. expect( nodes.getNode( 5 ) ).to.equal( xyz );
  118. expect( nodes.getNodeIndex( p ) ).to.equal( 0 );
  119. expect( nodes.getNodeIndex( newImg ) ).to.equal( 1 );
  120. expect( nodes.getNodeIndex( foo ) ).to.equal( 2 );
  121. expect( nodes.getNodeIndex( img ) ).to.equal( 3 );
  122. expect( nodes.getNodeIndex( bar ) ).to.equal( 4 );
  123. expect( nodes.getNodeIndex( xyz ) ).to.equal( 5 );
  124. expect( nodes.getNodeStartOffset( p ) ).to.equal( 0 );
  125. expect( nodes.getNodeStartOffset( newImg ) ).to.equal( 1 );
  126. expect( nodes.getNodeStartOffset( foo ) ).to.equal( 2 );
  127. expect( nodes.getNodeStartOffset( img ) ).to.equal( 5 );
  128. expect( nodes.getNodeStartOffset( bar ) ).to.equal( 6 );
  129. expect( nodes.getNodeStartOffset( xyz ) ).to.equal( 9 );
  130. } );
  131. it( 'should throw if not a Node is inserted', () => {
  132. expectToThrowCKEditorError( () => {
  133. nodes._insertNodes( 0, [ 'foo' ] );
  134. }, 'nodelist-insertnodes-not-node', nodes );
  135. } );
  136. } );
  137. describe( '_removeNodes', () => {
  138. it( 'should remove one or more nodes from given index', () => {
  139. nodes._removeNodes( 0, 2 );
  140. expect( nodes.length ).to.equal( 1 );
  141. expect( nodes.maxOffset ).to.equal( 1 );
  142. expect( nodes.getNode( 0 ) ).to.equal( img );
  143. expect( nodes.getNodeIndex( img ) ).to.equal( 0 );
  144. expect( nodes.getNodeStartOffset( img ) ).to.equal( 0 );
  145. } );
  146. it( 'should remove one node if howMany parameter was not specified', () => {
  147. nodes._removeNodes( 1 );
  148. expect( nodes.length ).to.equal( 2 );
  149. expect( nodes.maxOffset ).to.equal( 2 );
  150. expect( nodes.getNode( 0 ) ).to.equal( p );
  151. expect( nodes.getNode( 1 ) ).to.equal( img );
  152. expect( nodes.getNodeIndex( p ) ).to.equal( 0 );
  153. expect( nodes.getNodeIndex( img ) ).to.equal( 1 );
  154. expect( nodes.getNodeStartOffset( p ) ).to.equal( 0 );
  155. expect( nodes.getNodeStartOffset( img ) ).to.equal( 1 );
  156. } );
  157. } );
  158. describe( 'toJSON', () => {
  159. it( 'should serialize empty node list', () => {
  160. expect( ( new NodeList() ).toJSON() ).to.deep.equal( [] );
  161. } );
  162. it( 'should serialize node list with nodes', () => {
  163. expect( nodes.toJSON() ).to.deep.equal( [
  164. { name: 'p' },
  165. { data: 'foo' },
  166. { name: 'image' }
  167. ] );
  168. } );
  169. } );
  170. } );