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

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