downcast-selection-converters.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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 View from '../../src/view/view';
  7. import ViewUIElement from '../../src/view/uielement';
  8. import Mapper from '../../src/conversion/mapper';
  9. import DowncastDispatcher from '../../src/conversion/downcastdispatcher';
  10. import {
  11. convertRangeSelection,
  12. convertCollapsedSelection,
  13. clearAttributes,
  14. } from '../../src/conversion/downcast-selection-converters';
  15. import {
  16. insertElement,
  17. insertText,
  18. wrap,
  19. highlightElement,
  20. highlightText,
  21. removeHighlight
  22. } from '../../src/conversion/downcast-converters';
  23. import createViewRoot from '../view/_utils/createroot';
  24. import { stringify as stringifyView } from '../../src/dev-utils/view';
  25. import { setData as setModelData } from '../../src/dev-utils/model';
  26. describe( 'downcast-selection-converters', () => {
  27. let dispatcher, mapper, model, view, modelDoc, modelRoot, docSelection, viewDoc, viewRoot, viewSelection, highlightDescriptor;
  28. beforeEach( () => {
  29. model = new Model();
  30. modelDoc = model.document;
  31. modelRoot = modelDoc.createRoot();
  32. docSelection = modelDoc.selection;
  33. model.schema.extend( '$text', { allowIn: '$root' } );
  34. view = new View();
  35. viewDoc = view.document;
  36. viewRoot = createViewRoot( viewDoc );
  37. viewSelection = viewDoc.selection;
  38. mapper = new Mapper();
  39. mapper.bindElements( modelRoot, viewRoot );
  40. highlightDescriptor = { classes: 'marker', priority: 1 };
  41. dispatcher = new DowncastDispatcher( { mapper, viewSelection } );
  42. dispatcher.on( 'insert:$text', insertText() );
  43. const strongCreator = ( modelAttributeValue, viewWriter ) => viewWriter.createAttributeElement( 'strong' );
  44. dispatcher.on( 'attribute:bold', wrap( strongCreator ) );
  45. dispatcher.on( 'addMarker:marker', highlightText( highlightDescriptor ) );
  46. dispatcher.on( 'addMarker:marker', highlightElement( highlightDescriptor ) );
  47. dispatcher.on( 'removeMarker:marker', removeHighlight( highlightDescriptor ) );
  48. // Default selection converters.
  49. dispatcher.on( 'selection', clearAttributes(), { priority: 'low' } );
  50. dispatcher.on( 'selection', convertRangeSelection(), { priority: 'low' } );
  51. dispatcher.on( 'selection', convertCollapsedSelection(), { priority: 'low' } );
  52. } );
  53. afterEach( () => {
  54. view.destroy();
  55. } );
  56. describe( 'default converters', () => {
  57. describe( 'range selection', () => {
  58. it( 'in same container', () => {
  59. test(
  60. [ 1, 4 ],
  61. 'foobar',
  62. 'f{oob}ar'
  63. );
  64. } );
  65. it( 'in same container with unicode characters', () => {
  66. test(
  67. [ 2, 6 ],
  68. 'நிலைக்கு',
  69. 'நி{லைக்}கு'
  70. );
  71. } );
  72. it( 'in same container, over attribute', () => {
  73. test(
  74. [ 1, 5 ],
  75. 'fo<$text bold="true">ob</$text>ar',
  76. 'f{o<strong>ob</strong>a}r'
  77. );
  78. } );
  79. it( 'in same container, next to attribute', () => {
  80. test(
  81. [ 1, 2 ],
  82. 'fo<$text bold="true">ob</$text>ar',
  83. 'f{o}<strong>ob</strong>ar'
  84. );
  85. } );
  86. it( 'in same attribute', () => {
  87. test(
  88. [ 2, 4 ],
  89. 'f<$text bold="true">ooba</$text>r',
  90. 'f<strong>o{ob}a</strong>r'
  91. );
  92. } );
  93. it( 'in same attribute, selection same as attribute', () => {
  94. test(
  95. [ 2, 4 ],
  96. 'fo<$text bold="true">ob</$text>ar',
  97. 'fo{<strong>ob</strong>}ar'
  98. );
  99. } );
  100. it( 'starts in text node, ends in attribute #1', () => {
  101. test(
  102. [ 1, 3 ],
  103. 'fo<$text bold="true">ob</$text>ar',
  104. 'f{o<strong>o}b</strong>ar'
  105. );
  106. } );
  107. it( 'starts in text node, ends in attribute #2', () => {
  108. test(
  109. [ 1, 4 ],
  110. 'fo<$text bold="true">ob</$text>ar',
  111. 'f{o<strong>ob</strong>}ar'
  112. );
  113. } );
  114. it( 'starts in attribute, ends in text node', () => {
  115. test(
  116. [ 3, 5 ],
  117. 'fo<$text bold="true">ob</$text>ar',
  118. 'fo<strong>o{b</strong>a}r'
  119. );
  120. } );
  121. it( 'consumes consumable values properly', () => {
  122. // Add callback that will fire before default ones.
  123. // This should prevent default callback doing anything.
  124. dispatcher.on( 'selection', ( evt, data, conversionApi ) => {
  125. expect( conversionApi.consumable.consume( data.selection, 'selection' ) ).to.be.true;
  126. }, { priority: 'high' } );
  127. // Similar test case as the first in this suite.
  128. test(
  129. [ 1, 4 ],
  130. 'foobar',
  131. 'foobar' // No selection in view.
  132. );
  133. } );
  134. it( 'should convert backward selection', () => {
  135. test(
  136. [ 1, 3, 'backward' ],
  137. 'foobar',
  138. 'f{oo}bar'
  139. );
  140. expect( viewSelection.focus.offset ).to.equal( 1 );
  141. } );
  142. } );
  143. describe( 'collapsed selection', () => {
  144. let marker;
  145. it( 'in container', () => {
  146. test(
  147. [ 1, 1 ],
  148. 'foobar',
  149. 'f{}oobar'
  150. );
  151. } );
  152. it( 'in attribute', () => {
  153. test(
  154. [ 3, 3 ],
  155. 'f<$text bold="true">ooba</$text>r',
  156. 'f<strong>oo{}ba</strong>r'
  157. );
  158. } );
  159. it( 'in attribute and marker', () => {
  160. setModelData( model, 'fo<$text bold="true">ob</$text>ar' );
  161. model.change( writer => {
  162. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 5 ) );
  163. marker = writer.addMarker( 'marker', { range, usingOperation: false } );
  164. writer.setSelection( modelRoot, 3 );
  165. } );
  166. // Remove view children manually (without firing additional conversion).
  167. viewRoot._removeChildren( 0, viewRoot.childCount );
  168. // Convert model to view.
  169. view.change( writer => {
  170. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  171. dispatcher.convertMarkerAdd( marker.name, marker.getRange(), writer );
  172. dispatcher.convertSelection( docSelection, model.markers, writer );
  173. } );
  174. // Stringify view and check if it is same as expected.
  175. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) ).to.equal(
  176. '<div>f<span class="marker">o<strong>o{}b</strong>a</span>r</div>'
  177. );
  178. } );
  179. it( 'in attribute and marker - no attribute', () => {
  180. setModelData( model, 'fo<$text bold="true">ob</$text>ar' );
  181. model.change( writer => {
  182. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 5 ) );
  183. marker = writer.addMarker( 'marker', { range, usingOperation: false } );
  184. writer.setSelection( modelRoot, 3 );
  185. writer.removeSelectionAttribute( 'bold' );
  186. } );
  187. // Remove view children manually (without firing additional conversion).
  188. viewRoot._removeChildren( 0, viewRoot.childCount );
  189. // Convert model to view.
  190. view.change( writer => {
  191. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  192. dispatcher.convertMarkerAdd( marker.name, marker.getRange(), writer );
  193. dispatcher.convertSelection( docSelection, model.markers, writer );
  194. } );
  195. // Stringify view and check if it is same as expected.
  196. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  197. .to.equal( '<div>f<span class="marker">o<strong>o</strong>[]<strong>b</strong>a</span>r</div>' );
  198. } );
  199. it( 'in marker - using highlight descriptor creator', () => {
  200. dispatcher.on( 'addMarker:marker2', highlightText(
  201. data => ( { classes: data.markerName } )
  202. ) );
  203. setModelData( model, 'foobar' );
  204. model.change( writer => {
  205. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 5 ) );
  206. marker = writer.addMarker( 'marker2', { range, usingOperation: false } );
  207. writer.setSelection( modelRoot, 3 );
  208. } );
  209. // Remove view children manually (without firing additional conversion).
  210. viewRoot._removeChildren( 0, viewRoot.childCount );
  211. // Convert model to view.
  212. view.change( writer => {
  213. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  214. dispatcher.convertMarkerAdd( marker.name, marker.getRange(), writer );
  215. dispatcher.convertSelection( docSelection, model.markers, writer );
  216. } );
  217. // Stringify view and check if it is same as expected.
  218. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  219. .to.equal( '<div>f<span class="marker2">oo{}ba</span>r</div>' );
  220. } );
  221. it( 'should do nothing if creator return null', () => {
  222. dispatcher.on( 'addMarker:marker3', highlightText( () => null ) );
  223. setModelData( model, 'foobar' );
  224. model.change( writer => {
  225. const range = writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 5 ) );
  226. marker = writer.addMarker( 'marker3', { range, usingOperation: false } );
  227. writer.setSelection( 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( model.createRangeIn( 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._appendChild( [
  246. new ViewUIElement( 'span' ),
  247. new ViewUIElement( 'span' )
  248. ] );
  249. model.change( writer => {
  250. writer.setSelection( writer.createRange( writer.createPositionFromPath( 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( writer.createRange( writer.createPositionFromPath( modelRoot, [ 1 ] ) ) );
  266. writer.setSelectionAttribute( 'bold', true );
  267. } );
  268. // Convert model to view.
  269. view.change( writer => {
  270. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  271. // Add ui element to view.
  272. const uiElement = new ViewUIElement( 'span' );
  273. viewRoot._insertChild( 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( writer.createRange( writer.createPositionFromPath( modelRoot, [ 1 ] ) ) );
  285. writer.setSelectionAttribute( 'bold', true );
  286. } );
  287. // Convert model to view.
  288. view.change( writer => {
  289. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  290. // Add ui element to view.
  291. const uiElement = new ViewUIElement( 'span' );
  292. viewRoot._insertChild( 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, conversionApi ) => {
  303. expect( conversionApi.consumable.consume( data.selection, 'selection' ) ).to.be.true;
  304. }, { priority: 'high' } );
  305. dispatcher.on( 'attribute:bold', ( evt, data, conversionApi ) => {
  306. expect( conversionApi.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 = model.createRange( model.createPositionAt( modelRoot, 1 ), model.createPositionAt( 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 = model.createRange( model.createPositionAt( modelRoot, 1 ), model.createPositionAt( 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. it( 'should clear fake selection', () => {
  388. const modelRange = model.createRange( model.createPositionAt( modelRoot, 1 ), model.createPositionAt( modelRoot, 1 ) );
  389. view.change( writer => {
  390. writer.setSelection( modelRange, { fake: true } );
  391. dispatcher.convertSelection( docSelection, model.markers, writer );
  392. } );
  393. expect( viewSelection.isFake ).to.be.false;
  394. } );
  395. } );
  396. } );
  397. describe( 'table cell selection converter', () => {
  398. beforeEach( () => {
  399. model.schema.register( 'table', { isLimit: true } );
  400. model.schema.register( 'tr', { isLimit: true } );
  401. model.schema.register( 'td', { isLimit: true } );
  402. model.schema.extend( 'table', { allowIn: '$root' } );
  403. model.schema.extend( 'tr', { allowIn: 'table' } );
  404. model.schema.extend( 'td', { allowIn: 'tr' } );
  405. model.schema.extend( '$text', { allowIn: 'td' } );
  406. // "Universal" converter to convert table structure.
  407. const containerCreator = ( modelElement, viewWriter ) => viewWriter.createContainerElement( modelElement.name );
  408. const tableConverter = insertElement( containerCreator );
  409. dispatcher.on( 'insert:table', tableConverter );
  410. dispatcher.on( 'insert:tr', tableConverter );
  411. dispatcher.on( 'insert:td', tableConverter );
  412. // Special converter for table cells.
  413. dispatcher.on( 'selection', ( evt, data, conversionApi ) => {
  414. const selection = data.selection;
  415. if ( !conversionApi.consumable.test( selection, 'selection' ) || selection.isCollapsed ) {
  416. return;
  417. }
  418. for ( const range of selection.getRanges() ) {
  419. const node = range.start.parent;
  420. if ( !!node && node.is( 'td' ) ) {
  421. conversionApi.consumable.consume( selection, 'selection' );
  422. const viewNode = conversionApi.mapper.toViewElement( node );
  423. conversionApi.writer.addClass( 'selected', viewNode );
  424. }
  425. }
  426. }, { priority: 'high' } );
  427. } );
  428. it( 'should not be used to convert selection that is not on table cell', () => {
  429. test(
  430. [ 1, 5 ],
  431. 'f{o<$text bold="true">ob</$text>a}r',
  432. 'f{o<strong>ob</strong>a}r'
  433. );
  434. } );
  435. it( 'should add a class to the selected table cell', () => {
  436. test(
  437. // table tr#0 td#0 [foo, table tr#0 td#0 bar]
  438. [ [ 0, 0, 0, 0 ], [ 0, 0, 0, 3 ] ],
  439. '<table><tr><td>foo</td></tr><tr><td>bar</td></tr></table>',
  440. '<table><tr><td class="selected">foo</td></tr><tr><td>bar</td></tr></table>'
  441. );
  442. } );
  443. it( 'should not be used if selection contains more than just a table cell', () => {
  444. test(
  445. // table tr td#1 f{oo bar, table tr#2 bar]
  446. [ [ 0, 0, 0, 1 ], [ 0, 0, 1, 3 ] ],
  447. '<table><tr><td>foo</td><td>bar</td></tr></table>',
  448. '[<table><tr><td>foo</td><td>bar</td></tr></table>]'
  449. );
  450. } );
  451. } );
  452. // Tests if the selection got correctly converted.
  453. // Because `setData` might use selection converters itself to set the selection, we can't use it
  454. // to set the selection (because then we would test converters using converters).
  455. // Instead, the `test` function expects to be passed `selectionPaths` which is an array containing two numbers or two arrays,
  456. // that are offsets or paths of selection positions in root element.
  457. function test( selectionPaths, modelInput, expectedView, selectionAttributes = {} ) {
  458. // Parse passed `modelInput` string and set it as current model.
  459. setModelData( model, modelInput );
  460. // Manually set selection ranges using passed `selectionPaths`.
  461. const startPath = typeof selectionPaths[ 0 ] == 'number' ? [ selectionPaths[ 0 ] ] : selectionPaths[ 0 ];
  462. const endPath = typeof selectionPaths[ 1 ] == 'number' ? [ selectionPaths[ 1 ] ] : selectionPaths[ 1 ];
  463. const startPos = model.createPositionFromPath( modelRoot, startPath );
  464. const endPos = model.createPositionFromPath( modelRoot, endPath );
  465. const isBackward = selectionPaths[ 2 ] === 'backward';
  466. model.change( writer => {
  467. writer.setSelection( writer.createRange( startPos, endPos ), { backward: isBackward } );
  468. // And add or remove passed attributes.
  469. for ( const key in selectionAttributes ) {
  470. const value = selectionAttributes[ key ];
  471. if ( value ) {
  472. writer.setSelectionAttribute( key, value );
  473. } else {
  474. writer.removeSelectionAttribute( key );
  475. }
  476. }
  477. } );
  478. // Remove view children manually (without firing additional conversion).
  479. viewRoot._removeChildren( 0, viewRoot.childCount );
  480. // Convert model to view.
  481. view.change( writer => {
  482. dispatcher.convertInsert( model.createRangeIn( modelRoot ), writer );
  483. dispatcher.convertSelection( docSelection, model.markers, writer );
  484. } );
  485. // Stringify view and check if it is same as expected.
  486. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) ).to.equal( '<div>' + expectedView + '</div>' );
  487. }
  488. } );