nodelist.js 6.8 KB

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