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

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