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

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