8
0

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

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