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

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