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

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