8
0

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

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