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

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