nodelist.js 6.8 KB

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