nodelist.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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( 'totalOffset', () => {
  36. it( 'should be equal to the sum of offsetSize of all nodes in node list', () => {
  37. expect( nodes.totalOffset ).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 return 0 if given index was lower than 0', () => {
  73. expect( nodes.indexToOffset( -1 ) ).to.equal( 0 );
  74. } );
  75. it( 'should return totalOffset if given index was too high', () => {
  76. expect( nodes.indexToOffset( 3 ) ).to.equal( 5 );
  77. expect( nodes.indexToOffset( 99 ) ).to.equal( 5 );
  78. } );
  79. } );
  80. describe( 'offsetToIndex', () => {
  81. it( 'should return index of a node that occupies given offset', () => {
  82. expect( nodes.offsetToIndex( 0 ) ).to.equal( 0 );
  83. expect( nodes.offsetToIndex( 1 ) ).to.equal( 1 );
  84. expect( nodes.offsetToIndex( 2 ) ).to.equal( 1 );
  85. expect( nodes.offsetToIndex( 3 ) ).to.equal( 1 );
  86. expect( nodes.offsetToIndex( 4 ) ).to.equal( 2 );
  87. } );
  88. it( 'should return 0 if given offset was lower than 0', () => {
  89. expect( nodes.offsetToIndex( -1 ) ).to.equal( 0 );
  90. } );
  91. it( 'should return length if given offset was too high', () => {
  92. expect( nodes.offsetToIndex( 5 ) ).to.equal( 3 );
  93. expect( nodes.offsetToIndex( 99 ) ).to.equal( 3 );
  94. } );
  95. } );
  96. describe( 'insertNodes', () => {
  97. it( 'should insert nodes at given index', () => {
  98. let newImg = new Element( 'image' );
  99. nodes.insertNodes( 1, [ newImg ] );
  100. let bar = new Text( 'bar', { bold: true } );
  101. let xyz = new Text( 'xyz' );
  102. nodes.insertNodes( 4, [ bar, xyz ] );
  103. expect( nodes.length ).to.equal( 6 );
  104. expect( nodes.totalOffset ).to.equal( 12 );
  105. expect( Array.from( nodes ) ).to.deep.equal( [ p, newImg, foo, img, bar, xyz ] );
  106. expect( nodes.getNode( 0 ) ).to.equal( p );
  107. expect( nodes.getNode( 1 ) ).to.equal( newImg );
  108. expect( nodes.getNode( 2 ) ).to.equal( foo );
  109. expect( nodes.getNode( 3 ) ).to.equal( img );
  110. expect( nodes.getNode( 4 ) ).to.equal( bar );
  111. expect( nodes.getNode( 5 ) ).to.equal( xyz );
  112. expect( nodes.getNodeIndex( p ) ).to.equal( 0 );
  113. expect( nodes.getNodeIndex( newImg ) ).to.equal( 1 );
  114. expect( nodes.getNodeIndex( foo ) ).to.equal( 2 );
  115. expect( nodes.getNodeIndex( img ) ).to.equal( 3 );
  116. expect( nodes.getNodeIndex( bar ) ).to.equal( 4 );
  117. expect( nodes.getNodeIndex( xyz ) ).to.equal( 5 );
  118. expect( nodes.getNodeStartOffset( p ) ).to.equal( 0 );
  119. expect( nodes.getNodeStartOffset( newImg ) ).to.equal( 1 );
  120. expect( nodes.getNodeStartOffset( foo ) ).to.equal( 2 );
  121. expect( nodes.getNodeStartOffset( img ) ).to.equal( 5 );
  122. expect( nodes.getNodeStartOffset( bar ) ).to.equal( 6 );
  123. expect( nodes.getNodeStartOffset( xyz ) ).to.equal( 9 );
  124. } );
  125. it( 'should throw if not a Node is inserted', () => {
  126. expect( () => {
  127. nodes.insertNodes( 0, [ 'foo' ] );
  128. } ).to.throw( CKEditorError, /nodelist-insertNodes-not-node/ );
  129. } );
  130. } );
  131. describe( 'removeNodes', () => {
  132. it( 'should remove one or more nodes from given index', () => {
  133. nodes.removeNodes( 0, 2 );
  134. expect( nodes.length ).to.equal( 1 );
  135. expect( nodes.totalOffset ).to.equal( 1 );
  136. expect( nodes.getNode( 0 ) ).to.equal( img );
  137. expect( nodes.getNodeIndex( img ) ).to.equal( 0 );
  138. expect( nodes.getNodeStartOffset( img ) ).to.equal( 0 );
  139. } );
  140. it( 'should remove one node if howMany parameter was not specified', () => {
  141. nodes.removeNodes( 1 );
  142. expect( nodes.length ).to.equal( 2 );
  143. expect( nodes.totalOffset ).to.equal( 2 );
  144. expect( nodes.getNode( 0 ) ).to.equal( p );
  145. expect( nodes.getNode( 1 ) ).to.equal( img );
  146. expect( nodes.getNodeIndex( p ) ).to.equal( 0 );
  147. expect( nodes.getNodeIndex( img ) ).to.equal( 1 );
  148. expect( nodes.getNodeStartOffset( p ) ).to.equal( 0 );
  149. expect( nodes.getNodeStartOffset( img ) ).to.equal( 1 );
  150. } );
  151. } );
  152. describe( 'toJSON', () => {
  153. it( 'should serialize empty node list', () => {
  154. expect( jsonParseStringify( new NodeList() ) ).to.deep.equal( [] );
  155. } );
  156. it( 'should serialize node list with nodes', () => {
  157. expect( jsonParseStringify( nodes ) ).to.deep.equal( [
  158. { name: 'p' },
  159. { data: 'foo' },
  160. { name: 'image' }
  161. ] );
  162. } );
  163. } );
  164. } );