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

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