nodelist.js 6.6 KB

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