nodelist.js 10 KB

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