8
0

nodelist.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. 'use strict';
  7. import NodeList from '/ckeditor5/engine/model/nodelist.js';
  8. import DocumentFragment from '/ckeditor5/engine/model/documentfragment.js';
  9. import Element from '/ckeditor5/engine/model/element.js';
  10. import Text from '/ckeditor5/engine/model/text.js';
  11. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  12. import { jsonParseStringify } from '/tests/engine/model/_utils/utils.js';
  13. describe( 'NodeList', () => {
  14. describe( 'constructor', () => {
  15. it( 'should add elements to the node list', () => {
  16. let p = new Element( 'p' );
  17. let nodeList = new NodeList( p );
  18. expect( nodeList.length ).to.equal( 1 );
  19. expect( nodeList.get( 0 ) ).to.equal( p );
  20. } );
  21. it( 'should change string into a set of nodes', () => {
  22. let nodeList = new NodeList( 'foo' );
  23. expect( nodeList.length ).to.equal( 3 );
  24. expect( nodeList.get( 0 ).character ).to.equal( 'f' );
  25. expect( nodeList.get( 1 ).character ).to.equal( 'o' );
  26. expect( nodeList.get( 2 ).character ).to.equal( 'o' );
  27. } );
  28. it( 'should change node into a set of nodes', () => {
  29. let nodeList = new NodeList( new Text( 'xy' ) );
  30. expect( nodeList.length ).to.equal( 2 );
  31. expect( nodeList.get( 0 ).character ).to.equal( 'x' );
  32. expect( nodeList.get( 1 ).character ).to.equal( 'y' );
  33. } );
  34. it( 'should change text with attribute into a set of nodes', () => {
  35. let attr = { bold: true };
  36. let nodeList = new NodeList( new Text( 'foo', [ attr ] ) );
  37. expect( nodeList.length ).to.equal( 3 );
  38. expect( nodeList.get( 0 ).character ).to.equal( 'f' );
  39. expect( nodeList.get( 0 ).getAttribute( attr.key ) ).to.equal( attr.value );
  40. expect( nodeList.get( 1 ).character ).to.equal( 'o' );
  41. expect( nodeList.get( 1 ).getAttribute( attr.key ) ).to.equal( attr.value );
  42. expect( nodeList.get( 2 ).character ).to.equal( 'o' );
  43. expect( nodeList.get( 2 ).getAttribute( attr.key ) ).to.equal( attr.value );
  44. } );
  45. it( 'should change array of characters into a set of nodes', () => {
  46. let char = new Element( 'p', [], 'y' ).getChild( 0 );
  47. let nodeList = new NodeList( [ 'foo', new Text( 'x' ), char, 'bar' ] );
  48. expect( nodeList.length ).to.equal( 8 );
  49. expect( nodeList.get( 0 ).character ).to.equal( 'f' );
  50. expect( nodeList.get( 1 ).character ).to.equal( 'o' );
  51. expect( nodeList.get( 2 ).character ).to.equal( 'o' );
  52. expect( nodeList.get( 3 ).character ).to.equal( 'x' );
  53. expect( nodeList.get( 4 ).character ).to.equal( 'y' );
  54. expect( nodeList.get( 5 ).character ).to.equal( 'b' );
  55. expect( nodeList.get( 6 ).character ).to.equal( 'a' );
  56. expect( nodeList.get( 7 ).character ).to.equal( 'r' );
  57. } );
  58. it( 'should omit empty strings / texts', () => {
  59. let nodeList = new NodeList( [ 'fo', '', 'ob', new Text( '', { foo: true } ), 'ar' ] );
  60. expect( nodeList.length ).to.equal( 6 );
  61. expect( nodeList.get( 0 ).character ).to.equal( 'f' );
  62. expect( nodeList.get( 1 ).character ).to.equal( 'o' );
  63. expect( nodeList.get( 2 ).character ).to.equal( 'o' );
  64. expect( nodeList.get( 3 ).character ).to.equal( 'b' );
  65. expect( nodeList.get( 4 ).character ).to.equal( 'a' );
  66. expect( nodeList.get( 5 ).character ).to.equal( 'r' );
  67. expect( nodeList.get( 0 )._attrs.size ).to.equal( 0 );
  68. expect( nodeList.get( 1 )._attrs.size ).to.equal( 0 );
  69. expect( nodeList.get( 2 )._attrs.size ).to.equal( 0 );
  70. expect( nodeList.get( 3 )._attrs.size ).to.equal( 0 );
  71. expect( nodeList.get( 4 )._attrs.size ).to.equal( 0 );
  72. expect( nodeList.get( 5 )._attrs.size ).to.equal( 0 );
  73. } );
  74. it( 'should merge strings and text objects if possible', () => {
  75. let attr = { foo: 'bar' };
  76. let nodeList = new NodeList( [ 'fo', new Text( 'o' ), new Text( 'x', [ attr ] ), new Text( 'y', [ attr ] ), 'bar' ] );
  77. expect( nodeList.length ).to.equal( 8 );
  78. expect( nodeList.get( 0 ).character ).to.equal( 'f' );
  79. expect( nodeList.get( 1 ).character ).to.equal( 'o' );
  80. expect( nodeList.get( 2 ).character ).to.equal( 'o' );
  81. expect( nodeList.get( 3 ).character ).to.equal( 'x' );
  82. expect( nodeList.get( 4 ).character ).to.equal( 'y' );
  83. expect( nodeList.get( 5 ).character ).to.equal( 'b' );
  84. expect( nodeList.get( 6 ).character ).to.equal( 'a' );
  85. expect( nodeList.get( 7 ).character ).to.equal( 'r' );
  86. expect( nodeList._nodes.length ).to.equal( 3 );
  87. expect( nodeList._nodes[ 0 ].text ).to.equal( 'foo' );
  88. expect( nodeList._nodes[ 1 ].text ).to.equal( 'xy' );
  89. expect( nodeList._nodes[ 2 ].text ).to.equal( 'bar' );
  90. } );
  91. it( 'should accept DocumentFragment as a parameter', () => {
  92. let p1 = new Element( 'p' );
  93. let p2 = new Element( 'p' );
  94. let frag = new DocumentFragment( [ p1, p2 ] );
  95. let nodeList = new NodeList( frag );
  96. expect( nodeList.length ).to.equal( 2 );
  97. expect( nodeList.get( 0 ) ).to.equal( p1 );
  98. expect( nodeList.get( 1 ) ).to.equal( p2 );
  99. } );
  100. it( 'should accept DocumentFragment as one of items in input array', () => {
  101. let p1 = new Element( 'p' );
  102. let p2 = new Element( 'p' );
  103. let p3 = new Element( 'p' );
  104. let frag = new DocumentFragment( [ p1, p2 ] );
  105. let nodeList = new NodeList( [ frag, p3 ] );
  106. expect( nodeList.length ).to.equal( 3 );
  107. expect( nodeList.get( 0 ) ).to.equal( p1 );
  108. expect( nodeList.get( 1 ) ).to.equal( p2 );
  109. expect( nodeList.get( 2 ) ).to.equal( p3 );
  110. } );
  111. } );
  112. describe( 'insert', () => {
  113. it( 'should insert one node list into another', () => {
  114. let outerList = new NodeList( 'foo' );
  115. let innerList = new NodeList( 'xxx' );
  116. outerList.insert( 2, innerList );
  117. expect( outerList.length ).to.equal( 6 );
  118. expect( outerList.get( 0 ).character ).to.equal( 'f' );
  119. expect( outerList.get( 1 ).character ).to.equal( 'o' );
  120. expect( outerList.get( 2 ).character ).to.equal( 'x' );
  121. expect( outerList.get( 3 ).character ).to.equal( 'x' );
  122. expect( outerList.get( 4 ).character ).to.equal( 'x' );
  123. expect( outerList.get( 5 ).character ).to.equal( 'o' );
  124. } );
  125. it( 'should merge inserted text objects if possible', () => {
  126. let attr = { foo: 'bar' };
  127. let outerList = new NodeList( [ 'foo', new Text( 'bar', [ attr ] ) ] );
  128. let innerList = new NodeList( [ 'x', new Text( 'y', [ attr ] ) ] );
  129. outerList.insert( 3, innerList );
  130. expect( outerList._nodes.length ).to.equal( 2 );
  131. expect( outerList._nodes[ 0 ].text ).to.equal( 'foox' );
  132. expect( outerList._nodes[ 1 ].text ).to.equal( 'ybar' );
  133. } );
  134. } );
  135. describe( 'remove', () => {
  136. it( 'should remove part of the node list and return removed nodes as another node list', () => {
  137. let nodeList = new NodeList( 'foobar' );
  138. let removed = nodeList.remove( 2, 3 );
  139. expect( nodeList.length ).to.equal( 3 );
  140. expect( nodeList.get( 0 ).character ).to.equal( 'f' );
  141. expect( nodeList.get( 1 ).character ).to.equal( 'o' );
  142. expect( nodeList.get( 2 ).character ).to.equal( 'r' );
  143. expect( removed ).to.be.instanceof( NodeList );
  144. expect( removed.length ).to.equal( 3 );
  145. expect( removed.get( 0 ).character ).to.equal( 'o' );
  146. expect( removed.get( 1 ).character ).to.equal( 'b' );
  147. expect( removed.get( 2 ).character ).to.equal( 'a' );
  148. } );
  149. it( 'should merge text objects left in node list possible', () => {
  150. let attr = { foo: 'bar' };
  151. let nodeList = new NodeList( [ 'foo', new Text( 'xxx', [ attr ] ), 'bar' ] );
  152. nodeList.remove( 2, 5 );
  153. expect( nodeList._nodes.length ).to.equal( 1 );
  154. expect( nodeList._nodes[ 0 ].text ).to.equal( 'foar' );
  155. } );
  156. it( 'should return empty node list and do nothing if node list removed from is also empty', () => {
  157. let nodeList = new NodeList();
  158. let result = nodeList.remove( 2, 3 );
  159. expect( result.length ).to.equal( 0 );
  160. } );
  161. } );
  162. describe( 'indexOf', () => {
  163. let nodeList, p;
  164. beforeEach( () => {
  165. p = new Element( 'p' );
  166. nodeList = new NodeList( [ 'abc', p, 'def' ] );
  167. } );
  168. it( 'should return index of specified element', () => {
  169. let index = nodeList.indexOf( p );
  170. expect( index ).to.equal( 3 );
  171. } );
  172. it( 'should return index of specified character', () => {
  173. let char = nodeList.get( 5 );
  174. let index = nodeList.indexOf( char );
  175. expect( index ).to.equal( 5 );
  176. } );
  177. it( 'should return -1 if specified element is not a part of a node list', () => {
  178. expect( nodeList.indexOf( new Element( 'p' ) ) ).to.equal( -1 );
  179. } );
  180. it( 'should return -1 if specified character is not a part of a node list', () => {
  181. let div = new Element( 'div', [], 'a' );
  182. let char = div.getChild( 0 );
  183. expect( nodeList.indexOf( char ) ).to.equal( -1 );
  184. } );
  185. } );
  186. describe( 'iterator', () => {
  187. it( 'should iterate over all elements in the collection', () => {
  188. let characters = 'foo';
  189. let nodeList = new NodeList( characters );
  190. let i = 0;
  191. for ( let node of nodeList ) {
  192. expect( node.character ).to.equal( characters[ i ] );
  193. i++;
  194. }
  195. expect( i ).to.equal( 3 );
  196. } );
  197. } );
  198. describe( 'setAttribute', () => {
  199. it( 'should change attribute for multiple items in node list but not for their children', () => {
  200. let p = new Element( 'p', [], 'x' );
  201. let div = new Element( 'div' );
  202. let nodeList = new NodeList( [ p, 'foo', div, 'bar' ] );
  203. nodeList.setAttribute( 0, 6, 'a', 'true' );
  204. // Attribute set.
  205. expect( p.hasAttribute( 'a' ) ).to.be.true;
  206. expect( nodeList.get( 1 ).hasAttribute( 'a' ) ).to.be.true;
  207. expect( nodeList.get( 2 ).hasAttribute( 'a' ) ).to.be.true;
  208. expect( nodeList.get( 3 ).hasAttribute( 'a' ) ).to.be.true;
  209. expect( div.hasAttribute( 'a' ) ).to.be.true;
  210. expect( nodeList.get( 5 ).hasAttribute( 'a' ) ).to.be.true;
  211. expect( nodeList.get( 6 ).hasAttribute( 'a' ) ).to.be.false;
  212. expect( nodeList.get( 7 ).hasAttribute( 'a' ) ).to.be.false;
  213. // Attribute not set for children.
  214. expect( p.getChild( 0 ).hasAttribute( 'a' ) ).to.be.false;
  215. } );
  216. it( 'should remove attribute if no new attribute has been passed', () => {
  217. let p = new Element( 'p', { a: true } );
  218. let text = new Text( 'foobar', { a: true } );
  219. let nodeList = new NodeList( [ p, text ] );
  220. nodeList.setAttribute( 0, 4, 'a', null );
  221. expect( p.hasAttribute( 'a' ) ).to.be.false;
  222. expect( nodeList.get( 1 ).hasAttribute( 'a' ) ).to.be.false;
  223. expect( nodeList.get( 2 ).hasAttribute( 'a' ) ).to.be.false;
  224. expect( nodeList.get( 3 ).hasAttribute( 'a' ) ).to.be.false;
  225. expect( nodeList.get( 4 ).hasAttribute( 'a' ) ).to.be.true;
  226. expect( nodeList.get( 5 ).hasAttribute( 'a' ) ).to.be.true;
  227. expect( nodeList._nodes.length ).to.equal( 3 );
  228. } );
  229. it( 'should throw if wrong index or number is passed', () => {
  230. let attr = { a: true };
  231. let text = new Text( 'foo', [ attr ] );
  232. let nodeList = new NodeList( text );
  233. expect( () => {
  234. nodeList.setAttribute( -1, 2, attr.key, null );
  235. } ).to.throw( CKEditorError, /nodelist-setattribute-out-of-bounds/ );
  236. expect( () => {
  237. nodeList.setAttribute( 2, 2, attr.key, null );
  238. } ).to.throw( CKEditorError, /nodelist-setattribute-out-of-bounds/ );
  239. } );
  240. } );
  241. describe( '_splitNodeAt', () => {
  242. it( 'should split text object into two text objects', () => {
  243. let nodeList = new NodeList( 'abcd' );
  244. nodeList._splitNodeAt( 2 );
  245. expect( nodeList._nodes.length ).to.equal( 2 );
  246. expect( nodeList._nodes[ 0 ].text ).to.equal( 'ab' );
  247. expect( nodeList._nodes[ 1 ].text ).to.equal( 'cd' );
  248. } );
  249. it( 'should do nothing if node before and after index are different', () => {
  250. let nodeList = new NodeList( [ new Text( 'ab', { foo: true } ), 'cd' ] );
  251. nodeList._splitNodeAt( 2 );
  252. expect( nodeList._nodes.length ).to.equal( 2 );
  253. expect( nodeList._nodes[ 0 ].text ).to.equal( 'ab' );
  254. expect( nodeList._nodes[ 1 ].text ).to.equal( 'cd' );
  255. } );
  256. } );
  257. describe( '_mergeNodeAt', () => {
  258. it( 'should merge two text object if they have same attributes', () => {
  259. let attr = { foo: true };
  260. let nodeList = new NodeList( [ 'ab', new Text( 'cd', [ attr ] ) ] );
  261. expect( nodeList._nodes.length ).to.equal( 2 );
  262. nodeList._nodes[ 1 ]._attrs.delete( attr.key );
  263. nodeList._mergeNodeAt( 2 );
  264. expect( nodeList._nodes.length ).to.equal( 1 );
  265. expect( nodeList._nodes[ 0 ].text ).to.equal( 'abcd' );
  266. } );
  267. it( 'should do nothing if text objects has different attributes', () => {
  268. let nodeList = new NodeList( [ new Text( 'ab', { foo: true } ), 'cd' ] );
  269. nodeList._mergeNodeAt( 2 );
  270. expect( nodeList._nodes.length ).to.equal( 2 );
  271. expect( nodeList._nodes[ 0 ].text ).to.equal( 'ab' );
  272. expect( nodeList._nodes[ 1 ].text ).to.equal( 'cd' );
  273. } );
  274. } );
  275. describe( '_getCharIndex', () => {
  276. it( 'should return offset of character at given index from the beginning of the NodeListText containing that character', () => {
  277. let nodeList = new NodeList( [ new Text( 'ab', { foo: true } ), 'cd' ] );
  278. let charIndexC = nodeList._getCharIndex( 2 );
  279. let charIndexD = nodeList._getCharIndex( 3 );
  280. expect( charIndexC ).to.equal( 0 );
  281. expect( charIndexD ).to.equal( 1 );
  282. } );
  283. } );
  284. describe( 'toJSON', () => {
  285. it( 'should return serialized empty object', () => {
  286. let nodeList = new NodeList();
  287. expect( jsonParseStringify( nodeList ) ).to.deep.equal( {} );
  288. } );
  289. it( 'should return serialized object', () => {
  290. let p = new Element( 'p' );
  291. let nodeList = new NodeList( p );
  292. expect( jsonParseStringify( nodeList ) ).to.deep.equal( { nodes: [ jsonParseStringify( p ) ] } );
  293. } );
  294. it( 'should return serialized object with child text', () => {
  295. let p = new Element( 'p', null, 'bar' );
  296. let nodeList = new NodeList( p );
  297. let newVar = {
  298. nodes: [ {
  299. children: { nodes: [ { text: 'bar' } ] },
  300. name: 'p'
  301. } ]
  302. };
  303. expect( jsonParseStringify( nodeList ) ).to.deep.equal( newVar );
  304. } );
  305. it( 'should return serialized object for text', () => {
  306. let text = new Text( 'bar' );
  307. let nodeList = new NodeList( text );
  308. expect( jsonParseStringify( nodeList ) ).to.deep.equal( { nodes: [ { text: 'bar' } ] } );
  309. } );
  310. it( 'should return serialized object for text with attributes', () => {
  311. let text = new Text( 'bar', { bold: true } );
  312. let nodeList = new NodeList( text );
  313. expect( jsonParseStringify( nodeList ) ).to.deep.equal( { nodes: [ { attributes: [ [ 'bold', true ] ], text: 'bar' } ] } );
  314. } );
  315. it( 'should return serialized object for text with attributes', () => {
  316. let text = new Text( 'bar', { bold: true } );
  317. let nodeList = new NodeList( text );
  318. expect( jsonParseStringify( nodeList ) ).to.deep.equal( {
  319. nodes: [ { attributes: [ [ 'bold', true ] ], text: 'bar' } ]
  320. } );
  321. } );
  322. } );
  323. describe( 'fromJSON', () => {
  324. it( 'should create instance from empty serialized element', () => {
  325. let nodeList = new NodeList();
  326. let serialized = jsonParseStringify( nodeList );
  327. let deserialized = NodeList.fromJSON( serialized );
  328. expect( deserialized.length ).to.equal( nodeList.length );
  329. } );
  330. it( 'should create instance from serialized text with attributes', () => {
  331. let text = new Text( 'bar', { bold: true } );
  332. let nodeList = new NodeList( text );
  333. let serialized = jsonParseStringify( nodeList );
  334. let deserialized = NodeList.fromJSON( serialized );
  335. expect( deserialized.length ).to.equal( nodeList.length );
  336. for ( let i = 0; i < 3; i++ ) {
  337. expect( deserialized.get( i ).character ).to.equal( nodeList.get( i ).character );
  338. expect( deserialized.get( i ).hasAttribute( 'bold' ) ).to.equal( nodeList.get( i ).hasAttribute( 'bold' ) );
  339. expect( deserialized.get( i ).getAttribute( 'bold' ) ).to.equal( nodeList.get( i ).getAttribute( 'bold' ) );
  340. }
  341. } );
  342. it( 'should create instance from serialized element', () => {
  343. let p = new Element( 'p' );
  344. let nodeList = new NodeList( p );
  345. let serialized = jsonParseStringify( nodeList );
  346. let deserialized = NodeList.fromJSON( serialized );
  347. expect( deserialized.length ).to.equal( nodeList.length );
  348. expect( deserialized.get( 0 ).name ).to.deep.equal( nodeList.get( 0 ).name );
  349. } );
  350. it( 'should create instance from serialized element with attributes', () => {
  351. let p = new Element( 'p', { bold: true } );
  352. let nodeList = new NodeList( p );
  353. let serialized = jsonParseStringify( nodeList );
  354. let deserialized = NodeList.fromJSON( serialized );
  355. expect( deserialized.length ).to.equal( nodeList.length );
  356. expect( deserialized.get( 0 ).name ).to.deep.equal( nodeList.get( 0 ).name );
  357. expect( deserialized.get( 0 ).hasAttribute( 'bold' ) ).to.equal( nodeList.get( 0 ).hasAttribute( 'bold' ) );
  358. expect( deserialized.get( 0 ).getAttribute( 'bold' ) ).to.equal( nodeList.get( 0 ).getAttribute( 'bold' ) );
  359. } );
  360. it( 'should create instance from serialized element with parent', () => {
  361. let p = new Element( 'p', null, 'bar' );
  362. let nodeList = new NodeList( p );
  363. let serialized = jsonParseStringify( nodeList );
  364. let deserialized = NodeList.fromJSON( serialized );
  365. expect( deserialized.length ).to.equal( nodeList.length );
  366. expect( deserialized.get( 0 ).name ).to.equal( nodeList.get( 0 ).name );
  367. expect( deserialized.get( 0 ).getChildCount() ).to.equal( nodeList.get( 0 ).getChildCount() );
  368. for ( let i = 0; i < 3; i++ ) {
  369. expect( deserialized.get( 0 ).getChild( i ).character ).to.equal( nodeList.get( 0 ).getChild( i ).character );
  370. expect( deserialized.get( 0 ).getChild( i ).hasAttribute( 'bold' ) ).to.equal( nodeList.get( 0 ).getChild( i ).hasAttribute( 'bold' ) );
  371. expect( deserialized.get( 0 ).getChild( i ).getAttribute( 'bold' ) ).to.equal( nodeList.get( 0 ).getChild( i ).getAttribute( 'bold' ) );
  372. }
  373. } );
  374. } );
  375. } );