nodelist.js 7.2 KB

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