8
0

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

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