restrictededitingediting.js 15 KB

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