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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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 { stringify as stringifyView } from '../../src/dev-utils/view';
  33. import { setData as setModelData } from '../../src/dev-utils/model';
  34. describe( 'model-selection-to-view-converters', () => {
  35. let dispatcher, mapper, model, modelDoc, modelRoot, modelSelection, viewDoc, viewRoot, viewSelection, highlightDescriptor;
  36. beforeEach( () => {
  37. model = new Model();
  38. modelDoc = model.document;
  39. modelRoot = modelDoc.createRoot();
  40. modelSelection = modelDoc.selection;
  41. model.schema.extend( '$text', { allowIn: '$root' } );
  42. viewDoc = new ViewDocument();
  43. viewRoot = viewDoc.createRoot( 'div' );
  44. viewSelection = viewDoc.selection;
  45. mapper = new Mapper();
  46. mapper.bindElements( modelRoot, viewRoot );
  47. highlightDescriptor = { class: 'marker', priority: 1 };
  48. dispatcher = new ModelConversionDispatcher( model, { mapper, viewSelection } );
  49. dispatcher.on( 'insert:$text', insertText() );
  50. dispatcher.on( 'attribute:bold', wrap( new ViewAttributeElement( 'strong' ) ) );
  51. dispatcher.on( 'addMarker:marker', highlightText( highlightDescriptor ) );
  52. dispatcher.on( 'addMarker:marker', highlightElement( highlightDescriptor ) );
  53. dispatcher.on( 'removeMarker:marker', removeHighlight( 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.convertInsert( ModelRange.createIn( modelRoot ) );
  196. dispatcher.convertMarkerAdd( 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.convertInsert( ModelRange.createIn( modelRoot ) );
  215. dispatcher.convertMarkerAdd( 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.convertInsert( ModelRange.createIn( modelRoot ) );
  233. dispatcher.convertMarkerAdd( 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( 'addMarker:marker2', highlightElement( data => ( { 'class': data.markerName } ) ) );
  243. dispatcher.on( 'selectionMarker:marker2', convertSelectionMarker( data => ( { 'class': data.markerName } ) ) );
  244. setModelData( model, 'foobar' );
  245. const marker = model.markers.set( 'marker2', ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 5 ) );
  246. modelSelection.setRanges( [ new ModelRange( ModelPosition.createAt( modelRoot, 3 ) ) ] );
  247. // Remove view children manually (without firing additional conversion).
  248. viewRoot.removeChildren( 0, viewRoot.childCount );
  249. // Convert model to view.
  250. dispatcher.convertInsert( ModelRange.createIn( modelRoot ) );
  251. dispatcher.convertMarkerAdd( marker.name, marker.getRange() );
  252. const markers = Array.from( model.markers.getMarkersAtPosition( modelSelection.getFirstPosition() ) );
  253. dispatcher.convertSelection( modelSelection, markers );
  254. // Stringify view and check if it is same as expected.
  255. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  256. .to.equal( '<div>f<span class="marker2">oo{}ba</span>r</div>' );
  257. } );
  258. it( 'should do nothing if creator return null', () => {
  259. dispatcher.on( 'selectionMarker:marker3', convertSelectionMarker( () => {
  260. } ) );
  261. setModelData( model, 'foobar' );
  262. const marker = model.markers.set( 'marker3', ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 5 ) );
  263. modelSelection.setRanges( [ new ModelRange( ModelPosition.createAt( modelRoot, 3 ) ) ] );
  264. // Remove view children manually (without firing additional conversion).
  265. viewRoot.removeChildren( 0, viewRoot.childCount );
  266. // Convert model to view.
  267. dispatcher.convertInsert( ModelRange.createIn( modelRoot ) );
  268. dispatcher.convertMarkerAdd( marker.name, marker.getRange() );
  269. const markers = Array.from( model.markers.getMarkersAtPosition( modelSelection.getFirstPosition() ) );
  270. dispatcher.convertSelection( modelSelection, markers );
  271. // Stringify view and check if it is same as expected.
  272. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  273. .to.equal( '<div>foo{}bar</div>' );
  274. } );
  275. // #1072 - if the container has only ui elements, collapsed selection attribute should be rendered after those ui elements.
  276. it( 'selection with attribute before ui element - no non-ui children', () => {
  277. setModelData( model, '' );
  278. // Add two ui elements to view.
  279. viewRoot.appendChildren( [
  280. new ViewUIElement( 'span' ),
  281. new ViewUIElement( 'span' )
  282. ] );
  283. modelSelection.setRanges( [ new ModelRange( new ModelPosition( modelRoot, [ 0 ] ) ) ] );
  284. modelSelection.setAttribute( 'bold', true );
  285. // Convert model to view.
  286. dispatcher.convertSelection( modelSelection, [] );
  287. // Stringify view and check if it is same as expected.
  288. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  289. .to.equal( '<div><span></span><span></span><strong>[]</strong></div>' );
  290. } );
  291. // #1072.
  292. it( 'selection with attribute before ui element - has non-ui children #1', () => {
  293. setModelData( model, 'x' );
  294. modelSelection.setRanges( [ new ModelRange( new ModelPosition( modelRoot, [ 1 ] ) ) ] );
  295. modelSelection.setAttribute( 'bold', true );
  296. // Convert model to view.
  297. dispatcher.convertInsert( ModelRange.createIn( modelRoot ) );
  298. // Add ui element to view.
  299. const uiElement = new ViewUIElement( 'span' );
  300. viewRoot.insertChildren( 1, uiElement );
  301. dispatcher.convertSelection( modelSelection, [] );
  302. // Stringify view and check if it is same as expected.
  303. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  304. .to.equal( '<div>x<strong>[]</strong><span></span></div>' );
  305. } );
  306. // #1072.
  307. it( 'selection with attribute before ui element - has non-ui children #2', () => {
  308. setModelData( model, '<$text bold="true">x</$text>y' );
  309. modelSelection.setRanges( [ new ModelRange( new ModelPosition( modelRoot, [ 1 ] ) ) ] );
  310. modelSelection.setAttribute( 'bold', true );
  311. // Convert model to view.
  312. dispatcher.convertInsert( ModelRange.createIn( modelRoot ) );
  313. // Add ui element to view.
  314. const uiElement = new ViewUIElement( 'span' );
  315. viewRoot.insertChildren( 1, uiElement );
  316. dispatcher.convertSelection( modelSelection, [] );
  317. // Stringify view and check if it is same as expected.
  318. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) )
  319. .to.equal( '<div><strong>x{}</strong><span></span>y</div>' );
  320. } );
  321. it( 'consumes consumable values properly', () => {
  322. // Add callbacks that will fire before default ones.
  323. // This should prevent default callbacks doing anything.
  324. dispatcher.on( 'selection', ( evt, data, consumable ) => {
  325. expect( consumable.consume( data.selection, 'selection' ) ).to.be.true;
  326. }, { priority: 'high' } );
  327. dispatcher.on( 'selectionAttribute:bold', ( evt, data, consumable ) => {
  328. expect( consumable.consume( data.selection, 'selectionAttribute:bold' ) ).to.be.true;
  329. }, { priority: 'high' } );
  330. // Similar test case as above.
  331. test(
  332. [ 3, 3 ],
  333. 'f<$text bold="true">ooba</$text>r',
  334. 'f<strong>ooba</strong>r' // No selection in view.
  335. );
  336. } );
  337. } );
  338. } );
  339. describe( 'clean-up', () => {
  340. describe( 'convertRangeSelection', () => {
  341. it( 'should remove all ranges before adding new range', () => {
  342. test(
  343. [ 0, 2 ],
  344. 'foobar',
  345. '{fo}obar'
  346. );
  347. test(
  348. [ 3, 5 ],
  349. 'foobar',
  350. 'foo{ba}r'
  351. );
  352. expect( viewSelection.rangeCount ).to.equal( 1 );
  353. } );
  354. } );
  355. describe( 'convertCollapsedSelection', () => {
  356. it( 'should remove all ranges before adding new range', () => {
  357. test(
  358. [ 2, 2 ],
  359. 'foobar',
  360. 'fo{}obar'
  361. );
  362. test(
  363. [ 3, 3 ],
  364. 'foobar',
  365. 'foo{}bar'
  366. );
  367. expect( viewSelection.rangeCount ).to.equal( 1 );
  368. } );
  369. } );
  370. describe( 'clearAttributes', () => {
  371. it( 'should remove all ranges before adding new range', () => {
  372. dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( new ViewAttributeElement( 'b' ) ) );
  373. dispatcher.on( 'attribute:style', wrap( new ViewAttributeElement( 'b' ) ) );
  374. test(
  375. [ 3, 3 ],
  376. 'foobar',
  377. 'foo<b>[]</b>bar',
  378. { bold: 'true' }
  379. );
  380. const modelRange = ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 1 );
  381. modelDoc.selection.setRanges( [ modelRange ] );
  382. dispatcher.convertSelection( modelDoc.selection, [] );
  383. expect( viewSelection.rangeCount ).to.equal( 1 );
  384. const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
  385. expect( viewString ).to.equal( '<div>f{}oobar</div>' );
  386. } );
  387. it( 'should do nothing if the attribute element had been already removed', () => {
  388. dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( new ViewAttributeElement( 'b' ) ) );
  389. dispatcher.on( 'attribute:style', wrap( new ViewAttributeElement( 'b' ) ) );
  390. test(
  391. [ 3, 3 ],
  392. 'foobar',
  393. 'foo<b>[]</b>bar',
  394. { bold: 'true' }
  395. );
  396. // Remove <b></b> manually.
  397. mergeAttributes( viewSelection.getFirstPosition() );
  398. const modelRange = ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 1 );
  399. modelDoc.selection.setRanges( [ modelRange ] );
  400. dispatcher.convertSelection( modelDoc.selection, [] );
  401. expect( viewSelection.rangeCount ).to.equal( 1 );
  402. const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
  403. expect( viewString ).to.equal( '<div>f{}oobar</div>' );
  404. } );
  405. } );
  406. describe( 'clearFakeSelection', () => {
  407. it( 'should clear fake selection', () => {
  408. dispatcher.on( 'selection', clearFakeSelection() );
  409. viewSelection.setFake( true );
  410. dispatcher.convertSelection( modelSelection, [] );
  411. expect( viewSelection.isFake ).to.be.false;
  412. } );
  413. } );
  414. } );
  415. describe( 'using element creator for attributes conversion', () => {
  416. beforeEach( () => {
  417. function themeElementCreator( themeValue ) {
  418. if ( themeValue == 'important' ) {
  419. return new ViewAttributeElement( 'strong', { style: 'text-transform:uppercase' } );
  420. } else if ( themeValue == 'gold' ) {
  421. return new ViewAttributeElement( 'span', { style: 'color:yellow' } );
  422. }
  423. }
  424. dispatcher.on( 'selectionAttribute:theme', convertSelectionAttribute( themeElementCreator ) );
  425. dispatcher.on( 'attribute:theme', wrap( themeElementCreator ) );
  426. dispatcher.on( 'selectionAttribute:italic', convertSelectionAttribute( new ViewAttributeElement( 'em' ) ) );
  427. } );
  428. describe( 'range selection', () => {
  429. it( 'in same container, over attribute', () => {
  430. test(
  431. [ 1, 5 ],
  432. 'fo<$text theme="gold">ob</$text>ar',
  433. 'f{o<span style="color:yellow">ob</span>a}r'
  434. );
  435. } );
  436. it( 'in same attribute', () => {
  437. test(
  438. [ 2, 4 ],
  439. 'f<$text theme="gold">ooba</$text>r',
  440. 'f<span style="color:yellow">o{ob}a</span>r'
  441. );
  442. } );
  443. it( 'in same attribute, selection same as attribute', () => {
  444. test(
  445. [ 2, 4 ],
  446. 'fo<$text theme="important">ob</$text>ar',
  447. 'fo{<strong style="text-transform:uppercase">ob</strong>}ar'
  448. );
  449. } );
  450. it( 'starts in attribute, ends in text node', () => {
  451. test(
  452. [ 3, 5 ],
  453. 'fo<$text theme="important">ob</$text>ar',
  454. 'fo<strong style="text-transform:uppercase">o{b</strong>a}r'
  455. );
  456. } );
  457. } );
  458. describe( 'collapsed selection', () => {
  459. it( 'in attribute', () => {
  460. test(
  461. [ 3, 3 ],
  462. 'f<$text theme="gold">ooba</$text>r',
  463. 'f<span style="color:yellow">oo{}ba</span>r'
  464. );
  465. } );
  466. it( 'in container with theme attribute', () => {
  467. test(
  468. [ 1, 1 ],
  469. 'foobar',
  470. 'f<strong style="text-transform:uppercase">[]</strong>oobar',
  471. { theme: 'important' }
  472. );
  473. } );
  474. it( 'in theme attribute with extra attributes #1', () => {
  475. test(
  476. [ 3, 3 ],
  477. 'f<$text theme="gold">ooba</$text>r',
  478. 'f<span style="color:yellow">oo</span>' +
  479. '<em><span style="color:yellow">[]</span></em>' +
  480. '<span style="color:yellow">ba</span>r',
  481. { italic: true }
  482. );
  483. } );
  484. it( 'in theme attribute with extra attributes #2', () => {
  485. // In contrary to test above, we don't have strong + span on the selection.
  486. // This is because strong and span are both created by the same attribute.
  487. // Since style="important" overwrites style="gold" on selection, we have only strong element.
  488. // In example above, selection has both style and italic attribute.
  489. test(
  490. [ 3, 3 ],
  491. 'f<$text theme="gold">ooba</$text>r',
  492. 'f<span style="color:yellow">oo</span>' +
  493. '<strong style="text-transform:uppercase">[]</strong>' +
  494. '<span style="color:yellow">ba</span>r',
  495. { theme: 'important' }
  496. );
  497. } );
  498. it( 'convertSelectionAttribute should do nothing if creator return null', () => {
  499. dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( () => {
  500. } ) );
  501. test(
  502. [ 3, 3 ],
  503. 'foobar',
  504. 'foo{}bar',
  505. { bold: 'true' }
  506. );
  507. } );
  508. } );
  509. } );
  510. describe( 'table cell selection converter', () => {
  511. beforeEach( () => {
  512. model.schema.register( 'table' );
  513. model.schema.register( 'tr' );
  514. model.schema.register( 'td' );
  515. model.schema.extend( 'table', { allowIn: '$root' } );
  516. model.schema.extend( 'tr', { allowIn: 'table' } );
  517. model.schema.extend( 'td', { allowIn: 'tr' } );
  518. model.schema.extend( '$text', { allowIn: 'td' } );
  519. // "Universal" converter to convert table structure.
  520. const tableConverter = insertElement( data => new ViewContainerElement( data.item.name ) );
  521. dispatcher.on( 'insert:table', tableConverter );
  522. dispatcher.on( 'insert:tr', tableConverter );
  523. dispatcher.on( 'insert:td', tableConverter );
  524. // Special converter for table cells.
  525. dispatcher.on( 'selection', ( evt, data, consumable, conversionApi ) => {
  526. const selection = data.selection;
  527. if ( !consumable.test( selection, 'selection' ) || selection.isCollapsed ) {
  528. return;
  529. }
  530. for ( const range of selection.getRanges() ) {
  531. const node = range.start.nodeAfter;
  532. if ( node == range.end.nodeBefore && node instanceof ModelElement && node.name == 'td' ) {
  533. consumable.consume( selection, 'selection' );
  534. const viewNode = conversionApi.mapper.toViewElement( node );
  535. viewNode.addClass( 'selected' );
  536. }
  537. }
  538. } );
  539. } );
  540. it( 'should not be used to convert selection that is not on table cell', () => {
  541. test(
  542. [ 1, 5 ],
  543. 'f{o<$text bold="true">ob</$text>a}r',
  544. 'f{o<strong>ob</strong>a}r'
  545. );
  546. } );
  547. it( 'should add a class to the selected table cell', () => {
  548. test(
  549. // table tr#0 |td#0, table tr#0 td#0|
  550. [ [ 0, 0, 0 ], [ 0, 0, 1 ] ],
  551. '<table><tr><td>foo</td></tr><tr><td>bar</td></tr></table>',
  552. '<table><tr><td class="selected">foo</td></tr><tr><td>bar</td></tr></table>'
  553. );
  554. } );
  555. it( 'should not be used if selection contains more than just a table cell', () => {
  556. test(
  557. // table tr td#1, table tr#2
  558. [ [ 0, 0, 0, 1 ], [ 0, 0, 2 ] ],
  559. '<table><tr><td>foo</td><td>bar</td></tr></table>',
  560. '<table><tr><td>f{oo</td><td>bar</td>]</tr></table>'
  561. );
  562. } );
  563. } );
  564. // Tests if the selection got correctly converted.
  565. // Because `setData` might use selection converters itself to set the selection, we can't use it
  566. // to set the selection (because then we would test converters using converters).
  567. // Instead, the `test` function expects to be passed `selectionPaths` which is an array containing two numbers or two arrays,
  568. // that are offsets or paths of selection positions in root element.
  569. function test( selectionPaths, modelInput, expectedView, selectionAttributes = {} ) {
  570. // Parse passed `modelInput` string and set it as current model.
  571. setModelData( model, modelInput );
  572. // Manually set selection ranges using passed `selectionPaths`.
  573. const startPath = typeof selectionPaths[ 0 ] == 'number' ? [ selectionPaths[ 0 ] ] : selectionPaths[ 0 ];
  574. const endPath = typeof selectionPaths[ 1 ] == 'number' ? [ selectionPaths[ 1 ] ] : selectionPaths[ 1 ];
  575. const startPos = new ModelPosition( modelRoot, startPath );
  576. const endPos = new ModelPosition( modelRoot, endPath );
  577. const isBackward = selectionPaths[ 2 ] === 'backward';
  578. modelSelection.setRanges( [ new ModelRange( startPos, endPos ) ], isBackward );
  579. // Update selection attributes according to model.
  580. modelSelection.refreshAttributes();
  581. // And add or remove passed attributes.
  582. for ( const key in selectionAttributes ) {
  583. const value = selectionAttributes[ key ];
  584. if ( value ) {
  585. modelSelection.setAttribute( key, value );
  586. } else {
  587. modelSelection.removeAttribute( key );
  588. }
  589. }
  590. // Remove view children manually (without firing additional conversion).
  591. viewRoot.removeChildren( 0, viewRoot.childCount );
  592. // Convert model to view.
  593. dispatcher.convertInsert( ModelRange.createIn( modelRoot ) );
  594. dispatcher.convertSelection( modelSelection, [] );
  595. // Stringify view and check if it is same as expected.
  596. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) ).to.equal( '<div>' + expectedView + '</div>' );
  597. }
  598. } );