8
0

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /**
  2. * @license Copyright (c) 2003-2017, 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. convertSelectionAttribute,
  20. convertSelectionMarker,
  21. clearAttributes,
  22. clearFakeSelection
  23. } from '../../src/conversion/model-selection-to-view-converters';
  24. import {
  25. insertElement,
  26. insertText,
  27. wrapItem,
  28. highlightText,
  29. highlightElement
  30. } from '../../src/conversion/model-to-view-converters';
  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.allow( { name: '$text', inside: '$root' } );
  41. viewDoc = new ViewDocument();
  42. viewRoot = viewDoc.createRoot( 'div' );
  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( 'addAttribute:bold', wrapItem( new ViewAttributeElement( 'strong' ) ) );
  50. dispatcher.on( 'addMarker:marker', highlightText( highlightDescriptor ) );
  51. dispatcher.on( 'addMarker:marker', highlightElement( highlightDescriptor ) );
  52. dispatcher.on( 'removeMarker:marker', highlightText( highlightDescriptor ) );
  53. dispatcher.on( 'removeMarker:marker', highlightElement( highlightDescriptor ) );
  54. // Default selection converters.
  55. dispatcher.on( 'selection', clearAttributes(), { priority: 'low' } );
  56. dispatcher.on( 'selection', convertRangeSelection(), { priority: 'low' } );
  57. dispatcher.on( 'selection', convertCollapsedSelection(), { priority: 'low' } );
  58. } );
  59. afterEach( () => {
  60. viewDoc.destroy();
  61. } );
  62. describe( 'default converters', () => {
  63. beforeEach( () => {
  64. // Selection converters for selection attributes.
  65. dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( new ViewAttributeElement( 'strong' ) ) );
  66. dispatcher.on( 'selectionAttribute:italic', convertSelectionAttribute( new ViewAttributeElement( 'em' ) ) );
  67. dispatcher.on( 'selectionMarker:marker', convertSelectionMarker( highlightDescriptor ) );
  68. } );
  69. describe( 'range selection', () => {
  70. it( 'in same container', () => {
  71. test(
  72. [ 1, 4 ],
  73. 'foobar',
  74. 'f{oob}ar'
  75. );
  76. } );
  77. it( 'in same container with unicode characters', () => {
  78. test(
  79. [ 2, 6 ],
  80. 'நிலைக்கு',
  81. 'நி{லைக்}கு'
  82. );
  83. } );
  84. it( 'in same container, over attribute', () => {
  85. test(
  86. [ 1, 5 ],
  87. 'fo<$text bold="true">ob</$text>ar',
  88. 'f{o<strong>ob</strong>a}r'
  89. );
  90. } );
  91. it( 'in same container, next to attribute', () => {
  92. test(
  93. [ 1, 2 ],
  94. 'fo<$text bold="true">ob</$text>ar',
  95. 'f{o}<strong>ob</strong>ar'
  96. );
  97. } );
  98. it( 'in same attribute', () => {
  99. test(
  100. [ 2, 4 ],
  101. 'f<$text bold="true">ooba</$text>r',
  102. 'f<strong>o{ob}a</strong>r'
  103. );
  104. } );
  105. it( 'in same attribute, selection same as attribute', () => {
  106. test(
  107. [ 2, 4 ],
  108. 'fo<$text bold="true">ob</$text>ar',
  109. 'fo{<strong>ob</strong>}ar'
  110. );
  111. } );
  112. it( 'starts in text node, ends in attribute #1', () => {
  113. test(
  114. [ 1, 3 ],
  115. 'fo<$text bold="true">ob</$text>ar',
  116. 'f{o<strong>o}b</strong>ar'
  117. );
  118. } );
  119. it( 'starts in text node, ends in attribute #2', () => {
  120. test(
  121. [ 1, 4 ],
  122. 'fo<$text bold="true">ob</$text>ar',
  123. 'f{o<strong>ob</strong>}ar'
  124. );
  125. } );
  126. it( 'starts in attribute, ends in text node', () => {
  127. test(
  128. [ 3, 5 ],
  129. 'fo<$text bold="true">ob</$text>ar',
  130. 'fo<strong>o{b</strong>a}r'
  131. );
  132. } );
  133. it( 'consumes consumable values properly', () => {
  134. // Add callback that will fire before default ones.
  135. // This should prevent default callback doing anything.
  136. dispatcher.on( 'selection', ( evt, data, consumable ) => {
  137. expect( consumable.consume( data.selection, 'selection' ) ).to.be.true;
  138. }, { priority: 'high' } );
  139. // Similar test case as the first in this suite.
  140. test(
  141. [ 1, 4 ],
  142. 'foobar',
  143. 'foobar' // No selection in view.
  144. );
  145. } );
  146. it( 'should convert backward selection', () => {
  147. test(
  148. [ 1, 3, 'backward' ],
  149. 'foobar',
  150. 'f{oo}bar'
  151. );
  152. expect( viewSelection.focus.offset ).to.equal( 1 );
  153. } );
  154. } );
  155. describe( 'collapsed selection', () => {
  156. it( 'in container', () => {
  157. test(
  158. [ 1, 1 ],
  159. 'foobar',
  160. 'f{}oobar'
  161. );
  162. } );
  163. it( 'in attribute', () => {
  164. test(
  165. [ 3, 3 ],
  166. 'f<$text bold="true">ooba</$text>r',
  167. 'f<strong>oo{}ba</strong>r'
  168. );
  169. } );
  170. it( 'in container with extra attributes', () => {
  171. test(
  172. [ 1, 1 ],
  173. 'foobar',
  174. 'f<em>[]</em>oobar',
  175. { italic: true }
  176. );
  177. } );
  178. it( 'in attribute with extra attributes', () => {
  179. test(
  180. [ 3, 3 ],
  181. 'f<$text bold="true">ooba</$text>r',
  182. 'f<strong>oo</strong><em><strong>[]</strong></em><strong>ba</strong>r',
  183. { italic: true }
  184. );
  185. } );
  186. it( 'in attribute and marker', () => {
  187. setModelData( model, 'fo<$text bold="true">ob</$text>ar' );
  188. const marker = model.markers.set( 'marker', ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 5 ) );
  189. modelSelection.setRanges( [ new ModelRange( ModelPosition.createAt( modelRoot, 3 ) ) ] );
  190. // Update selection attributes according to model.
  191. modelSelection.refreshAttributes();
  192. // Remove view children manually (without firing additional conversion).
  193. viewRoot.removeChildren( 0, viewRoot.childCount );
  194. // Convert model to view.
  195. dispatcher.convertInsertion( ModelRange.createIn( modelRoot ) );
  196. dispatcher.convertMarker( 'addMarker', marker.name, marker.getRange() );
  197. const markers = Array.from( model.markers.getMarkersAtPosition( modelSelection.getFirstPosition() ) );
  198. dispatcher.convertSelection( modelSelection, markers );
  199. // Stringify view and check if it is same as expected.
  200. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) ).to.equal(
  201. '<div>f<span class="marker">o<strong>o{}b</strong>a</span>r</div>'
  202. );
  203. } );
  204. it( 'in attribute and marker - no attribute', () => {
  205. setModelData( model, 'fo<$text bold="true">ob</$text>ar' );
  206. const marker = model.markers.set( 'marker', ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 5 ) );
  207. modelSelection.setRanges( [ new ModelRange( ModelPosition.createAt( modelRoot, 3 ) ) ] );
  208. // Update selection attributes according to model.
  209. modelSelection.refreshAttributes();
  210. modelSelection.removeAttribute( 'bold' );
  211. // Remove view children manually (without firing additional conversion).
  212. viewRoot.removeChildren( 0, viewRoot.childCount );
  213. // Convert model to view.
  214. dispatcher.convertInsertion( ModelRange.createIn( modelRoot ) );
  215. dispatcher.convertMarker( 'addMarker', marker.name, marker.getRange() );
  216. const markers = Array.from( model.markers.getMarkersAtPosition( modelSelection.getFirstPosition() ) );
  217. dispatcher.convertSelection( modelSelection, markers );
  218. // Stringify view and check if it is same as expected.
  219. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  220. .to.equal( '<div>f<span class="marker">o<strong>o</strong>[]<strong>b</strong>a</span>r</div>' );
  221. } );
  222. it( 'in marker - using highlight descriptor creator', () => {
  223. dispatcher.on( 'selectionMarker:marker2', convertSelectionMarker(
  224. data => ( { 'class': data.markerName } )
  225. ) );
  226. setModelData( model, 'foobar' );
  227. const marker = model.markers.set( 'marker2', ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 5 ) );
  228. modelSelection.setRanges( [ new ModelRange( ModelPosition.createAt( modelRoot, 3 ) ) ] );
  229. // Remove view children manually (without firing additional conversion).
  230. viewRoot.removeChildren( 0, viewRoot.childCount );
  231. // Convert model to view.
  232. dispatcher.convertInsertion( ModelRange.createIn( modelRoot ) );
  233. dispatcher.convertMarker( 'addMarker', marker.name, marker.getRange() );
  234. const markers = Array.from( model.markers.getMarkersAtPosition( modelSelection.getFirstPosition() ) );
  235. dispatcher.convertSelection( modelSelection, markers );
  236. // Stringify view and check if it is same as expected.
  237. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  238. .to.equal( '<div>foo<span class="marker2">[]</span>bar</div>' );
  239. } );
  240. it( 'in marker - should merge with the rest of attribute elements', () => {
  241. dispatcher.on( 'addMarker:marker2', highlightText( data => ( { 'class': data.markerName } ) ) );
  242. dispatcher.on( 'selectionMarker:marker2', convertSelectionMarker( data => ( { 'class': data.markerName } ) ) );
  243. setModelData( model, 'foobar' );
  244. const marker = model.markers.set( 'marker2', ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 5 ) );
  245. modelSelection.setRanges( [ new ModelRange( ModelPosition.createAt( modelRoot, 3 ) ) ] );
  246. // Remove view children manually (without firing additional conversion).
  247. viewRoot.removeChildren( 0, viewRoot.childCount );
  248. // Convert model to view.
  249. dispatcher.convertInsertion( ModelRange.createIn( modelRoot ) );
  250. dispatcher.convertMarker( 'addMarker', marker.name, marker.getRange() );
  251. const markers = Array.from( model.markers.getMarkersAtPosition( modelSelection.getFirstPosition() ) );
  252. dispatcher.convertSelection( modelSelection, markers );
  253. // Stringify view and check if it is same as expected.
  254. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  255. .to.equal( '<div>f<span class="marker2">oo{}ba</span>r</div>' );
  256. } );
  257. it( 'should do nothing if creator return null', () => {
  258. dispatcher.on( 'selectionMarker:marker3', convertSelectionMarker( () => {
  259. } ) );
  260. setModelData( model, 'foobar' );
  261. const marker = model.markers.set( 'marker3', ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 5 ) );
  262. modelSelection.setRanges( [ new ModelRange( ModelPosition.createAt( modelRoot, 3 ) ) ] );
  263. // Remove view children manually (without firing additional conversion).
  264. viewRoot.removeChildren( 0, viewRoot.childCount );
  265. // Convert model to view.
  266. dispatcher.convertInsertion( ModelRange.createIn( modelRoot ) );
  267. dispatcher.convertMarker( 'addMarker', marker.name, marker.getRange() );
  268. const markers = Array.from( model.markers.getMarkersAtPosition( modelSelection.getFirstPosition() ) );
  269. dispatcher.convertSelection( modelSelection, markers );
  270. // Stringify view and check if it is same as expected.
  271. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  272. .to.equal( '<div>foo{}bar</div>' );
  273. } );
  274. // #1072 - if the container has only ui elements, collapsed selection attribute should be rendered after those ui elements.
  275. it( 'selection with attribute before ui element - no non-ui children', () => {
  276. setModelData( model, '' );
  277. // Add two ui elements to view.
  278. viewRoot.appendChildren( [
  279. new ViewUIElement( 'span' ),
  280. new ViewUIElement( 'span' )
  281. ] );
  282. modelSelection.setRanges( [ new ModelRange( new ModelPosition( modelRoot, [ 0 ] ) ) ] );
  283. modelSelection.setAttribute( 'bold', true );
  284. // Convert model to view.
  285. dispatcher.convertSelection( modelSelection, [] );
  286. // Stringify view and check if it is same as expected.
  287. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  288. .to.equal( '<div><span></span><span></span><strong>[]</strong></div>' );
  289. } );
  290. // #1072.
  291. it( 'selection with attribute before ui element - has non-ui children #1', () => {
  292. setModelData( model, 'x' );
  293. modelSelection.setRanges( [ new ModelRange( new ModelPosition( modelRoot, [ 1 ] ) ) ] );
  294. modelSelection.setAttribute( 'bold', true );
  295. // Convert model to view.
  296. dispatcher.convertInsertion( ModelRange.createIn( modelRoot ) );
  297. // Add ui element to view.
  298. const uiElement = new ViewUIElement( 'span' );
  299. viewRoot.insertChildren( 1, uiElement );
  300. dispatcher.convertSelection( modelSelection, [] );
  301. // Stringify view and check if it is same as expected.
  302. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  303. .to.equal( '<div>x<strong>[]</strong><span></span></div>' );
  304. } );
  305. // #1072.
  306. it( 'selection with attribute before ui element - has non-ui children #2', () => {
  307. setModelData( model, '<$text bold="true">x</$text>y' );
  308. modelSelection.setRanges( [ new ModelRange( new ModelPosition( modelRoot, [ 1 ] ) ) ] );
  309. modelSelection.setAttribute( 'bold', true );
  310. // Convert model to view.
  311. dispatcher.convertInsertion( ModelRange.createIn( modelRoot ) );
  312. // Add ui element to view.
  313. const uiElement = new ViewUIElement( 'span' );
  314. viewRoot.insertChildren( 1, uiElement );
  315. dispatcher.convertSelection( modelSelection, [] );
  316. // Stringify view and check if it is same as expected.
  317. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  318. .to.equal( '<div><strong>x{}</strong><span></span>y</div>' );
  319. } );
  320. it( 'consumes consumable values properly', () => {
  321. // Add callbacks that will fire before default ones.
  322. // This should prevent default callbacks doing anything.
  323. dispatcher.on( 'selection', ( evt, data, consumable ) => {
  324. expect( consumable.consume( data.selection, 'selection' ) ).to.be.true;
  325. }, { priority: 'high' } );
  326. dispatcher.on( 'selectionAttribute:bold', ( evt, data, consumable ) => {
  327. expect( consumable.consume( data.selection, 'selectionAttribute:bold' ) ).to.be.true;
  328. }, { priority: 'high' } );
  329. // Similar test case as above.
  330. test(
  331. [ 3, 3 ],
  332. 'f<$text bold="true">ooba</$text>r',
  333. 'f<strong>ooba</strong>r' // No selection in view.
  334. );
  335. } );
  336. } );
  337. } );
  338. describe( 'clean-up', () => {
  339. describe( 'convertRangeSelection', () => {
  340. it( 'should remove all ranges before adding new range', () => {
  341. test(
  342. [ 0, 2 ],
  343. 'foobar',
  344. '{fo}obar'
  345. );
  346. test(
  347. [ 3, 5 ],
  348. 'foobar',
  349. 'foo{ba}r'
  350. );
  351. expect( viewSelection.rangeCount ).to.equal( 1 );
  352. } );
  353. } );
  354. describe( 'convertCollapsedSelection', () => {
  355. it( 'should remove all ranges before adding new range', () => {
  356. test(
  357. [ 2, 2 ],
  358. 'foobar',
  359. 'fo{}obar'
  360. );
  361. test(
  362. [ 3, 3 ],
  363. 'foobar',
  364. 'foo{}bar'
  365. );
  366. expect( viewSelection.rangeCount ).to.equal( 1 );
  367. } );
  368. } );
  369. describe( 'clearAttributes', () => {
  370. it( 'should remove all ranges before adding new range', () => {
  371. dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( new ViewAttributeElement( 'b' ) ) );
  372. dispatcher.on( 'addAttribute:style', wrapItem( new ViewAttributeElement( 'b' ) ) );
  373. test(
  374. [ 3, 3 ],
  375. 'foobar',
  376. 'foo<b>[]</b>bar',
  377. { bold: 'true' }
  378. );
  379. const modelRange = ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 1 );
  380. modelDoc.selection.setRanges( [ modelRange ] );
  381. dispatcher.convertSelection( modelDoc.selection, [] );
  382. expect( viewSelection.rangeCount ).to.equal( 1 );
  383. const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
  384. expect( viewString ).to.equal( '<div>f{}oobar</div>' );
  385. } );
  386. it( 'should do nothing if the attribute element had been already removed', () => {
  387. dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( new ViewAttributeElement( 'b' ) ) );
  388. dispatcher.on( 'addAttribute:style', wrapItem( new ViewAttributeElement( 'b' ) ) );
  389. test(
  390. [ 3, 3 ],
  391. 'foobar',
  392. 'foo<b>[]</b>bar',
  393. { bold: 'true' }
  394. );
  395. // Remove <b></b> manually.
  396. mergeAttributes( viewSelection.getFirstPosition() );
  397. const modelRange = ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 1 );
  398. modelDoc.selection.setRanges( [ modelRange ] );
  399. dispatcher.convertSelection( modelDoc.selection, [] );
  400. expect( viewSelection.rangeCount ).to.equal( 1 );
  401. const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
  402. expect( viewString ).to.equal( '<div>f{}oobar</div>' );
  403. } );
  404. } );
  405. describe( 'clearFakeSelection', () => {
  406. it( 'should clear fake selection', () => {
  407. dispatcher.on( 'selection', clearFakeSelection() );
  408. viewSelection.setFake( true );
  409. dispatcher.convertSelection( modelSelection, [] );
  410. expect( viewSelection.isFake ).to.be.false;
  411. } );
  412. } );
  413. } );
  414. describe( 'using element creator for attributes conversion', () => {
  415. beforeEach( () => {
  416. function themeElementCreator( themeValue ) {
  417. if ( themeValue == 'important' ) {
  418. return new ViewAttributeElement( 'strong', { style: 'text-transform:uppercase' } );
  419. } else if ( themeValue == 'gold' ) {
  420. return new ViewAttributeElement( 'span', { style: 'color:yellow' } );
  421. }
  422. }
  423. dispatcher.on( 'selectionAttribute:theme', convertSelectionAttribute( themeElementCreator ) );
  424. dispatcher.on( 'addAttribute:theme', wrapItem( themeElementCreator ) );
  425. dispatcher.on( 'selectionAttribute:italic', convertSelectionAttribute( new ViewAttributeElement( 'em' ) ) );
  426. } );
  427. describe( 'range selection', () => {
  428. it( 'in same container, over attribute', () => {
  429. test(
  430. [ 1, 5 ],
  431. 'fo<$text theme="gold">ob</$text>ar',
  432. 'f{o<span style="color:yellow">ob</span>a}r'
  433. );
  434. } );
  435. it( 'in same attribute', () => {
  436. test(
  437. [ 2, 4 ],
  438. 'f<$text theme="gold">ooba</$text>r',
  439. 'f<span style="color:yellow">o{ob}a</span>r'
  440. );
  441. } );
  442. it( 'in same attribute, selection same as attribute', () => {
  443. test(
  444. [ 2, 4 ],
  445. 'fo<$text theme="important">ob</$text>ar',
  446. 'fo{<strong style="text-transform:uppercase">ob</strong>}ar'
  447. );
  448. } );
  449. it( 'starts in attribute, ends in text node', () => {
  450. test(
  451. [ 3, 5 ],
  452. 'fo<$text theme="important">ob</$text>ar',
  453. 'fo<strong style="text-transform:uppercase">o{b</strong>a}r'
  454. );
  455. } );
  456. } );
  457. describe( 'collapsed selection', () => {
  458. it( 'in attribute', () => {
  459. test(
  460. [ 3, 3 ],
  461. 'f<$text theme="gold">ooba</$text>r',
  462. 'f<span style="color:yellow">oo{}ba</span>r'
  463. );
  464. } );
  465. it( 'in container with theme attribute', () => {
  466. test(
  467. [ 1, 1 ],
  468. 'foobar',
  469. 'f<strong style="text-transform:uppercase">[]</strong>oobar',
  470. { theme: 'important' }
  471. );
  472. } );
  473. it( 'in theme attribute with extra attributes #1', () => {
  474. test(
  475. [ 3, 3 ],
  476. 'f<$text theme="gold">ooba</$text>r',
  477. 'f<span style="color:yellow">oo</span>' +
  478. '<em><span style="color:yellow">[]</span></em>' +
  479. '<span style="color:yellow">ba</span>r',
  480. { italic: true }
  481. );
  482. } );
  483. it( 'in theme attribute with extra attributes #2', () => {
  484. // In contrary to test above, we don't have strong + span on the selection.
  485. // This is because strong and span are both created by the same attribute.
  486. // Since style="important" overwrites style="gold" on selection, we have only strong element.
  487. // In example above, selection has both style and italic attribute.
  488. test(
  489. [ 3, 3 ],
  490. 'f<$text theme="gold">ooba</$text>r',
  491. 'f<span style="color:yellow">oo</span>' +
  492. '<strong style="text-transform:uppercase">[]</strong>' +
  493. '<span style="color:yellow">ba</span>r',
  494. { theme: 'important' }
  495. );
  496. } );
  497. it( 'convertSelectionAttribute should do nothing if creator return null', () => {
  498. dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( () => {
  499. } ) );
  500. test(
  501. [ 3, 3 ],
  502. 'foobar',
  503. 'foo{}bar',
  504. { bold: 'true' }
  505. );
  506. } );
  507. } );
  508. } );
  509. describe( 'table cell selection converter', () => {
  510. beforeEach( () => {
  511. model.schema.registerItem( 'table' );
  512. model.schema.registerItem( 'tr' );
  513. model.schema.registerItem( 'td' );
  514. model.schema.allow( { name: 'table', inside: '$root' } );
  515. model.schema.allow( { name: 'tr', inside: 'table' } );
  516. model.schema.allow( { name: 'td', inside: 'tr' } );
  517. model.schema.allow( { name: '$text', inside: 'td' } );
  518. // "Universal" converter to convert table structure.
  519. const tableConverter = insertElement( data => new ViewContainerElement( data.item.name ) );
  520. dispatcher.on( 'insert:table', tableConverter );
  521. dispatcher.on( 'insert:tr', tableConverter );
  522. dispatcher.on( 'insert:td', tableConverter );
  523. // Special converter for table cells.
  524. dispatcher.on( 'selection', ( evt, data, consumable, conversionApi ) => {
  525. const selection = data.selection;
  526. if ( !consumable.test( selection, 'selection' ) || selection.isCollapsed ) {
  527. return;
  528. }
  529. for ( const range of selection.getRanges() ) {
  530. const node = range.start.nodeAfter;
  531. if ( node == range.end.nodeBefore && node instanceof ModelElement && node.name == 'td' ) {
  532. consumable.consume( selection, 'selection' );
  533. const viewNode = conversionApi.mapper.toViewElement( node );
  534. viewNode.addClass( 'selected' );
  535. }
  536. }
  537. } );
  538. } );
  539. it( 'should not be used to convert selection that is not on table cell', () => {
  540. test(
  541. [ 1, 5 ],
  542. 'f{o<$text bold="true">ob</$text>a}r',
  543. 'f{o<strong>ob</strong>a}r'
  544. );
  545. } );
  546. it( 'should add a class to the selected table cell', () => {
  547. test(
  548. // table tr#0 |td#0, table tr#0 td#0|
  549. [ [ 0, 0, 0 ], [ 0, 0, 1 ] ],
  550. '<table><tr><td>foo</td></tr><tr><td>bar</td></tr></table>',
  551. '<table><tr><td class="selected">foo</td></tr><tr><td>bar</td></tr></table>'
  552. );
  553. } );
  554. it( 'should not be used if selection contains more than just a table cell', () => {
  555. test(
  556. // table tr td#1, table tr#2
  557. [ [ 0, 0, 0, 1 ], [ 0, 0, 2 ] ],
  558. '<table><tr><td>foo</td><td>bar</td></tr></table>',
  559. '<table><tr><td>f{oo</td><td>bar</td>]</tr></table>'
  560. );
  561. } );
  562. } );
  563. // Tests if the selection got correctly converted.
  564. // Because `setData` might use selection converters itself to set the selection, we can't use it
  565. // to set the selection (because then we would test converters using converters).
  566. // Instead, the `test` function expects to be passed `selectionPaths` which is an array containing two numbers or two arrays,
  567. // that are offsets or paths of selection positions in root element.
  568. function test( selectionPaths, modelInput, expectedView, selectionAttributes = {} ) {
  569. // Parse passed `modelInput` string and set it as current model.
  570. setModelData( model, modelInput );
  571. // Manually set selection ranges using passed `selectionPaths`.
  572. const startPath = typeof selectionPaths[ 0 ] == 'number' ? [ selectionPaths[ 0 ] ] : selectionPaths[ 0 ];
  573. const endPath = typeof selectionPaths[ 1 ] == 'number' ? [ selectionPaths[ 1 ] ] : selectionPaths[ 1 ];
  574. const startPos = new ModelPosition( modelRoot, startPath );
  575. const endPos = new ModelPosition( modelRoot, endPath );
  576. const isBackward = selectionPaths[ 2 ] === 'backward';
  577. modelSelection.setRanges( [ new ModelRange( startPos, endPos ) ], isBackward );
  578. // Update selection attributes according to model.
  579. modelSelection.refreshAttributes();
  580. // And add or remove passed attributes.
  581. for ( const key in selectionAttributes ) {
  582. const value = selectionAttributes[ key ];
  583. if ( value ) {
  584. modelSelection.setAttribute( key, value );
  585. } else {
  586. modelSelection.removeAttribute( key );
  587. }
  588. }
  589. // Remove view children manually (without firing additional conversion).
  590. viewRoot.removeChildren( 0, viewRoot.childCount );
  591. // Convert model to view.
  592. dispatcher.convertInsertion( ModelRange.createIn( modelRoot ) );
  593. dispatcher.convertSelection( modelSelection, [] );
  594. // Stringify view and check if it is same as expected.
  595. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) ).to.equal( '<div>' + expectedView + '</div>' );
  596. }
  597. } );