8
0

nodelist.js 7.3 KB

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