8
0

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

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