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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: conversion */
  6. import ModelDocument from 'ckeditor5/engine/model/document.js';
  7. import ModelElement from 'ckeditor5/engine/model/element.js';
  8. import ModelRange from 'ckeditor5/engine/model/range.js';
  9. import ModelPosition from 'ckeditor5/engine/model/position.js';
  10. import ViewDocument from 'ckeditor5/engine/view/document.js';
  11. import ViewContainerElement from 'ckeditor5/engine/view/containerelement.js';
  12. import ViewAttributeElement from 'ckeditor5/engine/view/attributeelement.js';
  13. import { mergeAttributes } from 'ckeditor5/engine/view/writer.js';
  14. import Mapper from 'ckeditor5/engine/conversion/mapper.js';
  15. import ModelConversionDispatcher from 'ckeditor5/engine/conversion/modelconversiondispatcher.js';
  16. import {
  17. convertRangeSelection,
  18. convertCollapsedSelection,
  19. convertSelectionAttribute,
  20. clearAttributes,
  21. clearFakeSelection
  22. } from 'ckeditor5/engine/conversion/model-selection-to-view-converters.js';
  23. import {
  24. insertElement,
  25. insertText,
  26. wrap
  27. } from 'ckeditor5/engine/conversion/model-to-view-converters.js';
  28. import { stringify as stringifyView } from 'ckeditor5/engine/dev-utils/view.js';
  29. import { setData as setModelData } from 'ckeditor5/engine/dev-utils/model.js';
  30. let dispatcher, mapper;
  31. let modelDoc, modelRoot, modelSelection;
  32. let viewDoc, viewRoot, viewSelection;
  33. beforeEach( () => {
  34. modelDoc = new ModelDocument();
  35. modelRoot = modelDoc.createRoot();
  36. modelSelection = modelDoc.selection;
  37. modelDoc.schema.allow( { name: '$text', inside: '$root' } );
  38. viewDoc = new ViewDocument();
  39. viewRoot = viewDoc.createRoot( 'div' );
  40. viewSelection = viewDoc.selection;
  41. mapper = new Mapper();
  42. mapper.bindElements( modelRoot, viewRoot );
  43. dispatcher = new ModelConversionDispatcher( { mapper, viewSelection } );
  44. dispatcher.on( 'insert:$text', insertText() );
  45. dispatcher.on( 'addAttribute:bold', wrap( new ViewAttributeElement( 'strong' ) ) );
  46. // Default selection converters.
  47. dispatcher.on( 'selection', clearAttributes(), { priority: 'low' } );
  48. dispatcher.on( 'selection', convertRangeSelection(), { priority: 'low' } );
  49. dispatcher.on( 'selection', convertCollapsedSelection(), { priority: 'low' } );
  50. } );
  51. afterEach( () => {
  52. viewDoc.destroy();
  53. } );
  54. describe( 'default converters', () => {
  55. beforeEach( () => {
  56. // Selection converters for selection attributes.
  57. dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( new ViewAttributeElement( 'strong' ) ) );
  58. dispatcher.on( 'selectionAttribute:italic', convertSelectionAttribute( new ViewAttributeElement( 'em' ) ) );
  59. } );
  60. describe( 'range selection', () => {
  61. it( 'in same container', () => {
  62. test(
  63. [ 1, 4 ],
  64. 'foobar',
  65. 'f{oob}ar'
  66. );
  67. } );
  68. it( 'in same container with unicode characters', () => {
  69. test(
  70. [ 2, 6 ],
  71. 'நிலைக்கு',
  72. 'நி{லைக்}கு'
  73. );
  74. } );
  75. it( 'in same container, over attribute', () => {
  76. test(
  77. [ 1, 5 ],
  78. 'fo<$text bold="true">ob</$text>ar',
  79. 'f{o<strong>ob</strong>a}r'
  80. );
  81. } );
  82. it( 'in same container, next to attribute', () => {
  83. test(
  84. [ 1, 2 ],
  85. 'fo<$text bold="true">ob</$text>ar',
  86. 'f{o}<strong>ob</strong>ar'
  87. );
  88. } );
  89. it( 'in same attribute', () => {
  90. test(
  91. [ 2, 4 ],
  92. 'f<$text bold="true">ooba</$text>r',
  93. 'f<strong>o{ob}a</strong>r'
  94. );
  95. } );
  96. it( 'in same attribute, selection same as attribute', () => {
  97. test(
  98. [ 2, 4 ],
  99. 'fo<$text bold="true">ob</$text>ar',
  100. 'fo{<strong>ob</strong>}ar'
  101. );
  102. } );
  103. it( 'starts in text node, ends in attribute #1', () => {
  104. test(
  105. [ 1, 3 ],
  106. 'fo<$text bold="true">ob</$text>ar',
  107. 'f{o<strong>o}b</strong>ar'
  108. );
  109. } );
  110. it( 'starts in text node, ends in attribute #2', () => {
  111. test(
  112. [ 1, 4 ],
  113. 'fo<$text bold="true">ob</$text>ar',
  114. 'f{o<strong>ob</strong>}ar'
  115. );
  116. } );
  117. it( 'starts in attribute, ends in text node', () => {
  118. test(
  119. [ 3, 5 ],
  120. 'fo<$text bold="true">ob</$text>ar',
  121. 'fo<strong>o{b</strong>a}r'
  122. );
  123. } );
  124. it( 'consumes consumable values properly', () => {
  125. // Add callback that will fire before default ones.
  126. // This should prevent default callback doing anything.
  127. dispatcher.on( 'selection', ( evt, data, consumable ) => {
  128. expect( consumable.consume( data.selection, 'selection' ) ).to.be.true;
  129. }, { priority: 'high' } );
  130. // Similar test case as the first in this suite.
  131. test(
  132. [ 1, 4 ],
  133. 'foobar',
  134. 'foobar' // No selection in view.
  135. );
  136. } );
  137. it( 'should convert backward selection', () => {
  138. test(
  139. [ 1, 3, 'backward' ],
  140. 'foobar',
  141. 'f{oo}bar'
  142. );
  143. expect( viewSelection.focus.offset ).to.equal( 1 );
  144. } );
  145. } );
  146. describe( 'collapsed selection', () => {
  147. it( 'in container', () => {
  148. test(
  149. [ 1, 1 ],
  150. 'foobar',
  151. 'f{}oobar'
  152. );
  153. } );
  154. it( 'in attribute', () => {
  155. test(
  156. [ 3, 3 ],
  157. 'f<$text bold="true">ooba</$text>r',
  158. 'f<strong>oo{}ba</strong>r'
  159. );
  160. } );
  161. it( 'in container with extra attributes', () => {
  162. test(
  163. [ 1, 1 ],
  164. 'foobar',
  165. 'f<em>[]</em>oobar',
  166. { italic: true }
  167. );
  168. } );
  169. it( 'in attribute with extra attributes', () => {
  170. test(
  171. [ 3, 3 ],
  172. 'f<$text bold="true">ooba</$text>r',
  173. 'f<strong>oo</strong><em><strong>[]</strong></em><strong>ba</strong>r',
  174. { italic: true }
  175. );
  176. } );
  177. it( 'consumes consumable values properly', () => {
  178. // Add callbacks that will fire before default ones.
  179. // This should prevent default callbacks doing anything.
  180. dispatcher.on( 'selection', ( evt, data, consumable ) => {
  181. expect( consumable.consume( data.selection, 'selection' ) ).to.be.true;
  182. }, { priority: 'high' } );
  183. dispatcher.on( 'selectionAttribute:bold', ( evt, data, consumable ) => {
  184. expect( consumable.consume( data.selection, 'selectionAttribute:bold' ) ).to.be.true;
  185. }, { priority: 'high' } );
  186. // Similar test case as above
  187. test(
  188. [ 3, 3 ],
  189. 'f<$text bold="true">ooba</$text>r',
  190. 'f<strong>ooba</strong>r' // No selection in view.
  191. );
  192. } );
  193. } );
  194. } );
  195. describe( 'clean-up', () => {
  196. describe( 'convertRangeSelection', () => {
  197. it( 'should remove all ranges before adding new range', () => {
  198. test(
  199. [ 0, 2 ],
  200. 'foobar',
  201. '{fo}obar'
  202. );
  203. test(
  204. [ 3, 5 ],
  205. 'foobar',
  206. 'foo{ba}r'
  207. );
  208. expect( viewSelection.rangeCount ).to.equal( 1 );
  209. } );
  210. } );
  211. describe( 'convertCollapsedSelection', () => {
  212. it( 'should remove all ranges before adding new range', () => {
  213. test(
  214. [ 2, 2 ],
  215. 'foobar',
  216. 'fo{}obar'
  217. );
  218. test(
  219. [ 3, 3 ],
  220. 'foobar',
  221. 'foo{}bar'
  222. );
  223. expect( viewSelection.rangeCount ).to.equal( 1 );
  224. } );
  225. } );
  226. describe( 'clearAttributes', () => {
  227. it( 'should remove all ranges before adding new range', () => {
  228. dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( new ViewAttributeElement( 'b' ) ) );
  229. dispatcher.on( 'addAttribute:style', wrap( new ViewAttributeElement( 'b' ) ) );
  230. test(
  231. [ 3, 3 ],
  232. 'foobar',
  233. 'foo<b>[]</b>bar',
  234. { bold: 'true' }
  235. );
  236. const modelRange = ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 1 );
  237. modelDoc.selection.setRanges( [ modelRange ] );
  238. dispatcher.convertSelection( modelDoc.selection );
  239. expect( viewSelection.rangeCount ).to.equal( 1 );
  240. const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
  241. expect( viewString ).to.equal( '<div>f{}oobar</div>' );
  242. } );
  243. it( 'should do nothing if the attribute element had been already removed', () => {
  244. dispatcher.on( 'selectionAttribute:bold', convertSelectionAttribute( new ViewAttributeElement( 'b' ) ) );
  245. dispatcher.on( 'addAttribute:style', wrap( new ViewAttributeElement( 'b' ) ) );
  246. test(
  247. [ 3, 3 ],
  248. 'foobar',
  249. 'foo<b>[]</b>bar',
  250. { bold: 'true' }
  251. );
  252. // Remove <b></b> manually.
  253. mergeAttributes( viewSelection.getFirstPosition() );
  254. const modelRange = ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 1 );
  255. modelDoc.selection.setRanges( [ modelRange ] );
  256. dispatcher.convertSelection( modelDoc.selection );
  257. expect( viewSelection.rangeCount ).to.equal( 1 );
  258. const viewString = stringifyView( viewRoot, viewSelection, { showType: false } );
  259. expect( viewString ).to.equal( '<div>f{}oobar</div>' );
  260. } );
  261. } );
  262. describe( 'clearFakeSelection', () => {
  263. it( 'should clear fake selection', () => {
  264. dispatcher.on( 'selection', clearFakeSelection() );
  265. viewSelection.setFake( true );
  266. dispatcher.convertSelection( modelSelection );
  267. expect( viewSelection.isFake ).to.be.false;
  268. } );
  269. } );
  270. } );
  271. describe( 'using element creator for attributes conversion', () => {
  272. beforeEach( () => {
  273. function themeElementCreator( themeValue ) {
  274. if ( themeValue == 'important' ) {
  275. return new ViewAttributeElement( 'strong', { style: 'text-transform:uppercase;' } );
  276. } else if ( themeValue == 'gold' ) {
  277. return new ViewAttributeElement( 'span', { style: 'color:yellow;' } );
  278. }
  279. }
  280. dispatcher.on( 'selectionAttribute:theme', convertSelectionAttribute( themeElementCreator ) );
  281. dispatcher.on( 'addAttribute:theme', wrap( themeElementCreator ) );
  282. dispatcher.on( 'selectionAttribute:italic', convertSelectionAttribute( new ViewAttributeElement( 'em' ) ) );
  283. } );
  284. describe( 'range selection', () => {
  285. it( 'in same container, over attribute', () => {
  286. test(
  287. [ 1, 5 ],
  288. 'fo<$text theme="gold">ob</$text>ar',
  289. 'f{o<span style="color:yellow;">ob</span>a}r'
  290. );
  291. } );
  292. it( 'in same attribute', () => {
  293. test(
  294. [ 2, 4 ],
  295. 'f<$text theme="gold">ooba</$text>r',
  296. 'f<span style="color:yellow;">o{ob}a</span>r'
  297. );
  298. } );
  299. it( 'in same attribute, selection same as attribute', () => {
  300. test(
  301. [ 2, 4 ],
  302. 'fo<$text theme="important">ob</$text>ar',
  303. 'fo{<strong style="text-transform:uppercase;">ob</strong>}ar'
  304. );
  305. } );
  306. it( 'starts in attribute, ends in text node', () => {
  307. test(
  308. [ 3, 5 ],
  309. 'fo<$text theme="important">ob</$text>ar',
  310. 'fo<strong style="text-transform:uppercase;">o{b</strong>a}r'
  311. );
  312. } );
  313. } );
  314. describe( 'collapsed selection', () => {
  315. it( 'in attribute', () => {
  316. test(
  317. [ 3, 3 ],
  318. 'f<$text theme="gold">ooba</$text>r',
  319. 'f<span style="color:yellow;">oo{}ba</span>r'
  320. );
  321. } );
  322. it( 'in container with theme attribute', () => {
  323. test(
  324. [ 1, 1 ],
  325. 'foobar',
  326. 'f<strong style="text-transform:uppercase;">[]</strong>oobar',
  327. { theme: 'important' }
  328. );
  329. } );
  330. it( 'in theme attribute with extra attributes #1', () => {
  331. test(
  332. [ 3, 3 ],
  333. 'f<$text theme="gold">ooba</$text>r',
  334. 'f<span style="color:yellow;">oo</span>' +
  335. '<em><span style="color:yellow;">[]</span></em>' +
  336. '<span style="color:yellow;">ba</span>r',
  337. { italic: true }
  338. );
  339. } );
  340. it( 'in theme attribute with extra attributes #2', () => {
  341. // In contrary to test above, we don't have strong + span on the selection.
  342. // This is because strong and span are both created by the same attribute.
  343. // Since style="important" overwrites style="gold" on selection, we have only strong element.
  344. // In example above, selection has both style and italic attribute.
  345. test(
  346. [ 3, 3 ],
  347. 'f<$text theme="gold">ooba</$text>r',
  348. 'f<span style="color:yellow;">oo</span>' +
  349. '<strong style="text-transform:uppercase;">[]</strong>' +
  350. '<span style="color:yellow;">ba</span>r',
  351. { theme: 'important' }
  352. );
  353. } );
  354. } );
  355. } );
  356. describe( 'table cell selection converter', () => {
  357. beforeEach( () => {
  358. modelDoc.schema.registerItem( 'table', '$block' );
  359. modelDoc.schema.registerItem( 'tr', '$block' );
  360. modelDoc.schema.registerItem( 'td', '$block' );
  361. // "Universal" converter to convert table structure.
  362. const tableConverter = insertElement( ( data ) => new ViewContainerElement( data.item.name ) );
  363. dispatcher.on( 'insert:table', tableConverter );
  364. dispatcher.on( 'insert:tr', tableConverter );
  365. dispatcher.on( 'insert:td', tableConverter );
  366. // Special converter for table cells.
  367. dispatcher.on( 'selection', ( evt, data, consumable, conversionApi ) => {
  368. const selection = data.selection;
  369. if ( !consumable.test( selection, 'selection' ) || selection.isCollapsed ) {
  370. return;
  371. }
  372. for ( let range of selection.getRanges() ) {
  373. const node = range.start.nodeAfter;
  374. if ( node == range.end.nodeBefore && node instanceof ModelElement && node.name == 'td' ) {
  375. consumable.consume( selection, 'selection' );
  376. let viewNode = conversionApi.mapper.toViewElement( node );
  377. viewNode.addClass( 'selected' );
  378. }
  379. }
  380. } );
  381. } );
  382. it( 'should not be used to convert selection that is not on table cell', () => {
  383. test(
  384. [ 1, 5 ],
  385. 'f{o<$text bold="true">ob</$text>a}r',
  386. 'f{o<strong>ob</strong>a}r'
  387. );
  388. } );
  389. it( 'should add a class to the selected table cell', () => {
  390. test(
  391. // table tr#0 |td#0, table tr#0 td#0|
  392. [ [ 0, 0, 0 ], [ 0, 0, 1 ] ],
  393. '<table><tr><td>foo</td></tr><tr><td>bar</td></tr></table>',
  394. '<table><tr><td class="selected">foo</td></tr><tr><td>bar</td></tr></table>'
  395. );
  396. } );
  397. it( 'should not be used if selection contains more than just a table cell', () => {
  398. test(
  399. // table tr td#1, table tr#2
  400. [ [ 0, 0, 0, 1 ], [ 0, 0, 2 ] ],
  401. '<table><tr><td>foo</td><td>bar</td></tr></table>',
  402. '<table><tr><td>f{oo</td><td>bar</td>]</tr></table>'
  403. );
  404. } );
  405. } );
  406. // Tests if the selection got correctly converted.
  407. // Because `setData` might use selection converters itself to set the selection, we can't use it
  408. // to set the selection (because then we would test converters using converters).
  409. // Instead, the `test` function expects to be passed `selectionPaths` which is an array containing two numbers or two arrays,
  410. // that are offsets or paths of selection positions in root element.
  411. function test( selectionPaths, modelInput, expectedView, selectionAttributes = {} ) {
  412. // Parse passed `modelInput` string and set it as current model.
  413. setModelData( modelDoc, modelInput );
  414. // Manually set selection ranges using passed `selectionPaths`.
  415. const startPath = typeof selectionPaths[ 0 ] == 'number' ? [ selectionPaths[ 0 ] ] : selectionPaths[ 0 ];
  416. const endPath = typeof selectionPaths[ 1 ] == 'number' ? [ selectionPaths[ 1 ] ] : selectionPaths[ 1 ];
  417. const startPos = new ModelPosition( modelRoot, startPath );
  418. const endPos = new ModelPosition( modelRoot, endPath );
  419. const isBackward = selectionPaths[ 2 ] === 'backward';
  420. modelSelection.setRanges( [ new ModelRange( startPos, endPos ) ], isBackward );
  421. // Updated selection attributes according to model.
  422. modelSelection._updateAttributes();
  423. // And add or remove passed attributes.
  424. for ( let key in selectionAttributes ) {
  425. let value = selectionAttributes[ key ];
  426. if ( value ) {
  427. modelSelection.setAttribute( key, value );
  428. } else {
  429. modelSelection.removeAttribute( key );
  430. }
  431. }
  432. // Remove view children since we do not want to convert deletion.
  433. viewRoot.removeChildren( 0, viewRoot.childCount );
  434. // Convert model to view.
  435. dispatcher.convertInsertion( ModelRange.createIn( modelRoot ) );
  436. dispatcher.convertSelection( modelSelection );
  437. // Stringify view and check if it is same as expected.
  438. expect( stringifyView( viewRoot, viewSelection, { showType: false } ) ).to.equal( '<div>' + expectedView + '</div>' );
  439. }