8
0

model-selection-to-view-converters.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelDocument from '../../src/model/document';
  6. import ModelElement from '../../src/model/element';
  7. import ModelRange from '../../src/model/range';
  8. import ModelPosition from '../../src/model/position';
  9. import ViewDocument from '../../src/view/document';
  10. import ViewContainerElement from '../../src/view/containerelement';
  11. import ViewAttributeElement from '../../src/view/attributeelement';
  12. import { mergeAttributes } from '../../src/view/writer';
  13. import Mapper from '../../src/conversion/mapper';
  14. import ModelConversionDispatcher from '../../src/conversion/modelconversiondispatcher';
  15. import {
  16. convertRangeSelection,
  17. convertCollapsedSelection,
  18. convertSelectionAttribute,
  19. convertSelectionMarker,
  20. clearAttributes,
  21. clearFakeSelection
  22. } from '../../src/conversion/model-selection-to-view-converters';
  23. import {
  24. insertElement,
  25. insertText,
  26. wrapItem,
  27. wrapRange,
  28. unwrapRange
  29. } from '../../src/conversion/model-to-view-converters';
  30. import { stringify as stringifyView } from '../../src/dev-utils/view';
  31. import { setData as setModelData } from '../../src/dev-utils/model';
  32. const spanViewElementForMarker = new ViewAttributeElement( 'span' );
  33. spanViewElementForMarker.priority = 1;
  34. describe( 'model-selection-to-view-converters', () => {
  35. let dispatcher, mapper;
  36. let modelDoc, modelRoot, modelSelection;
  37. let viewDoc, viewRoot, viewSelection;
  38. beforeEach( () => {
  39. modelDoc = new ModelDocument();
  40. modelRoot = modelDoc.createRoot();
  41. modelSelection = modelDoc.selection;
  42. modelDoc.schema.allow( { name: '$text', inside: '$root' } );
  43. viewDoc = new ViewDocument();
  44. viewRoot = viewDoc.createRoot( 'div' );
  45. viewSelection = viewDoc.selection;
  46. mapper = new Mapper();
  47. mapper.bindElements( modelRoot, viewRoot );
  48. dispatcher = new ModelConversionDispatcher( { mapper, viewSelection } );
  49. dispatcher.on( 'insert:$text', insertText() );
  50. dispatcher.on( 'addAttribute:bold', wrapItem( new ViewAttributeElement( 'strong' ) ) );
  51. dispatcher.on( 'addMarker:marker', wrapRange( spanViewElementForMarker ) );
  52. dispatcher.on( 'removeMarker:marker', unwrapRange( spanViewElementForMarker ) );
  53. // Default selection converters.
  54. dispatcher.on( 'selection', clearAttributes(), { priority: 'low' } );
  55. dispatcher.on( 'selection', convertRangeSelection(), { priority: 'low' } );
  56. dispatcher.on( 'selection', convertCollapsedSelection(), { priority: 'low' } );
  57. } );
  58. afterEach( () => {
  59. viewDoc.destroy();
  60. } );
  61. describe( 'default converters', () => {
  62. beforeEach( () => {
  63. // Selection converters for selection attributes.
  64. dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( new ViewAttributeElement( 'strong' ) ) );
  65. dispatcher.on( 'selectionAttribute:italic', convertSelectionAttribute( new ViewAttributeElement( 'em' ) ) );
  66. dispatcher.on( 'selectionMarker:marker', convertSelectionMarker( spanViewElementForMarker ) );
  67. } );
  68. describe( 'range selection', () => {
  69. it( 'in same container', () => {
  70. test(
  71. [ 1, 4 ],
  72. 'foobar',
  73. 'f{oob}ar'
  74. );
  75. } );
  76. it( 'in same container with unicode characters', () => {
  77. test(
  78. [ 2, 6 ],
  79. 'நிலைக்கு',
  80. 'நி{லைக்}கு'
  81. );
  82. } );
  83. it( 'in same container, over attribute', () => {
  84. test(
  85. [ 1, 5 ],
  86. 'fo<$text bold="true">ob</$text>ar',
  87. 'f{o<strong>ob</strong>a}r'
  88. );
  89. } );
  90. it( 'in same container, next to attribute', () => {
  91. test(
  92. [ 1, 2 ],
  93. 'fo<$text bold="true">ob</$text>ar',
  94. 'f{o}<strong>ob</strong>ar'
  95. );
  96. } );
  97. it( 'in same attribute', () => {
  98. test(
  99. [ 2, 4 ],
  100. 'f<$text bold="true">ooba</$text>r',
  101. 'f<strong>o{ob}a</strong>r'
  102. );
  103. } );
  104. it( 'in same attribute, selection same as attribute', () => {
  105. test(
  106. [ 2, 4 ],
  107. 'fo<$text bold="true">ob</$text>ar',
  108. 'fo{<strong>ob</strong>}ar'
  109. );
  110. } );
  111. it( 'starts in text node, ends in attribute #1', () => {
  112. test(
  113. [ 1, 3 ],
  114. 'fo<$text bold="true">ob</$text>ar',
  115. 'f{o<strong>o}b</strong>ar'
  116. );
  117. } );
  118. it( 'starts in text node, ends in attribute #2', () => {
  119. test(
  120. [ 1, 4 ],
  121. 'fo<$text bold="true">ob</$text>ar',
  122. 'f{o<strong>ob</strong>}ar'
  123. );
  124. } );
  125. it( 'starts in attribute, ends in text node', () => {
  126. test(
  127. [ 3, 5 ],
  128. 'fo<$text bold="true">ob</$text>ar',
  129. 'fo<strong>o{b</strong>a}r'
  130. );
  131. } );
  132. it( 'consumes consumable values properly', () => {
  133. // Add callback that will fire before default ones.
  134. // This should prevent default callback doing anything.
  135. dispatcher.on( 'selection', ( evt, data, consumable ) => {
  136. expect( consumable.consume( data.selection, 'selection' ) ).to.be.true;
  137. }, { priority: 'high' } );
  138. // Similar test case as the first in this suite.
  139. test(
  140. [ 1, 4 ],
  141. 'foobar',
  142. 'foobar' // No selection in view.
  143. );
  144. } );
  145. it( 'should convert backward selection', () => {
  146. test(
  147. [ 1, 3, 'backward' ],
  148. 'foobar',
  149. 'f{oo}bar'
  150. );
  151. expect( viewSelection.focus.offset ).to.equal( 1 );
  152. } );
  153. } );
  154. describe( 'collapsed selection', () => {
  155. it( 'in container', () => {
  156. test(
  157. [ 1, 1 ],
  158. 'foobar',
  159. 'f{}oobar'
  160. );
  161. } );
  162. it( 'in attribute', () => {
  163. test(
  164. [ 3, 3 ],
  165. 'f<$text bold="true">ooba</$text>r',
  166. 'f<strong>oo{}ba</strong>r'
  167. );
  168. } );
  169. it( 'in container with extra attributes', () => {
  170. test(
  171. [ 1, 1 ],
  172. 'foobar',
  173. 'f<em>[]</em>oobar',
  174. { italic: true }
  175. );
  176. } );
  177. it( 'in attribute with extra attributes', () => {
  178. test(
  179. [ 3, 3 ],
  180. 'f<$text bold="true">ooba</$text>r',
  181. 'f<strong>oo</strong><em><strong>[]</strong></em><strong>ba</strong>r',
  182. { italic: true }
  183. );
  184. } );
  185. it( 'in attribute and marker', () => {
  186. setModelData( modelDoc, 'fo<$text bold="true">ob</$text>ar' );
  187. const marker = modelDoc.markers.set( 'marker', ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 5 ) );
  188. modelSelection.setRanges( [ new ModelRange( ModelPosition.createAt( modelRoot, 3 ) ) ] );
  189. // Update selection attributes according to model.
  190. modelSelection.refreshAttributes();
  191. // Remove view children manually (without firing additional conversion).
  192. viewRoot.removeChildren( 0, viewRoot.childCount );
  193. // Convert model to view.
  194. dispatcher.convertInsertion( ModelRange.createIn( modelRoot ) );
  195. dispatcher.convertMarker( 'addMarker', marker.name, marker.getRange() );
  196. const markers = Array.from( modelDoc.markers.getMarkersAtPosition( modelSelection.getFirstPosition() ) );
  197. dispatcher.convertSelection( modelSelection, markers );
  198. // Stringify view and check if it is same as expected.
  199. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  200. .to.equal( '<div>f<span>o<strong>o{}b</strong>a</span>r</div>' );
  201. } );
  202. it( 'in attribute and marker - no attribute', () => {
  203. setModelData( modelDoc, 'fo<$text bold="true">ob</$text>ar' );
  204. const marker = modelDoc.markers.set( 'marker', ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 5 ) );
  205. modelSelection.setRanges( [ new ModelRange( ModelPosition.createAt( modelRoot, 3 ) ) ] );
  206. // Update selection attributes according to model.
  207. modelSelection.refreshAttributes();
  208. modelSelection.removeAttribute( 'bold' );
  209. // Remove view children manually (without firing additional conversion).
  210. viewRoot.removeChildren( 0, viewRoot.childCount );
  211. // Convert model to view.
  212. dispatcher.convertInsertion( ModelRange.createIn( modelRoot ) );
  213. dispatcher.convertMarker( 'addMarker', marker.name, marker.getRange() );
  214. const markers = Array.from( modelDoc.markers.getMarkersAtPosition( modelSelection.getFirstPosition() ) );
  215. dispatcher.convertSelection( modelSelection, markers );
  216. // Stringify view and check if it is same as expected.
  217. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  218. .to.equal( '<div>f<span>o<strong>o</strong>[]<strong>b</strong>a</span>r</div>' );
  219. } );
  220. it( 'in marker - using view element as element creator function for selection marker conversion', () => {
  221. dispatcher.on( 'selectionMarker:marker2', convertSelectionMarker(
  222. ( data ) => new ViewAttributeElement( 'span', { 'class': data.name } )
  223. ) );
  224. setModelData( modelDoc, 'foobar' );
  225. const marker = modelDoc.markers.set( 'marker2', ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 5 ) );
  226. modelSelection.setRanges( [ new ModelRange( ModelPosition.createAt( modelRoot, 3 ) ) ] );
  227. // Remove view children manually (without firing additional conversion).
  228. viewRoot.removeChildren( 0, viewRoot.childCount );
  229. // Convert model to view.
  230. dispatcher.convertInsertion( ModelRange.createIn( modelRoot ) );
  231. dispatcher.convertMarker( 'addMarker', marker.name, marker.getRange() );
  232. const markers = Array.from( modelDoc.markers.getMarkersAtPosition( modelSelection.getFirstPosition() ) );
  233. dispatcher.convertSelection( modelSelection, markers );
  234. // Stringify view and check if it is same as expected.
  235. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  236. .to.equal( '<div>foo<span class="marker2">[]</span>bar</div>' );
  237. } );
  238. it( 'should do nothing if creator return null', () => {
  239. dispatcher.on( 'selectionMarker:marker3', convertSelectionMarker( () => {
  240. return;
  241. } ) );
  242. setModelData( modelDoc, 'foobar' );
  243. const marker = modelDoc.markers.set( 'marker3', ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 5 ) );
  244. modelSelection.setRanges( [ new ModelRange( ModelPosition.createAt( modelRoot, 3 ) ) ] );
  245. // Remove view children manually (without firing additional conversion).
  246. viewRoot.removeChildren( 0, viewRoot.childCount );
  247. // Convert model to view.
  248. dispatcher.convertInsertion( ModelRange.createIn( modelRoot ) );
  249. dispatcher.convertMarker( 'addMarker', marker.name, marker.getRange() );
  250. const markers = Array.from( modelDoc.markers.getMarkersAtPosition( modelSelection.getFirstPosition() ) );
  251. dispatcher.convertSelection( modelSelection, markers );
  252. // Stringify view and check if it is same as expected.
  253. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  254. .to.equal( '<div>foo{}bar</div>' );
  255. } );
  256. it( 'consumes consumable values properly', () => {
  257. // Add callbacks that will fire before default ones.
  258. // This should prevent default callbacks doing anything.
  259. dispatcher.on( 'selection', ( evt, data, consumable ) => {
  260. expect( consumable.consume( data.selection, 'selection' ) ).to.be.true;
  261. }, { priority: 'high' } );
  262. dispatcher.on( 'selectionAttribute:bold', ( evt, data, consumable ) => {
  263. expect( consumable.consume( data.selection, 'selectionAttribute:bold' ) ).to.be.true;
  264. }, { priority: 'high' } );
  265. // Similar test case as above.
  266. test(
  267. [ 3, 3 ],
  268. 'f<$text bold="true">ooba</$text>r',
  269. 'f<strong>ooba</strong>r' // No selection in view.
  270. );
  271. } );
  272. } );
  273. } );
  274. describe( 'clean-up', () => {
  275. describe( 'convertRangeSelection', () => {
  276. it( 'should remove all ranges before adding new range', () => {
  277. test(
  278. [ 0, 2 ],
  279. 'foobar',
  280. '{fo}obar'
  281. );
  282. test(
  283. [ 3, 5 ],
  284. 'foobar',
  285. 'foo{ba}r'
  286. );
  287. expect( viewSelection.rangeCount ).to.equal( 1 );
  288. } );
  289. } );
  290. describe( 'convertCollapsedSelection', () => {
  291. it( 'should remove all ranges before adding new range', () => {
  292. test(
  293. [ 2, 2 ],
  294. 'foobar',
  295. 'fo{}obar'
  296. );
  297. test(
  298. [ 3, 3 ],
  299. 'foobar',
  300. 'foo{}bar'
  301. );
  302. expect( viewSelection.rangeCount ).to.equal( 1 );
  303. } );
  304. } );
  305. describe( 'clearAttributes', () => {
  306. it( 'should remove all ranges before adding new range', () => {
  307. dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( new ViewAttributeElement( 'b' ) ) );
  308. dispatcher.on( 'addAttribute:style', wrapItem( new ViewAttributeElement( 'b' ) ) );
  309. test(
  310. [ 3, 3 ],
  311. 'foobar',
  312. 'foo<b>[]</b>bar',
  313. { bold: 'true' }
  314. );
  315. const modelRange = ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 1 );
  316. modelDoc.selection.setRanges( [ modelRange ] );
  317. dispatcher.convertSelection( modelDoc.selection, [] );
  318. expect( viewSelection.rangeCount ).to.equal( 1 );
  319. const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
  320. expect( viewString ).to.equal( '<div>f{}oobar</div>' );
  321. } );
  322. it( 'should do nothing if the attribute element had been already removed', () => {
  323. dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( new ViewAttributeElement( 'b' ) ) );
  324. dispatcher.on( 'addAttribute:style', wrapItem( new ViewAttributeElement( 'b' ) ) );
  325. test(
  326. [ 3, 3 ],
  327. 'foobar',
  328. 'foo<b>[]</b>bar',
  329. { bold: 'true' }
  330. );
  331. // Remove <b></b> manually.
  332. mergeAttributes( viewSelection.getFirstPosition() );
  333. const modelRange = ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 1 );
  334. modelDoc.selection.setRanges( [ modelRange ] );
  335. dispatcher.convertSelection( modelDoc.selection, [] );
  336. expect( viewSelection.rangeCount ).to.equal( 1 );
  337. const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
  338. expect( viewString ).to.equal( '<div>f{}oobar</div>' );
  339. } );
  340. } );
  341. describe( 'clearFakeSelection', () => {
  342. it( 'should clear fake selection', () => {
  343. dispatcher.on( 'selection', clearFakeSelection() );
  344. viewSelection.setFake( true );
  345. dispatcher.convertSelection( modelSelection, [] );
  346. expect( viewSelection.isFake ).to.be.false;
  347. } );
  348. } );
  349. } );
  350. describe( 'using element creator for attributes conversion', () => {
  351. beforeEach( () => {
  352. function themeElementCreator( themeValue ) {
  353. if ( themeValue == 'important' ) {
  354. return new ViewAttributeElement( 'strong', { style: 'text-transform:uppercase;' } );
  355. } else if ( themeValue == 'gold' ) {
  356. return new ViewAttributeElement( 'span', { style: 'color:yellow;' } );
  357. }
  358. }
  359. dispatcher.on( 'selectionAttribute:theme', convertSelectionAttribute( themeElementCreator ) );
  360. dispatcher.on( 'addAttribute:theme', wrapItem( themeElementCreator ) );
  361. dispatcher.on( 'selectionAttribute:italic', convertSelectionAttribute( new ViewAttributeElement( 'em' ) ) );
  362. } );
  363. describe( 'range selection', () => {
  364. it( 'in same container, over attribute', () => {
  365. test(
  366. [ 1, 5 ],
  367. 'fo<$text theme="gold">ob</$text>ar',
  368. 'f{o<span style="color:yellow;">ob</span>a}r'
  369. );
  370. } );
  371. it( 'in same attribute', () => {
  372. test(
  373. [ 2, 4 ],
  374. 'f<$text theme="gold">ooba</$text>r',
  375. 'f<span style="color:yellow;">o{ob}a</span>r'
  376. );
  377. } );
  378. it( 'in same attribute, selection same as attribute', () => {
  379. test(
  380. [ 2, 4 ],
  381. 'fo<$text theme="important">ob</$text>ar',
  382. 'fo{<strong style="text-transform:uppercase;">ob</strong>}ar'
  383. );
  384. } );
  385. it( 'starts in attribute, ends in text node', () => {
  386. test(
  387. [ 3, 5 ],
  388. 'fo<$text theme="important">ob</$text>ar',
  389. 'fo<strong style="text-transform:uppercase;">o{b</strong>a}r'
  390. );
  391. } );
  392. } );
  393. describe( 'collapsed selection', () => {
  394. it( 'in attribute', () => {
  395. test(
  396. [ 3, 3 ],
  397. 'f<$text theme="gold">ooba</$text>r',
  398. 'f<span style="color:yellow;">oo{}ba</span>r'
  399. );
  400. } );
  401. it( 'in container with theme attribute', () => {
  402. test(
  403. [ 1, 1 ],
  404. 'foobar',
  405. 'f<strong style="text-transform:uppercase;">[]</strong>oobar',
  406. { theme: 'important' }
  407. );
  408. } );
  409. it( 'in theme attribute with extra attributes #1', () => {
  410. test(
  411. [ 3, 3 ],
  412. 'f<$text theme="gold">ooba</$text>r',
  413. 'f<span style="color:yellow;">oo</span>' +
  414. '<em><span style="color:yellow;">[]</span></em>' +
  415. '<span style="color:yellow;">ba</span>r',
  416. { italic: true }
  417. );
  418. } );
  419. it( 'in theme attribute with extra attributes #2', () => {
  420. // In contrary to test above, we don't have strong + span on the selection.
  421. // This is because strong and span are both created by the same attribute.
  422. // Since style="important" overwrites style="gold" on selection, we have only strong element.
  423. // In example above, selection has both style and italic attribute.
  424. test(
  425. [ 3, 3 ],
  426. 'f<$text theme="gold">ooba</$text>r',
  427. 'f<span style="color:yellow;">oo</span>' +
  428. '<strong style="text-transform:uppercase;">[]</strong>' +
  429. '<span style="color:yellow;">ba</span>r',
  430. { theme: 'important' }
  431. );
  432. } );
  433. it( 'convertSelectionAttribute should do nothing if creator return null', () => {
  434. dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( () => {
  435. return;
  436. } ) );
  437. test(
  438. [ 3, 3 ],
  439. 'foobar',
  440. 'foo{}bar',
  441. { bold: 'true' }
  442. );
  443. } );
  444. } );
  445. } );
  446. describe( 'table cell selection converter', () => {
  447. beforeEach( () => {
  448. modelDoc.schema.registerItem( 'table' );
  449. modelDoc.schema.registerItem( 'tr' );
  450. modelDoc.schema.registerItem( 'td' );
  451. modelDoc.schema.allow( { name: 'table', inside: '$root' } );
  452. modelDoc.schema.allow( { name: 'tr', inside: 'table' } );
  453. modelDoc.schema.allow( { name: 'td', inside: 'tr' } );
  454. modelDoc.schema.allow( { name: '$text', inside: 'td' } );
  455. // "Universal" converter to convert table structure.
  456. const tableConverter = insertElement( ( data ) => new ViewContainerElement( data.item.name ) );
  457. dispatcher.on( 'insert:table', tableConverter );
  458. dispatcher.on( 'insert:tr', tableConverter );
  459. dispatcher.on( 'insert:td', tableConverter );
  460. // Special converter for table cells.
  461. dispatcher.on( 'selection', ( evt, data, consumable, conversionApi ) => {
  462. const selection = data.selection;
  463. if ( !consumable.test( selection, 'selection' ) || selection.isCollapsed ) {
  464. return;
  465. }
  466. for ( let range of selection.getRanges() ) {
  467. const node = range.start.nodeAfter;
  468. if ( node == range.end.nodeBefore && node instanceof ModelElement && node.name == 'td' ) {
  469. consumable.consume( selection, 'selection' );
  470. let viewNode = conversionApi.mapper.toViewElement( node );
  471. viewNode.addClass( 'selected' );
  472. }
  473. }
  474. } );
  475. } );
  476. it( 'should not be used to convert selection that is not on table cell', () => {
  477. test(
  478. [ 1, 5 ],
  479. 'f{o<$text bold="true">ob</$text>a}r',
  480. 'f{o<strong>ob</strong>a}r'
  481. );
  482. } );
  483. it( 'should add a class to the selected table cell', () => {
  484. test(
  485. // table tr#0 |td#0, table tr#0 td#0|
  486. [ [ 0, 0, 0 ], [ 0, 0, 1 ] ],
  487. '<table><tr><td>foo</td></tr><tr><td>bar</td></tr></table>',
  488. '<table><tr><td class="selected">foo</td></tr><tr><td>bar</td></tr></table>'
  489. );
  490. } );
  491. it( 'should not be used if selection contains more than just a table cell', () => {
  492. test(
  493. // table tr td#1, table tr#2
  494. [ [ 0, 0, 0, 1 ], [ 0, 0, 2 ] ],
  495. '<table><tr><td>foo</td><td>bar</td></tr></table>',
  496. '<table><tr><td>f{oo</td><td>bar</td>]</tr></table>'
  497. );
  498. } );
  499. } );
  500. // Tests if the selection got correctly converted.
  501. // Because `setData` might use selection converters itself to set the selection, we can't use it
  502. // to set the selection (because then we would test converters using converters).
  503. // Instead, the `test` function expects to be passed `selectionPaths` which is an array containing two numbers or two arrays,
  504. // that are offsets or paths of selection positions in root element.
  505. function test( selectionPaths, modelInput, expectedView, selectionAttributes = {} ) {
  506. // Parse passed `modelInput` string and set it as current model.
  507. setModelData( modelDoc, modelInput );
  508. // Manually set selection ranges using passed `selectionPaths`.
  509. const startPath = typeof selectionPaths[ 0 ] == 'number' ? [ selectionPaths[ 0 ] ] : selectionPaths[ 0 ];
  510. const endPath = typeof selectionPaths[ 1 ] == 'number' ? [ selectionPaths[ 1 ] ] : selectionPaths[ 1 ];
  511. const startPos = new ModelPosition( modelRoot, startPath );
  512. const endPos = new ModelPosition( modelRoot, endPath );
  513. const isBackward = selectionPaths[ 2 ] === 'backward';
  514. modelSelection.setRanges( [ new ModelRange( startPos, endPos ) ], isBackward );
  515. // Update selection attributes according to model.
  516. modelSelection.refreshAttributes();
  517. // And add or remove passed attributes.
  518. for ( let key in selectionAttributes ) {
  519. let value = selectionAttributes[ key ];
  520. if ( value ) {
  521. modelSelection.setAttribute( key, value );
  522. } else {
  523. modelSelection.removeAttribute( key );
  524. }
  525. }
  526. // Remove view children manually (without firing additional conversion).
  527. viewRoot.removeChildren( 0, viewRoot.childCount );
  528. // Convert model to view.
  529. dispatcher.convertInsertion( ModelRange.createIn( modelRoot ) );
  530. dispatcher.convertSelection( modelSelection, [] );
  531. // Stringify view and check if it is same as expected.
  532. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) ).to.equal( '<div>' + expectedView + '</div>' );
  533. }
  534. } );