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

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