8
0

nodelist.js 13 KB

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