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

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