8
0

nodelist.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 CKEditorError from '/ckeditor5/core/ckeditorerror.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 = { 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 ).getAttribute( attr.key ) ).to.equal( attr.value );
  38. expect( nodeList.get( 1 ).character ).to.equal( 'o' );
  39. expect( nodeList.get( 1 ).getAttribute( attr.key ) ).to.equal( attr.value );
  40. expect( nodeList.get( 2 ).character ).to.equal( 'o' );
  41. expect( nodeList.get( 2 ).getAttribute( 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( '', { 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 = { 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 = { 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 = { 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( 'setAttribute', () => {
  177. it( 'should change attribute for multiple items in node list but not for their children', () => {
  178. let p = new Element( 'p', [], 'x' );
  179. let div = new Element( 'div' );
  180. let nodeList = new NodeList( [ p, 'foo', div, 'bar' ] );
  181. nodeList.setAttribute( 0, 6, 'a', 'true' );
  182. // Attribute set.
  183. expect( p.hasAttribute( 'a' ) ).to.be.true;
  184. expect( nodeList.get( 1 ).hasAttribute( 'a' ) ).to.be.true;
  185. expect( nodeList.get( 2 ).hasAttribute( 'a' ) ).to.be.true;
  186. expect( nodeList.get( 3 ).hasAttribute( 'a' ) ).to.be.true;
  187. expect( div.hasAttribute( 'a' ) ).to.be.true;
  188. expect( nodeList.get( 5 ).hasAttribute( 'a' ) ).to.be.true;
  189. expect( nodeList.get( 6 ).hasAttribute( 'a' ) ).to.be.false;
  190. expect( nodeList.get( 7 ).hasAttribute( 'a' ) ).to.be.false;
  191. // Attribute not set for children.
  192. expect( p.getChild( 0 ).hasAttribute( 'a' ) ).to.be.false;
  193. } );
  194. it( 'should remove attribute if no new attribute has been passed', () => {
  195. let p = new Element( 'p', { a: true } );
  196. let text = new Text( 'foobar', { a: true } );
  197. let nodeList = new NodeList( [ p, text ] );
  198. nodeList.setAttribute( 0, 4, 'a', null );
  199. expect( p.hasAttribute( 'a' ) ).to.be.false;
  200. expect( nodeList.get( 1 ).hasAttribute( 'a' ) ).to.be.false;
  201. expect( nodeList.get( 2 ).hasAttribute( 'a' ) ).to.be.false;
  202. expect( nodeList.get( 3 ).hasAttribute( 'a' ) ).to.be.false;
  203. expect( nodeList.get( 4 ).hasAttribute( 'a' ) ).to.be.true;
  204. expect( nodeList.get( 5 ).hasAttribute( 'a' ) ).to.be.true;
  205. expect( nodeList._nodes.length ).to.equal( 3 );
  206. } );
  207. it( 'should throw if wrong index or number is passed', () => {
  208. let attr = { a: true };
  209. let text = new Text( 'foo', [ attr ] );
  210. let nodeList = new NodeList( text );
  211. expect( () => {
  212. nodeList.setAttribute( -1, 2, attr.key, null );
  213. } ).to.throw( CKEditorError, /nodelist-setattribute-out-of-bounds/ );
  214. expect( () => {
  215. nodeList.setAttribute( 2, 2, attr.key, null );
  216. } ).to.throw( CKEditorError, /nodelist-setattribute-out-of-bounds/ );
  217. } );
  218. } );
  219. describe( '_splitNodeAt', () => {
  220. it( 'should split text object into two text objects', () => {
  221. let nodeList = new NodeList( 'abcd' );
  222. nodeList._splitNodeAt( 2 );
  223. expect( nodeList._nodes.length ).to.equal( 2 );
  224. expect( nodeList._nodes[ 0 ].text ).to.equal( 'ab' );
  225. expect( nodeList._nodes[ 1 ].text ).to.equal( 'cd' );
  226. } );
  227. it( 'should do nothing if node before and after index are different', () => {
  228. let nodeList = new NodeList( [ new Text( 'ab', { foo: true } ), 'cd' ] );
  229. nodeList._splitNodeAt( 2 );
  230. expect( nodeList._nodes.length ).to.equal( 2 );
  231. expect( nodeList._nodes[ 0 ].text ).to.equal( 'ab' );
  232. expect( nodeList._nodes[ 1 ].text ).to.equal( 'cd' );
  233. } );
  234. } );
  235. describe( '_mergeNodeAt', () => {
  236. it( 'should merge two text object if they have same attributes', () => {
  237. let attr = { foo: true };
  238. let nodeList = new NodeList( [ 'ab', new Text( 'cd', [ attr ] ) ] );
  239. expect( nodeList._nodes.length ).to.equal( 2 );
  240. nodeList._nodes[ 1 ]._attrs.delete( attr.key );
  241. nodeList._mergeNodeAt( 2 );
  242. expect( nodeList._nodes.length ).to.equal( 1 );
  243. expect( nodeList._nodes[ 0 ].text ).to.equal( 'abcd' );
  244. } );
  245. it( 'should do nothing if text objects has different attributes', () => {
  246. let nodeList = new NodeList( [ new Text( 'ab', { foo: true } ), 'cd' ] );
  247. nodeList._mergeNodeAt( 2 );
  248. expect( nodeList._nodes.length ).to.equal( 2 );
  249. expect( nodeList._nodes[ 0 ].text ).to.equal( 'ab' );
  250. expect( nodeList._nodes[ 1 ].text ).to.equal( 'cd' );
  251. } );
  252. } );
  253. describe( '_getCharIndex', () => {
  254. it( 'should return offset of character at given index from the beginning of the NodeListText containing that character', () => {
  255. let nodeList = new NodeList( [ new Text( 'ab', { foo: true } ), 'cd' ] );
  256. let charIndexC = nodeList._getCharIndex( 2 );
  257. let charIndexD = nodeList._getCharIndex( 3 );
  258. expect( charIndexC ).to.equal( 0 );
  259. expect( charIndexD ).to.equal( 1 );
  260. } );
  261. } );
  262. // Additional test for code coverage.
  263. describe( 'NodeListText', () => {
  264. it( 'should create proper JSON string using toJSON method', () => {
  265. let nodeList = new NodeList( 'foo' );
  266. let parsed = JSON.parse( JSON.stringify( nodeList ) );
  267. expect( parsed._nodes[ 0 ].parent ).to.equal( null );
  268. } );
  269. } );
  270. } );