8
0

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

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