8
0

downcast-selection-converters.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 ViewUIElement from '../../src/view/uielement';
  11. import Mapper from '../../src/conversion/mapper';
  12. import DowncastDispatcher from '../../src/conversion/downcastdispatcher';
  13. import {
  14. convertRangeSelection,
  15. convertCollapsedSelection,
  16. clearAttributes,
  17. clearFakeSelection
  18. } from '../../src/conversion/downcast-selection-converters';
  19. import {
  20. insertElement,
  21. insertText,
  22. wrap,
  23. highlightElement,
  24. highlightText,
  25. removeHighlight
  26. } from '../../src/conversion/downcast-converters';
  27. import createViewRoot from '../view/_utils/createroot';
  28. import { stringify as stringifyView } from '../../src/dev-utils/view';
  29. import { setData as setModelData } from '../../src/dev-utils/model';
  30. describe( 'downcast-selection-converters', () => {
  31. let dispatcher, mapper, model, view, modelDoc, modelRoot, docSelection, viewDoc, viewRoot, viewSelection, highlightDescriptor;
  32. beforeEach( () => {
  33. model = new Model();
  34. modelDoc = model.document;
  35. modelRoot = modelDoc.createRoot();
  36. docSelection = modelDoc.selection;
  37. model.schema.extend( '$text', { allowIn: '$root' } );
  38. view = new View();
  39. viewDoc = view.document;
  40. viewRoot = createViewRoot( viewDoc );
  41. viewSelection = viewDoc.selection;
  42. mapper = new Mapper();
  43. mapper.bindElements( modelRoot, viewRoot );
  44. highlightDescriptor = { class: 'marker', priority: 1 };
  45. dispatcher = new DowncastDispatcher( { mapper, viewSelection } );
  46. dispatcher.on( 'insert:$text', insertText() );
  47. const strongCreator = ( value, data, consumable, api ) => api.writer.createAttributeElement( 'strong' );
  48. dispatcher.on( 'attribute:bold', wrap( strongCreator ) );
  49. dispatcher.on( 'addMarker:marker', highlightText( highlightDescriptor ) );
  50. dispatcher.on( 'addMarker:marker', highlightElement( highlightDescriptor ) );
  51. dispatcher.on( 'removeMarker:marker', removeHighlight( highlightDescriptor ) );
  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. view.destroy();
  59. } );
  60. describe( 'default converters', () => {
  61. describe( 'range selection', () => {
  62. it( 'in same container', () => {
  63. test(
  64. [ 1, 4 ],
  65. 'foobar',
  66. 'f{oob}ar'
  67. );
  68. } );
  69. it( 'in same container with unicode characters', () => {
  70. test(
  71. [ 2, 6 ],
  72. 'நிலைக்கு',
  73. 'நி{லைக்}கு'
  74. );
  75. } );
  76. it( 'in same container, over attribute', () => {
  77. test(
  78. [ 1, 5 ],
  79. 'fo<$text bold="true">ob</$text>ar',
  80. 'f{o<strong>ob</strong>a}r'
  81. );
  82. } );
  83. it( 'in same container, next to attribute', () => {
  84. test(
  85. [ 1, 2 ],
  86. 'fo<$text bold="true">ob</$text>ar',
  87. 'f{o}<strong>ob</strong>ar'
  88. );
  89. } );
  90. it( 'in same attribute', () => {
  91. test(
  92. [ 2, 4 ],
  93. 'f<$text bold="true">ooba</$text>r',
  94. 'f<strong>o{ob}a</strong>r'
  95. );
  96. } );
  97. it( 'in same attribute, selection same as attribute', () => {
  98. test(
  99. [ 2, 4 ],
  100. 'fo<$text bold="true">ob</$text>ar',
  101. 'fo{<strong>ob</strong>}ar'
  102. );
  103. } );
  104. it( 'starts in text node, ends in attribute #1', () => {
  105. test(
  106. [ 1, 3 ],
  107. 'fo<$text bold="true">ob</$text>ar',
  108. 'f{o<strong>o}b</strong>ar'
  109. );
  110. } );
  111. it( 'starts in text node, ends in attribute #2', () => {
  112. test(
  113. [ 1, 4 ],
  114. 'fo<$text bold="true">ob</$text>ar',
  115. 'f{o<strong>ob</strong>}ar'
  116. );
  117. } );
  118. it( 'starts in attribute, ends in text node', () => {
  119. test(
  120. [ 3, 5 ],
  121. 'fo<$text bold="true">ob</$text>ar',
  122. 'fo<strong>o{b</strong>a}r'
  123. );
  124. } );
  125. it( 'consumes consumable values properly', () => {
  126. // Add callback that will fire before default ones.
  127. // This should prevent default callback doing anything.
  128. dispatcher.on( 'selection', ( evt, data, consumable ) => {
  129. expect( consumable.consume( data.selection, 'selection' ) ).to.be.true;
  130. }, { priority: 'high' } );
  131. // Similar test case as the first in this suite.
  132. test(
  133. [ 1, 4 ],
  134. 'foobar',
  135. 'foobar' // No selection in view.
  136. );
  137. } );
  138. it( 'should convert backward selection', () => {
  139. test(
  140. [ 1, 3, 'backward' ],
  141. 'foobar',
  142. 'f{oo}bar'
  143. );
  144. expect( viewSelection.focus.offset ).to.equal( 1 );
  145. } );
  146. } );
  147. describe( 'collapsed selection', () => {
  148. let marker;
  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. model.change( writer => {
  166. marker = writer.setMarker( 'marker', ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 5 ) );
  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, model.markers, 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. model.change( writer => {
  185. marker = writer.setMarker( 'marker', ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 5 ) );
  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, model.markers, 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. model.change( writer => {
  207. marker = writer.setMarker( 'marker2', ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 5 ) );
  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, model.markers, 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. model.change( writer => {
  226. marker = writer.setMarker( 'marker3', ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 5 ) );
  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, model.markers, 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, model.markers, 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, model.markers, 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, model.markers, 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, model.markers, 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, model.markers, 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. writer.setFakeSelection( true );
  393. dispatcher.convertSelection( docSelection, model.markers, 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 containerCreator = ( item, consumable, api ) => api.writer.createContainerElement( item.name );
  410. const tableConverter = insertElement( containerCreator );
  411. dispatcher.on( 'insert:table', tableConverter );
  412. dispatcher.on( 'insert:tr', tableConverter );
  413. dispatcher.on( 'insert:td', tableConverter );
  414. // Special converter for table cells.
  415. dispatcher.on( 'selection', ( evt, data, consumable, conversionApi ) => {
  416. const selection = data.selection;
  417. if ( !consumable.test( selection, 'selection' ) || selection.isCollapsed ) {
  418. return;
  419. }
  420. for ( const range of selection.getRanges() ) {
  421. const node = range.start.nodeAfter;
  422. if ( node == range.end.nodeBefore && node instanceof ModelElement && node.name == 'td' ) {
  423. consumable.consume( selection, 'selection' );
  424. const viewNode = conversionApi.mapper.toViewElement( node );
  425. conversionApi.writer.addClass( 'selected', viewNode );
  426. }
  427. }
  428. } );
  429. } );
  430. it( 'should not be used to convert selection that is not on table cell', () => {
  431. test(
  432. [ 1, 5 ],
  433. 'f{o<$text bold="true">ob</$text>a}r',
  434. 'f{o<strong>ob</strong>a}r'
  435. );
  436. } );
  437. it( 'should add a class to the selected table cell', () => {
  438. test(
  439. // table tr#0 |td#0, table tr#0 td#0|
  440. [ [ 0, 0, 0 ], [ 0, 0, 1 ] ],
  441. '<table><tr><td>foo</td></tr><tr><td>bar</td></tr></table>',
  442. '<table><tr><td class="selected">foo</td></tr><tr><td>bar</td></tr></table>'
  443. );
  444. } );
  445. it( 'should not be used if selection contains more than just a table cell', () => {
  446. test(
  447. // table tr td#1, table tr#2
  448. [ [ 0, 0, 0, 1 ], [ 0, 0, 2 ] ],
  449. '<table><tr><td>foo</td><td>bar</td></tr></table>',
  450. '<table><tr><td>f{oo</td><td>bar</td>]</tr></table>'
  451. );
  452. } );
  453. } );
  454. // Tests if the selection got correctly converted.
  455. // Because `setData` might use selection converters itself to set the selection, we can't use it
  456. // to set the selection (because then we would test converters using converters).
  457. // Instead, the `test` function expects to be passed `selectionPaths` which is an array containing two numbers or two arrays,
  458. // that are offsets or paths of selection positions in root element.
  459. function test( selectionPaths, modelInput, expectedView, selectionAttributes = {} ) {
  460. // Parse passed `modelInput` string and set it as current model.
  461. setModelData( model, modelInput );
  462. // Manually set selection ranges using passed `selectionPaths`.
  463. const startPath = typeof selectionPaths[ 0 ] == 'number' ? [ selectionPaths[ 0 ] ] : selectionPaths[ 0 ];
  464. const endPath = typeof selectionPaths[ 1 ] == 'number' ? [ selectionPaths[ 1 ] ] : selectionPaths[ 1 ];
  465. const startPos = new ModelPosition( modelRoot, startPath );
  466. const endPos = new ModelPosition( modelRoot, endPath );
  467. const isBackward = selectionPaths[ 2 ] === 'backward';
  468. model.change( writer => {
  469. writer.setSelection( new ModelRange( startPos, endPos ), isBackward );
  470. // And add or remove passed attributes.
  471. for ( const key in selectionAttributes ) {
  472. const value = selectionAttributes[ key ];
  473. if ( value ) {
  474. writer.setSelectionAttribute( key, value );
  475. } else {
  476. writer.removeSelectionAttribute( key );
  477. }
  478. }
  479. } );
  480. // Remove view children manually (without firing additional conversion).
  481. viewRoot.removeChildren( 0, viewRoot.childCount );
  482. // Convert model to view.
  483. view.change( writer => {
  484. dispatcher.convertInsert( ModelRange.createIn( modelRoot ), writer );
  485. dispatcher.convertSelection( docSelection, model.markers, writer );
  486. } );
  487. // Stringify view and check if it is same as expected.
  488. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) ).to.equal( '<div>' + expectedView + '</div>' );
  489. }
  490. } );