8
0

restrictededitingediting.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document */
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  8. import RestrictedEditingEditing from './../src/restrictededitingediting';
  9. import RestrictedEditingNavigationCommand from '../src/restrictededitingnavigationcommand';
  10. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  12. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  13. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  14. import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
  15. describe( 'RestrictedEditingEditing', () => {
  16. let editor, element;
  17. testUtils.createSinonSandbox();
  18. describe( 'plugin', () => {
  19. beforeEach( async () => {
  20. element = document.createElement( 'div' );
  21. document.body.appendChild( element );
  22. editor = await ClassicTestEditor.create( element, { plugins: [ RestrictedEditingEditing ] } );
  23. } );
  24. afterEach( () => {
  25. element.remove();
  26. return editor.destroy();
  27. } );
  28. it( 'should be named', () => {
  29. expect( RestrictedEditingEditing.pluginName ).to.equal( 'RestrictedEditingEditing' );
  30. } );
  31. it( 'should be loaded', () => {
  32. expect( editor.plugins.get( RestrictedEditingEditing ) ).to.be.instanceOf( RestrictedEditingEditing );
  33. } );
  34. it( 'adds a "goToPreviousRestrictedEditingRegion" command', () => {
  35. expect( editor.commands.get( 'goToPreviousRestrictedEditingRegion' ) ).to.be.instanceOf( RestrictedEditingNavigationCommand );
  36. } );
  37. it( 'adds a "goToNextRestrictedEditingRegion" command', () => {
  38. expect( editor.commands.get( 'goToNextRestrictedEditingRegion' ) ).to.be.instanceOf( RestrictedEditingNavigationCommand );
  39. } );
  40. } );
  41. describe( 'conversion', () => {
  42. let model;
  43. beforeEach( async () => {
  44. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, RestrictedEditingEditing ] } );
  45. model = editor.model;
  46. } );
  47. afterEach( () => {
  48. return editor.destroy();
  49. } );
  50. describe( 'upcast', () => {
  51. it( 'should convert <span class="ck-restricted-editing-exception"> to marker', () => {
  52. editor.setData( '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' );
  53. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.true;
  54. const marker = model.markers.get( 'restricted-editing-exception:1' );
  55. expect( marker.getStart().path ).to.deep.equal( [ 0, 4 ] );
  56. expect( marker.getEnd().path ).to.deep.equal( [ 0, 7 ] );
  57. } );
  58. it( 'should convert multiple <span class="ck-restricted-editing-exception">', () => {
  59. editor.setData(
  60. '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  61. '<p>ABCDEF<span class="ck-restricted-editing-exception">GHIJK</span>LMNOPQRST</p>'
  62. );
  63. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.true;
  64. expect( model.markers.has( 'restricted-editing-exception:2' ) ).to.be.true;
  65. // Data for the first marker is the same as in previous tests so no need to test it again.
  66. const secondMarker = model.markers.get( 'restricted-editing-exception:2' );
  67. expect( secondMarker.getStart().path ).to.deep.equal( [ 1, 6 ] );
  68. expect( secondMarker.getEnd().path ).to.deep.equal( [ 1, 11 ] );
  69. } );
  70. it( 'should not convert other <span> elements', () => {
  71. editor.setData( '<p>foo <span class="foo bar">bar</span> baz</p>' );
  72. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.false;
  73. } );
  74. } );
  75. describe( 'downcast', () => {
  76. it( 'should convert model marker to <span>', () => {
  77. setModelData( model, '<paragraph>foo bar baz</paragraph>' );
  78. const paragraph = model.document.getRoot().getChild( 0 );
  79. model.change( writer => {
  80. writer.addMarker( 'restricted-editing-exception:1', {
  81. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  82. usingOperation: true,
  83. affectsData: true
  84. } );
  85. } );
  86. const expectedView = '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>';
  87. expect( editor.getData() ).to.equal( expectedView );
  88. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  89. } );
  90. it( 'converted <span> should be the outermost attribute element', () => {
  91. editor.conversion.for( 'downcast' ).attributeToElement( { model: 'bold', view: 'b' } );
  92. setModelData( model, '<paragraph><$text bold="true">foo bar baz</$text></paragraph>' );
  93. const paragraph = model.document.getRoot().getChild( 0 );
  94. model.change( writer => {
  95. writer.addMarker( 'restricted-editing-exception:1', {
  96. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 'end' ) ),
  97. usingOperation: true,
  98. affectsData: true
  99. } );
  100. } );
  101. const expectedView = '<p><span class="ck-restricted-editing-exception"><b>foo bar baz</b></span></p>';
  102. expect( editor.getData() ).to.equal( expectedView );
  103. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  104. } );
  105. } );
  106. } );
  107. describe( 'editing behavior', () => {
  108. let model;
  109. beforeEach( async () => {
  110. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, RestrictedEditingEditing ] } );
  111. model = editor.model;
  112. } );
  113. afterEach( () => {
  114. return editor.destroy();
  115. } );
  116. it( 'should keep markers in the view when editable region is edited', () => {
  117. setModelData( model,
  118. '<paragraph>foo bar baz</paragraph>' +
  119. '<paragraph>xxx y[]yy zzz</paragraph>'
  120. );
  121. const firstParagraph = model.document.getRoot().getChild( 0 );
  122. const secondParagraph = model.document.getRoot().getChild( 1 );
  123. model.change( writer => {
  124. writer.addMarker( 'restricted-editing-exception:1', {
  125. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  126. usingOperation: true,
  127. affectsData: true
  128. } );
  129. writer.addMarker( 'restricted-editing-exception:2', {
  130. range: writer.createRange(
  131. writer.createPositionAt( secondParagraph, 4 ),
  132. writer.createPositionAt( secondParagraph, 7 )
  133. ),
  134. usingOperation: true,
  135. affectsData: true
  136. } );
  137. } );
  138. model.change( writer => {
  139. model.insertContent( writer.createText( 'R', model.document.selection.getAttributes() ) );
  140. } );
  141. expect( editor.getData() ).to.equal(
  142. '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  143. '<p>xxx <span class="ck-restricted-editing-exception">yRyy</span> zzz</p>' );
  144. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  145. '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  146. '<p>xxx <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">yRyy</span> zzz</p>' );
  147. } );
  148. } );
  149. describe( 'exception highlighting', () => {
  150. let model, view;
  151. beforeEach( async () => {
  152. editor = await VirtualTestEditor.create( {
  153. plugins: [ Paragraph, RestrictedEditingEditing, BoldEditing ]
  154. } );
  155. model = editor.model;
  156. view = editor.editing.view;
  157. } );
  158. afterEach( () => {
  159. return editor.destroy();
  160. } );
  161. it( 'should convert the highlight to a proper view classes', () => {
  162. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  163. const paragraph = model.document.getRoot().getChild( 0 );
  164. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  165. model.change( writer => {
  166. writer.addMarker( 'restricted-editing-exception:1', {
  167. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  168. usingOperation: true,
  169. affectsData: true
  170. } );
  171. } );
  172. expect( getViewData( view ) ).to.equal(
  173. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{a}r</span> baz</p>'
  174. );
  175. } );
  176. it( 'should remove classes when selection is moved away from an exception', () => {
  177. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  178. const paragraph = model.document.getRoot().getChild( 0 );
  179. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  180. model.change( writer => {
  181. writer.addMarker( 'restricted-editing-exception:1', {
  182. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  183. usingOperation: true,
  184. affectsData: true
  185. } );
  186. } );
  187. expect( getViewData( view ) ).to.equal(
  188. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{a}r</span> baz</p>'
  189. );
  190. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  191. expect( getViewData( view ) ).to.equal(
  192. '<p>{}foo <span class="ck-restricted-editing-exception">bar</span> baz</p>'
  193. );
  194. } );
  195. it( 'should work correctly when selection is moved inside an exception', () => {
  196. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  197. const paragraph = model.document.getRoot().getChild( 0 );
  198. // <paragraph>[]foo <$marker>bar</$marker> baz</paragraph>
  199. model.change( writer => {
  200. writer.addMarker( 'restricted-editing-exception:1', {
  201. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  202. usingOperation: true,
  203. affectsData: true
  204. } );
  205. } );
  206. expect( getViewData( view ) ).to.equal(
  207. '<p>{}foo <span class="ck-restricted-editing-exception">bar</span> baz</p>'
  208. );
  209. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 6 ) );
  210. expect( getViewData( view ) ).to.equal(
  211. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">ba{}r</span> baz</p>'
  212. );
  213. } );
  214. describe( 'editing downcast conversion integration', () => {
  215. it( 'works for the #insert event', () => {
  216. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  217. const paragraph = model.document.getRoot().getChild( 0 );
  218. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  219. model.change( writer => {
  220. writer.addMarker( 'restricted-editing-exception:1', {
  221. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  222. usingOperation: true,
  223. affectsData: true
  224. } );
  225. } );
  226. model.change( writer => {
  227. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  228. } );
  229. expect( getViewData( view ) ).to.equal(
  230. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">bFOO{a}r</span> baz</p>'
  231. );
  232. } );
  233. it( 'works for the #remove event', () => {
  234. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  235. const paragraph = model.document.getRoot().getChild( 0 );
  236. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  237. model.change( writer => {
  238. writer.addMarker( 'restricted-editing-exception:1', {
  239. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  240. usingOperation: true,
  241. affectsData: true
  242. } );
  243. } );
  244. model.change( writer => {
  245. writer.remove( writer.createRange(
  246. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 ),
  247. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 6 )
  248. ) );
  249. } );
  250. expect( getViewData( view ) ).to.equal(
  251. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{}r</span> baz</p>'
  252. );
  253. } );
  254. it( 'works for the #attribute event', () => {
  255. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  256. const paragraph = model.document.getRoot().getChild( 0 );
  257. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  258. model.change( writer => {
  259. writer.addMarker( 'restricted-editing-exception:1', {
  260. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  261. usingOperation: true,
  262. affectsData: true
  263. } );
  264. } );
  265. model.change( writer => {
  266. writer.setAttribute( 'bold', true, writer.createRange(
  267. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  268. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  269. );
  270. } );
  271. expect( getViewData( view ) ).to.equal(
  272. '<p>foo ' +
  273. '<span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">' +
  274. '<strong>b{a</strong>' +
  275. '}r</span>' +
  276. ' baz</p>'
  277. );
  278. } );
  279. it( 'works for the #selection event', () => {
  280. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  281. const paragraph = model.document.getRoot().getChild( 0 );
  282. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  283. model.change( writer => {
  284. writer.addMarker( 'restricted-editing-exception:1', {
  285. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  286. usingOperation: true,
  287. affectsData: true
  288. } );
  289. } );
  290. model.change( writer => {
  291. writer.setSelection( writer.createRange(
  292. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  293. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  294. );
  295. } );
  296. expect( getViewData( view ) ).to.equal(
  297. '<p>foo {<span class="ck-restricted-editing-exception">ba}r</span> baz</p>'
  298. );
  299. } );
  300. it( 'works for the addMarker and removeMarker events', () => {
  301. editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
  302. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  303. const paragraph = model.document.getRoot().getChild( 0 );
  304. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  305. model.change( writer => {
  306. writer.addMarker( 'restricted-editing-exception:1', {
  307. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  308. usingOperation: true,
  309. affectsData: true
  310. } );
  311. } );
  312. model.change( writer => {
  313. const range = writer.createRange(
  314. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  315. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  316. );
  317. writer.addMarker( 'fooMarker', { range, usingOperation: true } );
  318. } );
  319. expect( getViewData( view ) ).to.equal(
  320. '<p>' +
  321. '<span>foo </span>' +
  322. '<span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">' +
  323. '<span>b</span>{a}r' +
  324. '</span>' +
  325. ' baz</p>'
  326. );
  327. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  328. expect( getViewData( view ) ).to.equal(
  329. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{a}r</span> baz</p>'
  330. );
  331. } );
  332. } );
  333. } );
  334. } );