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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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( 'consumes consumable values properly', () => {
  239. // Add callbacks that will fire before default ones.
  240. // This should prevent default callbacks doing anything.
  241. dispatcher.on( 'selection', ( evt, data, consumable ) => {
  242. expect( consumable.consume( data.selection, 'selection' ) ).to.be.true;
  243. }, { priority: 'high' } );
  244. dispatcher.on( 'selectionAttribute:bold', ( evt, data, consumable ) => {
  245. expect( consumable.consume( data.selection, 'selectionAttribute:bold' ) ).to.be.true;
  246. }, { priority: 'high' } );
  247. // Similar test case as above.
  248. test(
  249. [ 3, 3 ],
  250. 'f<$text bold="true">ooba</$text>r',
  251. 'f<strong>ooba</strong>r' // No selection in view.
  252. );
  253. } );
  254. } );
  255. } );
  256. describe( 'clean-up', () => {
  257. describe( 'convertRangeSelection', () => {
  258. it( 'should remove all ranges before adding new range', () => {
  259. test(
  260. [ 0, 2 ],
  261. 'foobar',
  262. '{fo}obar'
  263. );
  264. test(
  265. [ 3, 5 ],
  266. 'foobar',
  267. 'foo{ba}r'
  268. );
  269. expect( viewSelection.rangeCount ).to.equal( 1 );
  270. } );
  271. } );
  272. describe( 'convertCollapsedSelection', () => {
  273. it( 'should remove all ranges before adding new range', () => {
  274. test(
  275. [ 2, 2 ],
  276. 'foobar',
  277. 'fo{}obar'
  278. );
  279. test(
  280. [ 3, 3 ],
  281. 'foobar',
  282. 'foo{}bar'
  283. );
  284. expect( viewSelection.rangeCount ).to.equal( 1 );
  285. } );
  286. } );
  287. describe( 'clearAttributes', () => {
  288. it( 'should remove all ranges before adding new range', () => {
  289. dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( new ViewAttributeElement( 'b' ) ) );
  290. dispatcher.on( 'addAttribute:style', wrapItem( new ViewAttributeElement( 'b' ) ) );
  291. test(
  292. [ 3, 3 ],
  293. 'foobar',
  294. 'foo<b>[]</b>bar',
  295. { bold: 'true' }
  296. );
  297. const modelRange = ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 1 );
  298. modelDoc.selection.setRanges( [ modelRange ] );
  299. dispatcher.convertSelection( modelDoc.selection, [] );
  300. expect( viewSelection.rangeCount ).to.equal( 1 );
  301. const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
  302. expect( viewString ).to.equal( '<div>f{}oobar</div>' );
  303. } );
  304. it( 'should do nothing if the attribute element had been already removed', () => {
  305. dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( new ViewAttributeElement( 'b' ) ) );
  306. dispatcher.on( 'addAttribute:style', wrapItem( new ViewAttributeElement( 'b' ) ) );
  307. test(
  308. [ 3, 3 ],
  309. 'foobar',
  310. 'foo<b>[]</b>bar',
  311. { bold: 'true' }
  312. );
  313. // Remove <b></b> manually.
  314. mergeAttributes( viewSelection.getFirstPosition() );
  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. } );
  323. describe( 'clearFakeSelection', () => {
  324. it( 'should clear fake selection', () => {
  325. dispatcher.on( 'selection', clearFakeSelection() );
  326. viewSelection.setFake( true );
  327. dispatcher.convertSelection( modelSelection, [] );
  328. expect( viewSelection.isFake ).to.be.false;
  329. } );
  330. } );
  331. } );
  332. describe( 'using element creator for attributes conversion', () => {
  333. beforeEach( () => {
  334. function themeElementCreator( themeValue ) {
  335. if ( themeValue == 'important' ) {
  336. return new ViewAttributeElement( 'strong', { style: 'text-transform:uppercase;' } );
  337. } else if ( themeValue == 'gold' ) {
  338. return new ViewAttributeElement( 'span', { style: 'color:yellow;' } );
  339. }
  340. }
  341. dispatcher.on( 'selectionAttribute:theme', convertSelectionAttribute( themeElementCreator ) );
  342. dispatcher.on( 'addAttribute:theme', wrapItem( themeElementCreator ) );
  343. dispatcher.on( 'selectionAttribute:italic', convertSelectionAttribute( new ViewAttributeElement( 'em' ) ) );
  344. } );
  345. describe( 'range selection', () => {
  346. it( 'in same container, over attribute', () => {
  347. test(
  348. [ 1, 5 ],
  349. 'fo<$text theme="gold">ob</$text>ar',
  350. 'f{o<span style="color:yellow;">ob</span>a}r'
  351. );
  352. } );
  353. it( 'in same attribute', () => {
  354. test(
  355. [ 2, 4 ],
  356. 'f<$text theme="gold">ooba</$text>r',
  357. 'f<span style="color:yellow;">o{ob}a</span>r'
  358. );
  359. } );
  360. it( 'in same attribute, selection same as attribute', () => {
  361. test(
  362. [ 2, 4 ],
  363. 'fo<$text theme="important">ob</$text>ar',
  364. 'fo{<strong style="text-transform:uppercase;">ob</strong>}ar'
  365. );
  366. } );
  367. it( 'starts in attribute, ends in text node', () => {
  368. test(
  369. [ 3, 5 ],
  370. 'fo<$text theme="important">ob</$text>ar',
  371. 'fo<strong style="text-transform:uppercase;">o{b</strong>a}r'
  372. );
  373. } );
  374. } );
  375. describe( 'collapsed selection', () => {
  376. it( 'in attribute', () => {
  377. test(
  378. [ 3, 3 ],
  379. 'f<$text theme="gold">ooba</$text>r',
  380. 'f<span style="color:yellow;">oo{}ba</span>r'
  381. );
  382. } );
  383. it( 'in container with theme attribute', () => {
  384. test(
  385. [ 1, 1 ],
  386. 'foobar',
  387. 'f<strong style="text-transform:uppercase;">[]</strong>oobar',
  388. { theme: 'important' }
  389. );
  390. } );
  391. it( 'in theme attribute with extra attributes #1', () => {
  392. test(
  393. [ 3, 3 ],
  394. 'f<$text theme="gold">ooba</$text>r',
  395. 'f<span style="color:yellow;">oo</span>' +
  396. '<em><span style="color:yellow;">[]</span></em>' +
  397. '<span style="color:yellow;">ba</span>r',
  398. { italic: true }
  399. );
  400. } );
  401. it( 'in theme attribute with extra attributes #2', () => {
  402. // In contrary to test above, we don't have strong + span on the selection.
  403. // This is because strong and span are both created by the same attribute.
  404. // Since style="important" overwrites style="gold" on selection, we have only strong element.
  405. // In example above, selection has both style and italic attribute.
  406. test(
  407. [ 3, 3 ],
  408. 'f<$text theme="gold">ooba</$text>r',
  409. 'f<span style="color:yellow;">oo</span>' +
  410. '<strong style="text-transform:uppercase;">[]</strong>' +
  411. '<span style="color:yellow;">ba</span>r',
  412. { theme: 'important' }
  413. );
  414. } );
  415. } );
  416. } );
  417. describe( 'table cell selection converter', () => {
  418. beforeEach( () => {
  419. modelDoc.schema.registerItem( 'table' );
  420. modelDoc.schema.registerItem( 'tr' );
  421. modelDoc.schema.registerItem( 'td' );
  422. modelDoc.schema.allow( { name: 'table', inside: '$root' } );
  423. modelDoc.schema.allow( { name: 'tr', inside: 'table' } );
  424. modelDoc.schema.allow( { name: 'td', inside: 'tr' } );
  425. modelDoc.schema.allow( { name: '$text', inside: 'td' } );
  426. // "Universal" converter to convert table structure.
  427. const tableConverter = insertElement( ( data ) => new ViewContainerElement( data.item.name ) );
  428. dispatcher.on( 'insert:table', tableConverter );
  429. dispatcher.on( 'insert:tr', tableConverter );
  430. dispatcher.on( 'insert:td', tableConverter );
  431. // Special converter for table cells.
  432. dispatcher.on( 'selection', ( evt, data, consumable, conversionApi ) => {
  433. const selection = data.selection;
  434. if ( !consumable.test( selection, 'selection' ) || selection.isCollapsed ) {
  435. return;
  436. }
  437. for ( let range of selection.getRanges() ) {
  438. const node = range.start.nodeAfter;
  439. if ( node == range.end.nodeBefore && node instanceof ModelElement && node.name == 'td' ) {
  440. consumable.consume( selection, 'selection' );
  441. let viewNode = conversionApi.mapper.toViewElement( node );
  442. viewNode.addClass( 'selected' );
  443. }
  444. }
  445. } );
  446. } );
  447. it( 'should not be used to convert selection that is not on table cell', () => {
  448. test(
  449. [ 1, 5 ],
  450. 'f{o<$text bold="true">ob</$text>a}r',
  451. 'f{o<strong>ob</strong>a}r'
  452. );
  453. } );
  454. it( 'should add a class to the selected table cell', () => {
  455. test(
  456. // table tr#0 |td#0, table tr#0 td#0|
  457. [ [ 0, 0, 0 ], [ 0, 0, 1 ] ],
  458. '<table><tr><td>foo</td></tr><tr><td>bar</td></tr></table>',
  459. '<table><tr><td class="selected">foo</td></tr><tr><td>bar</td></tr></table>'
  460. );
  461. } );
  462. it( 'should not be used if selection contains more than just a table cell', () => {
  463. test(
  464. // table tr td#1, table tr#2
  465. [ [ 0, 0, 0, 1 ], [ 0, 0, 2 ] ],
  466. '<table><tr><td>foo</td><td>bar</td></tr></table>',
  467. '<table><tr><td>f{oo</td><td>bar</td>]</tr></table>'
  468. );
  469. } );
  470. } );
  471. // Tests if the selection got correctly converted.
  472. // Because `setData` might use selection converters itself to set the selection, we can't use it
  473. // to set the selection (because then we would test converters using converters).
  474. // Instead, the `test` function expects to be passed `selectionPaths` which is an array containing two numbers or two arrays,
  475. // that are offsets or paths of selection positions in root element.
  476. function test( selectionPaths, modelInput, expectedView, selectionAttributes = {} ) {
  477. // Parse passed `modelInput` string and set it as current model.
  478. setModelData( modelDoc, modelInput );
  479. // Manually set selection ranges using passed `selectionPaths`.
  480. const startPath = typeof selectionPaths[ 0 ] == 'number' ? [ selectionPaths[ 0 ] ] : selectionPaths[ 0 ];
  481. const endPath = typeof selectionPaths[ 1 ] == 'number' ? [ selectionPaths[ 1 ] ] : selectionPaths[ 1 ];
  482. const startPos = new ModelPosition( modelRoot, startPath );
  483. const endPos = new ModelPosition( modelRoot, endPath );
  484. const isBackward = selectionPaths[ 2 ] === 'backward';
  485. modelSelection.setRanges( [ new ModelRange( startPos, endPos ) ], isBackward );
  486. // Update selection attributes according to model.
  487. modelSelection.refreshAttributes();
  488. // And add or remove passed attributes.
  489. for ( let key in selectionAttributes ) {
  490. let value = selectionAttributes[ key ];
  491. if ( value ) {
  492. modelSelection.setAttribute( key, value );
  493. } else {
  494. modelSelection.removeAttribute( key );
  495. }
  496. }
  497. // Remove view children manually (without firing additional conversion).
  498. viewRoot.removeChildren( 0, viewRoot.childCount );
  499. // Convert model to view.
  500. dispatcher.convertInsertion( ModelRange.createIn( modelRoot ) );
  501. dispatcher.convertSelection( modelSelection, [] );
  502. // Stringify view and check if it is same as expected.
  503. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) ).to.equal( '<div>' + expectedView + '</div>' );
  504. }
  505. } );