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

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