downcast-selection-converters.js 19 KB

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