nodelist.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel */
  6. 'use strict';
  7. import NodeList from '/ckeditor5/core/treemodel/nodelist.js';
  8. import Element from '/ckeditor5/core/treemodel/element.js';
  9. import Text from '/ckeditor5/core/treemodel/text.js';
  10. import Attribute from '/ckeditor5/core/treemodel/attribute.js';
  11. describe( 'NodeList', () => {
  12. describe( 'constructor', () => {
  13. it( 'should change array of characters into a set of nodes', () => {
  14. let textFoo = new Text( 'foo' );
  15. let nodeList = new NodeList( [ 'foo', new Text( 'x' ), 'bar', textFoo.getTextNode() ] );
  16. expect( nodeList.length ).to.equal( 10 );
  17. expect( nodeList.get( 0 ).text ).to.equal( 'f' );
  18. expect( nodeList.get( 1 ).text ).to.equal( 'o' );
  19. expect( nodeList.get( 2 ).text ).to.equal( 'o' );
  20. expect( nodeList.get( 3 ).text ).to.equal( 'x' );
  21. expect( nodeList.get( 4 ).text ).to.equal( 'b' );
  22. expect( nodeList.get( 5 ).text ).to.equal( 'a' );
  23. expect( nodeList.get( 6 ).text ).to.equal( 'r' );
  24. expect( nodeList.get( 7 ).text ).to.equal( 'f' );
  25. expect( nodeList.get( 8 ).text ).to.equal( 'o' );
  26. expect( nodeList.get( 9 ).text ).to.equal( 'o' );
  27. } );
  28. it( 'should omit empty strings / texts', () => {
  29. let nodeList = new NodeList( [ 'fo', '', 'ob', new Text( '', [ new Attribute( 'foo', true ) ] ), 'ar' ] );
  30. expect( nodeList.length ).to.equal( 6 );
  31. expect( nodeList.get( 0 ).text ).to.equal( 'f' );
  32. expect( nodeList.get( 1 ).text ).to.equal( 'o' );
  33. expect( nodeList.get( 2 ).text ).to.equal( 'o' );
  34. expect( nodeList.get( 3 ).text ).to.equal( 'b' );
  35. expect( nodeList.get( 4 ).text ).to.equal( 'a' );
  36. expect( nodeList.get( 5 ).text ).to.equal( 'r' );
  37. expect( nodeList.get( 0 ).attrs.size ).to.equal( 0 );
  38. expect( nodeList.get( 1 ).attrs.size ).to.equal( 0 );
  39. expect( nodeList.get( 2 ).attrs.size ).to.equal( 0 );
  40. expect( nodeList.get( 3 ).attrs.size ).to.equal( 0 );
  41. expect( nodeList.get( 4 ).attrs.size ).to.equal( 0 );
  42. expect( nodeList.get( 5 ).attrs.size ).to.equal( 0 );
  43. } );
  44. it( 'should change string into a set of nodes', () => {
  45. let nodeList = new NodeList( 'foo' );
  46. expect( nodeList.length ).to.equal( 3 );
  47. expect( nodeList.get( 0 ).text ).to.equal( 'f' );
  48. expect( nodeList.get( 1 ).text ).to.equal( 'o' );
  49. expect( nodeList.get( 2 ).text ).to.equal( 'o' );
  50. } );
  51. it( 'should change node into a set of nodes', () => {
  52. let nodeList = new NodeList( new Text( 'x' ) );
  53. expect( nodeList.length ).to.equal( 1 );
  54. expect( nodeList.get( 0 ).text ).to.equal( 'x' );
  55. } );
  56. it( 'should change text with attribute into a set of nodes', () => {
  57. let attr = new Attribute( 'bold', true );
  58. let nodeList = new NodeList( new Text( 'foo', [ attr ] ) );
  59. expect( nodeList.length ).to.equal( 3 );
  60. expect( nodeList.get( 0 ).text ).to.equal( 'f' );
  61. expect( nodeList.get( 0 ).attrs.getValue( attr.key ) ).to.equal( attr.value );
  62. expect( nodeList.get( 1 ).text ).to.equal( 'o' );
  63. expect( nodeList.get( 1 ).attrs.getValue( attr.key ) ).to.equal( attr.value );
  64. expect( nodeList.get( 2 ).text ).to.equal( 'o' );
  65. expect( nodeList.get( 2 ).attrs.getValue( attr.key ) ).to.equal( attr.value );
  66. } );
  67. it( 'should merge strings and text objects if possible', () => {
  68. let attr = new Attribute( 'foo', 'bar' );
  69. let nodeList = new NodeList( [ 'fo', new Text( 'o' ), new Text( 'x', [ attr ] ), new Text( 'y', [ attr ] ), 'bar' ] );
  70. expect( nodeList.length ).to.equal( 8 );
  71. expect( nodeList.get( 0 ).text ).to.equal( 'f' );
  72. expect( nodeList.get( 1 ).text ).to.equal( 'o' );
  73. expect( nodeList.get( 2 ).text ).to.equal( 'o' );
  74. expect( nodeList.get( 3 ).text ).to.equal( 'x' );
  75. expect( nodeList.get( 4 ).text ).to.equal( 'y' );
  76. expect( nodeList.get( 5 ).text ).to.equal( 'b' );
  77. expect( nodeList.get( 6 ).text ).to.equal( 'a' );
  78. expect( nodeList.get( 7 ).text ).to.equal( 'r' );
  79. expect( nodeList._nodes.length ).to.equal( 3 );
  80. expect( nodeList._nodes[ 0 ].text ).to.equal( 'foo' );
  81. expect( nodeList._nodes[ 1 ].text ).to.equal( 'xy' );
  82. expect( nodeList._nodes[ 2 ].text ).to.equal( 'bar' );
  83. } );
  84. } );
  85. describe( 'insert', () => {
  86. it( 'should insert one node list into another', () => {
  87. let outerList = new NodeList( 'foo' );
  88. let innerList = new NodeList( 'xxx' );
  89. outerList.insert( 2, innerList );
  90. expect( outerList.length ).to.equal( 6 );
  91. expect( outerList.get( 0 ).text ).to.equal( 'f' );
  92. expect( outerList.get( 1 ).text ).to.equal( 'o' );
  93. expect( outerList.get( 2 ).text ).to.equal( 'x' );
  94. expect( outerList.get( 3 ).text ).to.equal( 'x' );
  95. expect( outerList.get( 4 ).text ).to.equal( 'x' );
  96. expect( outerList.get( 5 ).text ).to.equal( 'o' );
  97. } );
  98. it( 'should merge inserted text objects if possible', () => {
  99. let attr = new Attribute( 'foo', 'bar' );
  100. let outerList = new NodeList( [ 'foo', new Text( 'bar', [ attr ] ) ] );
  101. let innerList = new NodeList( [ 'x' , new Text( 'y', [ attr ] ) ] );
  102. outerList.insert( 3, innerList );
  103. expect( outerList._nodes.length ).to.equal( 2 );
  104. expect( outerList._nodes[ 0 ].text ).to.equal( 'foox' );
  105. expect( outerList._nodes[ 1 ].text ).to.equal( 'ybar' );
  106. } );
  107. } );
  108. describe( 'remove', () => {
  109. it( 'should remove part of the node list and return removed nodes as another node list', () => {
  110. let nodeList = new NodeList( 'foobar' );
  111. let removed = nodeList.remove( 2, 3 );
  112. expect( nodeList.length ).to.equal( 3 );
  113. expect( nodeList.get( 0 ).text ).to.equal( 'f' );
  114. expect( nodeList.get( 1 ).text ).to.equal( 'o' );
  115. expect( nodeList.get( 2 ).text ).to.equal( 'r' );
  116. expect( removed ).to.be.instanceof( NodeList );
  117. expect( removed.length ).to.equal( 3 );
  118. expect( removed.get( 0 ).text ).to.equal( 'o' );
  119. expect( removed.get( 1 ).text ).to.equal( 'b' );
  120. expect( removed.get( 2 ).text ).to.equal( 'a' );
  121. } );
  122. it( 'should merge text objects left in node list possible', () => {
  123. let attr = new Attribute( 'foo', 'bar' );
  124. let nodeList = new NodeList( [ 'foo', new Text( 'xxx', [ attr ] ), 'bar' ] );
  125. nodeList.remove( 2, 5 );
  126. expect( nodeList._nodes.length ).to.equal( 1 );
  127. expect( nodeList._nodes[ 0 ].text ).to.equal( 'foar' );
  128. } );
  129. it( 'should return empty node list and do nothing if node list removed from is also empty', () => {
  130. let nodeList = new NodeList();
  131. let result = nodeList.remove( 2, 3 );
  132. expect( result.length ).to.equal( 0 );
  133. } );
  134. } );
  135. describe( 'indexOf', () => {
  136. let nodeList, p;
  137. beforeEach( () => {
  138. p = new Element( 'p' );
  139. nodeList = new NodeList( [ 'abc', p, 'def' ] );
  140. } );
  141. it( 'should return index of specified element', () => {
  142. let index = nodeList.indexOf( p );
  143. expect( index ).to.equal( 3 );
  144. } );
  145. it( 'should return index of specified text node', () => {
  146. let textNode = nodeList.get( 5 );
  147. let index = nodeList.indexOf( textNode );
  148. expect( index ).to.equal( 5 );
  149. } );
  150. it( 'should return -1 if specified element is not a part of a node list', () => {
  151. expect( nodeList.indexOf( new Element( 'p' ) ) ).to.equal( -1 );
  152. } );
  153. it( 'should return -1 if specified text node is not a part of a node list', () => {
  154. let text = new Text( 'foobar' );
  155. let textNode = text.getTextNode( 2, 2 );
  156. expect( nodeList.indexOf( textNode ) ).to.equal( -1 );
  157. } );
  158. } );
  159. describe( 'iterator', () => {
  160. it( 'should iterate over all elements in the collection', () => {
  161. let characters = 'foo';
  162. let nodeList = new NodeList( characters );
  163. let i = 0;
  164. for ( let node of nodeList ) {
  165. expect( node.text ).to.equal( characters[ i ] );
  166. i++;
  167. }
  168. expect( i ).to.equal( 3 );
  169. } );
  170. } );
  171. describe( '_splitNodeAt', () => {
  172. it( 'should split text object into two text objects', () => {
  173. let nodeList = new NodeList( 'abcd' );
  174. nodeList._splitNodeAt( 2 );
  175. expect( nodeList._nodes.length ).to.equal( 2 );
  176. expect( nodeList._nodes[ 0 ].text ).to.equal( 'ab' );
  177. expect( nodeList._nodes[ 1 ].text ).to.equal( 'cd' );
  178. } );
  179. it( 'should do nothing if node before and after index are different', () => {
  180. let nodeList = new NodeList( [ new Text( 'ab', [ new Attribute( 'foo', true ) ] ), 'cd' ] );
  181. nodeList._splitNodeAt( 2 );
  182. expect( nodeList._nodes.length ).to.equal( 2 );
  183. expect( nodeList._nodes[ 0 ].text ).to.equal( 'ab' );
  184. expect( nodeList._nodes[ 1 ].text ).to.equal( 'cd' );
  185. } );
  186. } );
  187. describe( '_mergeNodeAt', () => {
  188. it( 'should merge two text object if they have same attributes', () => {
  189. let attr = new Attribute( 'foo', true );
  190. let nodeList = new NodeList( [ 'ab', new Text( 'cd', [ attr ] ) ] );
  191. nodeList._nodes[ 1 ].attrs.delete( attr.key );
  192. expect( nodeList._nodes.length ).to.equal( 2 );
  193. nodeList._mergeNodeAt( 2 );
  194. expect( nodeList._nodes.length ).to.equal( 1 );
  195. expect( nodeList._nodes[ 0 ].text ).to.equal( 'abcd' );
  196. } );
  197. it( 'should do nothing if text objects has different attributes', () => {
  198. let nodeList = new NodeList( [ new Text( 'ab', [ new Attribute( 'foo', true ) ] ), 'cd' ] );
  199. nodeList._mergeNodeAt( 2 );
  200. expect( nodeList._nodes.length ).to.equal( 2 );
  201. expect( nodeList._nodes[ 0 ].text ).to.equal( 'ab' );
  202. expect( nodeList._nodes[ 1 ].text ).to.equal( 'cd' );
  203. } );
  204. } );
  205. describe( '_getCharIndex', () => {
  206. it( 'should return offset of given index from the beginning of the text node', () => {
  207. let nodeList = new NodeList( [ new Text( 'ab', [ new Attribute( 'foo', true ) ] ), 'cd' ] );
  208. let charIndexC = nodeList._getCharIndex( 2 );
  209. let charIndexD = nodeList._getCharIndex( 3 );
  210. expect( charIndexC ).to.equal( 0 );
  211. expect( charIndexD ).to.equal( 1 );
  212. } );
  213. } );
  214. } );