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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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 ViewDocument from '../../src/view/document';
  10. import ViewContainerElement from '../../src/view/containerelement';
  11. import ViewAttributeElement from '../../src/view/attributeelement';
  12. import ViewUIElement from '../../src/view/uielement';
  13. import { mergeAttributes } from '../../src/view/writer';
  14. import Mapper from '../../src/conversion/mapper';
  15. import ModelConversionDispatcher from '../../src/conversion/modelconversiondispatcher';
  16. import {
  17. convertRangeSelection,
  18. convertCollapsedSelection,
  19. clearAttributes,
  20. clearFakeSelection
  21. } from '../../src/conversion/model-selection-to-view-converters';
  22. import {
  23. insertElement,
  24. insertText,
  25. wrap,
  26. highlightElement,
  27. highlightText,
  28. removeHighlight
  29. } from '../../src/conversion/model-to-view-converters';
  30. import createViewRoot from '../view/_utils/createroot';
  31. import { stringify as stringifyView } from '../../src/dev-utils/view';
  32. import { setData as setModelData } from '../../src/dev-utils/model';
  33. describe( 'model-selection-to-view-converters', () => {
  34. let dispatcher, mapper, model, modelDoc, modelRoot, docSelection, viewDoc, viewRoot, viewSelection, highlightDescriptor;
  35. beforeEach( () => {
  36. model = new Model();
  37. modelDoc = model.document;
  38. modelRoot = modelDoc.createRoot();
  39. docSelection = modelDoc.selection;
  40. model.schema.extend( '$text', { allowIn: '$root' } );
  41. viewDoc = new ViewDocument();
  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. viewDoc.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. let marker;
  150. it( 'in container', () => {
  151. test(
  152. [ 1, 1 ],
  153. 'foobar',
  154. 'f{}oobar'
  155. );
  156. } );
  157. it( 'in attribute', () => {
  158. test(
  159. [ 3, 3 ],
  160. 'f<$text bold="true">ooba</$text>r',
  161. 'f<strong>oo{}ba</strong>r'
  162. );
  163. } );
  164. it( 'in attribute and marker', () => {
  165. setModelData( model, 'fo<$text bold="true">ob</$text>ar' );
  166. model.change( writer => {
  167. marker = writer.setMarker( 'marker', ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 5 ) );
  168. writer.setSelection( new ModelRange( ModelPosition.createAt( modelRoot, 3 ) ) );
  169. } );
  170. // Remove view children manually (without firing additional conversion).
  171. viewRoot.removeChildren( 0, viewRoot.childCount );
  172. // Convert model to view.
  173. dispatcher.convertInsert( ModelRange.createIn( modelRoot ) );
  174. dispatcher.convertMarkerAdd( marker.name, marker.getRange() );
  175. const markers = Array.from( model.markers.getMarkersAtPosition( docSelection.getFirstPosition() ) );
  176. dispatcher.convertSelection( docSelection, markers );
  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. dispatcher.convertInsert( ModelRange.createIn( modelRoot ) );
  193. dispatcher.convertMarkerAdd( marker.name, marker.getRange() );
  194. const markers = Array.from( model.markers.getMarkersAtPosition( docSelection.getFirstPosition() ) );
  195. dispatcher.convertSelection( docSelection, markers );
  196. // Stringify view and check if it is same as expected.
  197. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  198. .to.equal( '<div>f<span class="marker">o<strong>o</strong>[]<strong>b</strong>a</span>r</div>' );
  199. } );
  200. it( 'in marker - using highlight descriptor creator', () => {
  201. dispatcher.on( 'addMarker:marker2', highlightText(
  202. data => ( { 'class': data.markerName } )
  203. ) );
  204. setModelData( model, 'foobar' );
  205. model.change( writer => {
  206. marker = writer.setMarker( 'marker2', ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 5 ) );
  207. writer.setSelection( new ModelRange( ModelPosition.createAt( modelRoot, 3 ) ) );
  208. } );
  209. // Remove view children manually (without firing additional conversion).
  210. viewRoot.removeChildren( 0, viewRoot.childCount );
  211. // Convert model to view.
  212. dispatcher.convertInsert( ModelRange.createIn( modelRoot ) );
  213. dispatcher.convertMarkerAdd( marker.name, marker.getRange() );
  214. const markers = Array.from( model.markers.getMarkersAtPosition( docSelection.getFirstPosition() ) );
  215. dispatcher.convertSelection( docSelection, 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 class="marker2">oo{}ba</span>r</div>' );
  219. } );
  220. it( 'should do nothing if creator return null', () => {
  221. dispatcher.on( 'addMarker:marker3', highlightText( () => null ) );
  222. setModelData( model, 'foobar' );
  223. model.change( writer => {
  224. marker = writer.setMarker( 'marker3', ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 5 ) );
  225. writer.setSelection( new ModelRange( ModelPosition.createAt( modelRoot, 3 ) ) );
  226. } );
  227. // Remove view children manually (without firing additional conversion).
  228. viewRoot.removeChildren( 0, viewRoot.childCount );
  229. // Convert model to view.
  230. dispatcher.convertInsert( ModelRange.createIn( modelRoot ) );
  231. dispatcher.convertMarkerAdd( marker.name, marker.getRange() );
  232. const markers = Array.from( model.markers.getMarkersAtPosition( docSelection.getFirstPosition() ) );
  233. dispatcher.convertSelection( docSelection, markers );
  234. // Stringify view and check if it is same as expected.
  235. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  236. .to.equal( '<div>foo{}bar</div>' );
  237. } );
  238. // #1072 - if the container has only ui elements, collapsed selection attribute should be rendered after those ui elements.
  239. it( 'selection with attribute before ui element - no non-ui children', () => {
  240. setModelData( model, '' );
  241. // Add two ui elements to view.
  242. viewRoot.appendChildren( [
  243. new ViewUIElement( 'span' ),
  244. new ViewUIElement( 'span' )
  245. ] );
  246. model.change( writer => {
  247. writer.setSelection( new ModelRange( new ModelPosition( modelRoot, [ 0 ] ) ) );
  248. writer.setSelectionAttribute( 'bold', true );
  249. } );
  250. // Convert model to view.
  251. dispatcher.convertSelection( docSelection, [] );
  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( new ModelRange( new ModelPosition( modelRoot, [ 1 ] ) ) );
  261. writer.setSelectionAttribute( 'bold', true );
  262. } );
  263. // Convert model to view.
  264. dispatcher.convertInsert( ModelRange.createIn( modelRoot ) );
  265. // Add ui element to view.
  266. const uiElement = new ViewUIElement( 'span' );
  267. viewRoot.insertChildren( 1, uiElement );
  268. dispatcher.convertSelection( docSelection, [] );
  269. // Stringify view and check if it is same as expected.
  270. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  271. .to.equal( '<div>x<strong>[]</strong><span></span></div>' );
  272. } );
  273. // #1072.
  274. it( 'selection with attribute before ui element - has non-ui children #2', () => {
  275. setModelData( model, '<$text bold="true">x</$text>y' );
  276. model.change( writer => {
  277. writer.setSelection( new ModelRange( new ModelPosition( modelRoot, [ 1 ] ) ) );
  278. writer.setSelectionAttribute( 'bold', true );
  279. } );
  280. // Convert model to view.
  281. dispatcher.convertInsert( ModelRange.createIn( modelRoot ) );
  282. // Add ui element to view.
  283. const uiElement = new ViewUIElement( 'span' );
  284. viewRoot.insertChildren( 1, uiElement );
  285. dispatcher.convertSelection( docSelection, [] );
  286. // Stringify view and check if it is same as expected.
  287. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  288. .to.equal( '<div><strong>x{}</strong><span></span>y</div>' );
  289. } );
  290. it( 'consumes consumable values properly', () => {
  291. // Add callbacks that will fire before default ones.
  292. // This should prevent default callbacks doing anything.
  293. dispatcher.on( 'selection', ( evt, data, consumable ) => {
  294. expect( consumable.consume( data.selection, 'selection' ) ).to.be.true;
  295. }, { priority: 'high' } );
  296. dispatcher.on( 'attribute:bold', ( evt, data, consumable ) => {
  297. expect( consumable.consume( data.item, 'attribute:bold' ) ).to.be.true;
  298. }, { priority: 'high' } );
  299. // Similar test case as above.
  300. test(
  301. [ 3, 3 ],
  302. 'f<$text bold="true">ooba</$text>r',
  303. 'foobar' // No selection in view and no attribute.
  304. );
  305. } );
  306. } );
  307. } );
  308. describe( 'clean-up', () => {
  309. describe( 'convertRangeSelection', () => {
  310. it( 'should remove all ranges before adding new range', () => {
  311. test(
  312. [ 0, 2 ],
  313. 'foobar',
  314. '{fo}obar'
  315. );
  316. test(
  317. [ 3, 5 ],
  318. 'foobar',
  319. 'foo{ba}r'
  320. );
  321. expect( viewSelection.rangeCount ).to.equal( 1 );
  322. } );
  323. } );
  324. describe( 'convertCollapsedSelection', () => {
  325. it( 'should remove all ranges before adding new range', () => {
  326. test(
  327. [ 2, 2 ],
  328. 'foobar',
  329. 'fo{}obar'
  330. );
  331. test(
  332. [ 3, 3 ],
  333. 'foobar',
  334. 'foo{}bar'
  335. );
  336. expect( viewSelection.rangeCount ).to.equal( 1 );
  337. } );
  338. } );
  339. describe( 'clearAttributes', () => {
  340. it( 'should remove all ranges before adding new range', () => {
  341. test(
  342. [ 3, 3 ],
  343. 'foobar',
  344. 'foo<strong>[]</strong>bar',
  345. { bold: 'true' }
  346. );
  347. const modelRange = ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 1 );
  348. model.change( writer => {
  349. writer.setSelection( modelRange );
  350. } );
  351. dispatcher.convertSelection( modelDoc.selection, [] );
  352. expect( viewSelection.rangeCount ).to.equal( 1 );
  353. const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
  354. expect( viewString ).to.equal( '<div>f{}oobar</div>' );
  355. } );
  356. it( 'should do nothing if the attribute element had been already removed', () => {
  357. test(
  358. [ 3, 3 ],
  359. 'foobar',
  360. 'foo<strong>[]</strong>bar',
  361. { bold: 'true' }
  362. );
  363. // Remove <strong></strong> manually.
  364. mergeAttributes( viewSelection.getFirstPosition() );
  365. const modelRange = ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 1 );
  366. model.change( writer => {
  367. writer.setSelection( modelRange );
  368. } );
  369. dispatcher.convertSelection( modelDoc.selection, [] );
  370. expect( viewSelection.rangeCount ).to.equal( 1 );
  371. const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
  372. expect( viewString ).to.equal( '<div>f{}oobar</div>' );
  373. } );
  374. } );
  375. describe( 'clearFakeSelection', () => {
  376. it( 'should clear fake selection', () => {
  377. dispatcher.on( 'selection', clearFakeSelection() );
  378. viewSelection.setFake( true );
  379. dispatcher.convertSelection( docSelection, [] );
  380. expect( viewSelection.isFake ).to.be.false;
  381. } );
  382. } );
  383. } );
  384. describe( 'table cell selection converter', () => {
  385. beforeEach( () => {
  386. model.schema.register( 'table' );
  387. model.schema.register( 'tr' );
  388. model.schema.register( 'td' );
  389. model.schema.extend( 'table', { allowIn: '$root' } );
  390. model.schema.extend( 'tr', { allowIn: 'table' } );
  391. model.schema.extend( 'td', { allowIn: 'tr' } );
  392. model.schema.extend( '$text', { allowIn: 'td' } );
  393. // "Universal" converter to convert table structure.
  394. const tableConverter = insertElement( modelItem => new ViewContainerElement( modelItem.name ) );
  395. dispatcher.on( 'insert:table', tableConverter );
  396. dispatcher.on( 'insert:tr', tableConverter );
  397. dispatcher.on( 'insert:td', tableConverter );
  398. // Special converter for table cells.
  399. dispatcher.on( 'selection', ( evt, data, consumable, conversionApi ) => {
  400. const selection = data.selection;
  401. if ( !consumable.test( selection, 'selection' ) || selection.isCollapsed ) {
  402. return;
  403. }
  404. for ( const range of selection.getRanges() ) {
  405. const node = range.start.nodeAfter;
  406. if ( node == range.end.nodeBefore && node instanceof ModelElement && node.name == 'td' ) {
  407. consumable.consume( selection, 'selection' );
  408. const viewNode = conversionApi.mapper.toViewElement( node );
  409. viewNode.addClass( 'selected' );
  410. }
  411. }
  412. } );
  413. } );
  414. it( 'should not be used to convert selection that is not on table cell', () => {
  415. test(
  416. [ 1, 5 ],
  417. 'f{o<$text bold="true">ob</$text>a}r',
  418. 'f{o<strong>ob</strong>a}r'
  419. );
  420. } );
  421. it( 'should add a class to the selected table cell', () => {
  422. test(
  423. // table tr#0 |td#0, table tr#0 td#0|
  424. [ [ 0, 0, 0 ], [ 0, 0, 1 ] ],
  425. '<table><tr><td>foo</td></tr><tr><td>bar</td></tr></table>',
  426. '<table><tr><td class="selected">foo</td></tr><tr><td>bar</td></tr></table>'
  427. );
  428. } );
  429. it( 'should not be used if selection contains more than just a table cell', () => {
  430. test(
  431. // table tr td#1, table tr#2
  432. [ [ 0, 0, 0, 1 ], [ 0, 0, 2 ] ],
  433. '<table><tr><td>foo</td><td>bar</td></tr></table>',
  434. '<table><tr><td>f{oo</td><td>bar</td>]</tr></table>'
  435. );
  436. } );
  437. } );
  438. // Tests if the selection got correctly converted.
  439. // Because `setData` might use selection converters itself to set the selection, we can't use it
  440. // to set the selection (because then we would test converters using converters).
  441. // Instead, the `test` function expects to be passed `selectionPaths` which is an array containing two numbers or two arrays,
  442. // that are offsets or paths of selection positions in root element.
  443. function test( selectionPaths, modelInput, expectedView, selectionAttributes = {} ) {
  444. // Parse passed `modelInput` string and set it as current model.
  445. setModelData( model, modelInput );
  446. // Manually set selection ranges using passed `selectionPaths`.
  447. const startPath = typeof selectionPaths[ 0 ] == 'number' ? [ selectionPaths[ 0 ] ] : selectionPaths[ 0 ];
  448. const endPath = typeof selectionPaths[ 1 ] == 'number' ? [ selectionPaths[ 1 ] ] : selectionPaths[ 1 ];
  449. const startPos = new ModelPosition( modelRoot, startPath );
  450. const endPos = new ModelPosition( modelRoot, endPath );
  451. const isBackward = selectionPaths[ 2 ] === 'backward';
  452. model.change( writer => {
  453. writer.setSelection( new ModelRange( startPos, endPos ), isBackward );
  454. // And add or remove passed attributes.
  455. for ( const key in selectionAttributes ) {
  456. const value = selectionAttributes[ key ];
  457. if ( value ) {
  458. writer.setSelectionAttribute( key, value );
  459. } else {
  460. writer.removeSelectionAttribute( key );
  461. }
  462. }
  463. } );
  464. // Remove view children manually (without firing additional conversion).
  465. viewRoot.removeChildren( 0, viewRoot.childCount );
  466. // Convert model to view.
  467. dispatcher.convertInsert( ModelRange.createIn( modelRoot ) );
  468. dispatcher.convertSelection( docSelection, [] );
  469. // Stringify view and check if it is same as expected.
  470. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) ).to.equal( '<div>' + expectedView + '</div>' );
  471. }
  472. } );