8
0

nodelist.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. it( 'should accept DocumentFragment as one of items in input array', () => {
  100. let p1 = new Element( 'p' );
  101. let p2 = new Element( 'p' );
  102. let p3 = new Element( 'p' );
  103. let frag = new DocumentFragment( [ p1, p2 ] );
  104. let nodeList = new NodeList( [ frag, p3 ] );
  105. expect( nodeList.length ).to.equal( 3 );
  106. expect( nodeList.get( 0 ) ).to.equal( p1 );
  107. expect( nodeList.get( 1 ) ).to.equal( p2 );
  108. expect( nodeList.get( 2 ) ).to.equal( p3 );
  109. } );
  110. } );
  111. describe( 'insert', () => {
  112. it( 'should insert one node list into another', () => {
  113. let outerList = new NodeList( 'foo' );
  114. let innerList = new NodeList( 'xxx' );
  115. outerList.insert( 2, innerList );
  116. expect( outerList.length ).to.equal( 6 );
  117. expect( outerList.get( 0 ).character ).to.equal( 'f' );
  118. expect( outerList.get( 1 ).character ).to.equal( 'o' );
  119. expect( outerList.get( 2 ).character ).to.equal( 'x' );
  120. expect( outerList.get( 3 ).character ).to.equal( 'x' );
  121. expect( outerList.get( 4 ).character ).to.equal( 'x' );
  122. expect( outerList.get( 5 ).character ).to.equal( 'o' );
  123. } );
  124. it( 'should merge inserted text objects if possible', () => {
  125. let attr = { foo: 'bar' };
  126. let outerList = new NodeList( [ 'foo', new Text( 'bar', [ attr ] ) ] );
  127. let innerList = new NodeList( [ 'x' , new Text( 'y', [ attr ] ) ] );
  128. outerList.insert( 3, innerList );
  129. expect( outerList._nodes.length ).to.equal( 2 );
  130. expect( outerList._nodes[ 0 ].text ).to.equal( 'foox' );
  131. expect( outerList._nodes[ 1 ].text ).to.equal( 'ybar' );
  132. } );
  133. } );
  134. describe( 'remove', () => {
  135. it( 'should remove part of the node list and return removed nodes as another node list', () => {
  136. let nodeList = new NodeList( 'foobar' );
  137. let removed = nodeList.remove( 2, 3 );
  138. expect( nodeList.length ).to.equal( 3 );
  139. expect( nodeList.get( 0 ).character ).to.equal( 'f' );
  140. expect( nodeList.get( 1 ).character ).to.equal( 'o' );
  141. expect( nodeList.get( 2 ).character ).to.equal( 'r' );
  142. expect( removed ).to.be.instanceof( NodeList );
  143. expect( removed.length ).to.equal( 3 );
  144. expect( removed.get( 0 ).character ).to.equal( 'o' );
  145. expect( removed.get( 1 ).character ).to.equal( 'b' );
  146. expect( removed.get( 2 ).character ).to.equal( 'a' );
  147. } );
  148. it( 'should merge text objects left in node list possible', () => {
  149. let attr = { foo: 'bar' };
  150. let nodeList = new NodeList( [ 'foo', new Text( 'xxx', [ attr ] ), 'bar' ] );
  151. nodeList.remove( 2, 5 );
  152. expect( nodeList._nodes.length ).to.equal( 1 );
  153. expect( nodeList._nodes[ 0 ].text ).to.equal( 'foar' );
  154. } );
  155. it( 'should return empty node list and do nothing if node list removed from is also empty', () => {
  156. let nodeList = new NodeList();
  157. let result = nodeList.remove( 2, 3 );
  158. expect( result.length ).to.equal( 0 );
  159. } );
  160. } );
  161. describe( 'indexOf', () => {
  162. let nodeList, p;
  163. beforeEach( () => {
  164. p = new Element( 'p' );
  165. nodeList = new NodeList( [ 'abc', p, 'def' ] );
  166. } );
  167. it( 'should return index of specified element', () => {
  168. let index = nodeList.indexOf( p );
  169. expect( index ).to.equal( 3 );
  170. } );
  171. it( 'should return index of specified character', () => {
  172. let char = nodeList.get( 5 );
  173. let index = nodeList.indexOf( char );
  174. expect( index ).to.equal( 5 );
  175. } );
  176. it( 'should return -1 if specified element is not a part of a node list', () => {
  177. expect( nodeList.indexOf( new Element( 'p' ) ) ).to.equal( -1 );
  178. } );
  179. it( 'should return -1 if specified character is not a part of a node list', () => {
  180. let div = new Element( 'div', [], 'a' );
  181. let char = div.getChild( 0 );
  182. expect( nodeList.indexOf( char ) ).to.equal( -1 );
  183. } );
  184. } );
  185. describe( 'iterator', () => {
  186. it( 'should iterate over all elements in the collection', () => {
  187. let characters = 'foo';
  188. let nodeList = new NodeList( characters );
  189. let i = 0;
  190. for ( let node of nodeList ) {
  191. expect( node.character ).to.equal( characters[ i ] );
  192. i++;
  193. }
  194. expect( i ).to.equal( 3 );
  195. } );
  196. } );
  197. describe( 'setAttribute', () => {
  198. it( 'should change attribute for multiple items in node list but not for their children', () => {
  199. let p = new Element( 'p', [], 'x' );
  200. let div = new Element( 'div' );
  201. let nodeList = new NodeList( [ p, 'foo', div, 'bar' ] );
  202. nodeList.setAttribute( 0, 6, 'a', 'true' );
  203. // Attribute set.
  204. expect( p.hasAttribute( 'a' ) ).to.be.true;
  205. expect( nodeList.get( 1 ).hasAttribute( 'a' ) ).to.be.true;
  206. expect( nodeList.get( 2 ).hasAttribute( 'a' ) ).to.be.true;
  207. expect( nodeList.get( 3 ).hasAttribute( 'a' ) ).to.be.true;
  208. expect( div.hasAttribute( 'a' ) ).to.be.true;
  209. expect( nodeList.get( 5 ).hasAttribute( 'a' ) ).to.be.true;
  210. expect( nodeList.get( 6 ).hasAttribute( 'a' ) ).to.be.false;
  211. expect( nodeList.get( 7 ).hasAttribute( 'a' ) ).to.be.false;
  212. // Attribute not set for children.
  213. expect( p.getChild( 0 ).hasAttribute( 'a' ) ).to.be.false;
  214. } );
  215. it( 'should remove attribute if no new attribute has been passed', () => {
  216. let p = new Element( 'p', { a: true } );
  217. let text = new Text( 'foobar', { a: true } );
  218. let nodeList = new NodeList( [ p, text ] );
  219. nodeList.setAttribute( 0, 4, 'a', null );
  220. expect( p.hasAttribute( 'a' ) ).to.be.false;
  221. expect( nodeList.get( 1 ).hasAttribute( 'a' ) ).to.be.false;
  222. expect( nodeList.get( 2 ).hasAttribute( 'a' ) ).to.be.false;
  223. expect( nodeList.get( 3 ).hasAttribute( 'a' ) ).to.be.false;
  224. expect( nodeList.get( 4 ).hasAttribute( 'a' ) ).to.be.true;
  225. expect( nodeList.get( 5 ).hasAttribute( 'a' ) ).to.be.true;
  226. expect( nodeList._nodes.length ).to.equal( 3 );
  227. } );
  228. it( 'should throw if wrong index or number is passed', () => {
  229. let attr = { a: true };
  230. let text = new Text( 'foo', [ attr ] );
  231. let nodeList = new NodeList( text );
  232. expect( () => {
  233. nodeList.setAttribute( -1, 2, attr.key, null );
  234. } ).to.throw( CKEditorError, /nodelist-setattribute-out-of-bounds/ );
  235. expect( () => {
  236. nodeList.setAttribute( 2, 2, attr.key, null );
  237. } ).to.throw( CKEditorError, /nodelist-setattribute-out-of-bounds/ );
  238. } );
  239. } );
  240. describe( '_splitNodeAt', () => {
  241. it( 'should split text object into two text objects', () => {
  242. let nodeList = new NodeList( 'abcd' );
  243. nodeList._splitNodeAt( 2 );
  244. expect( nodeList._nodes.length ).to.equal( 2 );
  245. expect( nodeList._nodes[ 0 ].text ).to.equal( 'ab' );
  246. expect( nodeList._nodes[ 1 ].text ).to.equal( 'cd' );
  247. } );
  248. it( 'should do nothing if node before and after index are different', () => {
  249. let nodeList = new NodeList( [ new Text( 'ab', { foo: true } ), 'cd' ] );
  250. nodeList._splitNodeAt( 2 );
  251. expect( nodeList._nodes.length ).to.equal( 2 );
  252. expect( nodeList._nodes[ 0 ].text ).to.equal( 'ab' );
  253. expect( nodeList._nodes[ 1 ].text ).to.equal( 'cd' );
  254. } );
  255. } );
  256. describe( '_mergeNodeAt', () => {
  257. it( 'should merge two text object if they have same attributes', () => {
  258. let attr = { foo: true };
  259. let nodeList = new NodeList( [ 'ab', new Text( 'cd', [ attr ] ) ] );
  260. expect( nodeList._nodes.length ).to.equal( 2 );
  261. nodeList._nodes[ 1 ]._attrs.delete( attr.key );
  262. nodeList._mergeNodeAt( 2 );
  263. expect( nodeList._nodes.length ).to.equal( 1 );
  264. expect( nodeList._nodes[ 0 ].text ).to.equal( 'abcd' );
  265. } );
  266. it( 'should do nothing if text objects has different attributes', () => {
  267. let nodeList = new NodeList( [ new Text( 'ab', { foo: true } ), 'cd' ] );
  268. nodeList._mergeNodeAt( 2 );
  269. expect( nodeList._nodes.length ).to.equal( 2 );
  270. expect( nodeList._nodes[ 0 ].text ).to.equal( 'ab' );
  271. expect( nodeList._nodes[ 1 ].text ).to.equal( 'cd' );
  272. } );
  273. } );
  274. describe( '_getCharIndex', () => {
  275. it( 'should return offset of character at given index from the beginning of the NodeListText containing that character', () => {
  276. let nodeList = new NodeList( [ new Text( 'ab', { foo: true } ), 'cd' ] );
  277. let charIndexC = nodeList._getCharIndex( 2 );
  278. let charIndexD = nodeList._getCharIndex( 3 );
  279. expect( charIndexC ).to.equal( 0 );
  280. expect( charIndexD ).to.equal( 1 );
  281. } );
  282. } );
  283. // Additional test for code coverage.
  284. describe( 'NodeListText', () => {
  285. it( 'should create proper JSON string using toJSON method', () => {
  286. let nodeList = new NodeList( 'foo' );
  287. let parsed = JSON.parse( JSON.stringify( nodeList ) );
  288. expect( parsed._nodes[ 0 ].parent ).to.equal( null );
  289. } );
  290. } );
  291. } );