8
0

restrictededitingediting.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  13. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  14. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  15. import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
  16. describe( 'RestrictedEditingEditing', () => {
  17. let editor, element;
  18. testUtils.createSinonSandbox();
  19. describe( 'plugin', () => {
  20. beforeEach( async () => {
  21. element = document.createElement( 'div' );
  22. document.body.appendChild( element );
  23. editor = await ClassicTestEditor.create( element, { plugins: [ RestrictedEditingEditing ] } );
  24. } );
  25. afterEach( () => {
  26. element.remove();
  27. return editor.destroy();
  28. } );
  29. it( 'should be named', () => {
  30. expect( RestrictedEditingEditing.pluginName ).to.equal( 'RestrictedEditingEditing' );
  31. } );
  32. it( 'should be loaded', () => {
  33. expect( editor.plugins.get( RestrictedEditingEditing ) ).to.be.instanceOf( RestrictedEditingEditing );
  34. } );
  35. it( 'adds a "goToPreviousRestrictedEditingRegion" command', () => {
  36. expect( editor.commands.get( 'goToPreviousRestrictedEditingRegion' ) ).to.be.instanceOf( RestrictedEditingNavigationCommand );
  37. } );
  38. it( 'adds a "goToNextRestrictedEditingRegion" command', () => {
  39. expect( editor.commands.get( 'goToNextRestrictedEditingRegion' ) ).to.be.instanceOf( RestrictedEditingNavigationCommand );
  40. } );
  41. } );
  42. describe( 'conversion', () => {
  43. let model;
  44. beforeEach( async () => {
  45. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, RestrictedEditingEditing ] } );
  46. model = editor.model;
  47. } );
  48. afterEach( () => {
  49. return editor.destroy();
  50. } );
  51. describe( 'upcast', () => {
  52. it( 'should convert <span class="ck-restricted-editing-exception"> to marker', () => {
  53. editor.setData( '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' );
  54. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.true;
  55. const marker = model.markers.get( 'restricted-editing-exception:1' );
  56. expect( marker.getStart().path ).to.deep.equal( [ 0, 4 ] );
  57. expect( marker.getEnd().path ).to.deep.equal( [ 0, 7 ] );
  58. } );
  59. it( 'should convert multiple <span class="ck-restricted-editing-exception">', () => {
  60. editor.setData(
  61. '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  62. '<p>ABCDEF<span class="ck-restricted-editing-exception">GHIJK</span>LMNOPQRST</p>'
  63. );
  64. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.true;
  65. expect( model.markers.has( 'restricted-editing-exception:2' ) ).to.be.true;
  66. // Data for the first marker is the same as in previous tests so no need to test it again.
  67. const secondMarker = model.markers.get( 'restricted-editing-exception:2' );
  68. expect( secondMarker.getStart().path ).to.deep.equal( [ 1, 6 ] );
  69. expect( secondMarker.getEnd().path ).to.deep.equal( [ 1, 11 ] );
  70. } );
  71. it( 'should not convert other <span> elements', () => {
  72. editor.setData( '<p>foo <span class="foo bar">bar</span> baz</p>' );
  73. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.false;
  74. } );
  75. } );
  76. describe( 'downcast', () => {
  77. it( 'should convert model marker to <span>', () => {
  78. setModelData( model, '<paragraph>foo bar baz</paragraph>' );
  79. const paragraph = model.document.getRoot().getChild( 0 );
  80. model.change( writer => {
  81. writer.addMarker( 'restricted-editing-exception:1', {
  82. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  83. usingOperation: true,
  84. affectsData: true
  85. } );
  86. } );
  87. const expectedView = '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>';
  88. expect( editor.getData() ).to.equal( expectedView );
  89. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  90. } );
  91. it( 'converted <span> should be the outermost attribute element', () => {
  92. editor.conversion.for( 'downcast' ).attributeToElement( { model: 'bold', view: 'b' } );
  93. setModelData( model, '<paragraph><$text bold="true">foo bar baz</$text></paragraph>' );
  94. const paragraph = model.document.getRoot().getChild( 0 );
  95. model.change( writer => {
  96. writer.addMarker( 'restricted-editing-exception:1', {
  97. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 'end' ) ),
  98. usingOperation: true,
  99. affectsData: true
  100. } );
  101. } );
  102. const expectedView = '<p><span class="ck-restricted-editing-exception"><b>foo bar baz</b></span></p>';
  103. expect( editor.getData() ).to.equal( expectedView );
  104. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  105. } );
  106. } );
  107. } );
  108. describe( 'editing behavior', () => {
  109. let model;
  110. beforeEach( async () => {
  111. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, RestrictedEditingEditing ] } );
  112. model = editor.model;
  113. } );
  114. afterEach( () => {
  115. return editor.destroy();
  116. } );
  117. it( 'should keep markers in the view when editable region is edited', () => {
  118. setModelData( model,
  119. '<paragraph>foo bar baz</paragraph>' +
  120. '<paragraph>xxx y[]yy zzz</paragraph>'
  121. );
  122. const firstParagraph = model.document.getRoot().getChild( 0 );
  123. const secondParagraph = model.document.getRoot().getChild( 1 );
  124. model.change( writer => {
  125. writer.addMarker( 'restricted-editing-exception:1', {
  126. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  127. usingOperation: true,
  128. affectsData: true
  129. } );
  130. writer.addMarker( 'restricted-editing-exception:2', {
  131. range: writer.createRange(
  132. writer.createPositionAt( secondParagraph, 4 ),
  133. writer.createPositionAt( secondParagraph, 7 )
  134. ),
  135. usingOperation: true,
  136. affectsData: true
  137. } );
  138. } );
  139. model.change( writer => {
  140. model.insertContent( writer.createText( 'R', model.document.selection.getAttributes() ) );
  141. } );
  142. expect( editor.getData() ).to.equal(
  143. '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  144. '<p>xxx <span class="ck-restricted-editing-exception">yRyy</span> zzz</p>' );
  145. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  146. '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  147. '<p>xxx <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">yRyy</span> zzz</p>' );
  148. } );
  149. } );
  150. describe( 'exception highlighting', () => {
  151. let model, view;
  152. beforeEach( async () => {
  153. editor = await VirtualTestEditor.create( {
  154. plugins: [ Paragraph, RestrictedEditingEditing, BoldEditing ]
  155. } );
  156. model = editor.model;
  157. view = editor.editing.view;
  158. } );
  159. afterEach( () => {
  160. return editor.destroy();
  161. } );
  162. it( 'should convert the highlight to a proper view classes', () => {
  163. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  164. const paragraph = model.document.getRoot().getChild( 0 );
  165. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  166. model.change( writer => {
  167. writer.addMarker( 'restricted-editing-exception:1', {
  168. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  169. usingOperation: true,
  170. affectsData: true
  171. } );
  172. } );
  173. expect( getViewData( view ) ).to.equal(
  174. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{a}r</span> baz</p>'
  175. );
  176. } );
  177. it( 'should remove classes when selection is moved away from an exception', () => {
  178. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  179. const paragraph = model.document.getRoot().getChild( 0 );
  180. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  181. model.change( writer => {
  182. writer.addMarker( 'restricted-editing-exception:1', {
  183. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  184. usingOperation: true,
  185. affectsData: true
  186. } );
  187. } );
  188. expect( getViewData( view ) ).to.equal(
  189. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{a}r</span> baz</p>'
  190. );
  191. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  192. expect( getViewData( view ) ).to.equal(
  193. '<p>{}foo <span class="ck-restricted-editing-exception">bar</span> baz</p>'
  194. );
  195. } );
  196. it( 'should work correctly when selection is moved inside an exception', () => {
  197. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  198. const paragraph = model.document.getRoot().getChild( 0 );
  199. // <paragraph>[]foo <$marker>bar</$marker> baz</paragraph>
  200. model.change( writer => {
  201. writer.addMarker( 'restricted-editing-exception:1', {
  202. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  203. usingOperation: true,
  204. affectsData: true
  205. } );
  206. } );
  207. expect( getViewData( view ) ).to.equal(
  208. '<p>{}foo <span class="ck-restricted-editing-exception">bar</span> baz</p>'
  209. );
  210. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 6 ) );
  211. expect( getViewData( view ) ).to.equal(
  212. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">ba{}r</span> baz</p>'
  213. );
  214. } );
  215. describe( 'editing downcast conversion integration', () => {
  216. it( 'works for the #insert event', () => {
  217. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  218. const paragraph = model.document.getRoot().getChild( 0 );
  219. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  220. model.change( writer => {
  221. writer.addMarker( 'restricted-editing-exception:1', {
  222. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  223. usingOperation: true,
  224. affectsData: true
  225. } );
  226. } );
  227. model.change( writer => {
  228. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  229. } );
  230. expect( getViewData( view ) ).to.equal(
  231. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">bFOO{a}r</span> baz</p>'
  232. );
  233. } );
  234. it( 'works for the #remove event', () => {
  235. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  236. const paragraph = model.document.getRoot().getChild( 0 );
  237. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  238. model.change( writer => {
  239. writer.addMarker( 'restricted-editing-exception:1', {
  240. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  241. usingOperation: true,
  242. affectsData: true
  243. } );
  244. } );
  245. model.change( writer => {
  246. writer.remove( writer.createRange(
  247. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 ),
  248. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 6 )
  249. ) );
  250. } );
  251. expect( getViewData( view ) ).to.equal(
  252. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{}r</span> baz</p>'
  253. );
  254. } );
  255. it( 'works for the #attribute event', () => {
  256. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  257. const paragraph = model.document.getRoot().getChild( 0 );
  258. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  259. model.change( writer => {
  260. writer.addMarker( 'restricted-editing-exception:1', {
  261. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  262. usingOperation: true,
  263. affectsData: true
  264. } );
  265. } );
  266. model.change( writer => {
  267. writer.setAttribute( 'bold', true, writer.createRange(
  268. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  269. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  270. );
  271. } );
  272. expect( getViewData( view ) ).to.equal(
  273. '<p>foo ' +
  274. '<span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">' +
  275. '<strong>b{a</strong>' +
  276. '}r</span>' +
  277. ' baz</p>'
  278. );
  279. } );
  280. it( 'works for the #selection event', () => {
  281. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  282. const paragraph = model.document.getRoot().getChild( 0 );
  283. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  284. model.change( writer => {
  285. writer.addMarker( 'restricted-editing-exception:1', {
  286. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  287. usingOperation: true,
  288. affectsData: true
  289. } );
  290. } );
  291. model.change( writer => {
  292. writer.setSelection( writer.createRange(
  293. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  294. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  295. );
  296. } );
  297. expect( getViewData( view ) ).to.equal(
  298. '<p>foo {<span class="ck-restricted-editing-exception">ba}r</span> baz</p>'
  299. );
  300. } );
  301. it( 'works for the addMarker and removeMarker events', () => {
  302. editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
  303. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  304. const paragraph = model.document.getRoot().getChild( 0 );
  305. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  306. model.change( writer => {
  307. writer.addMarker( 'restricted-editing-exception:1', {
  308. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  309. usingOperation: true,
  310. affectsData: true
  311. } );
  312. } );
  313. model.change( writer => {
  314. const range = writer.createRange(
  315. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  316. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  317. );
  318. writer.addMarker( 'fooMarker', { range, usingOperation: true } );
  319. } );
  320. expect( getViewData( view ) ).to.equal(
  321. '<p>' +
  322. '<span>foo </span>' +
  323. '<span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">' +
  324. '<span>b</span>{a}r' +
  325. '</span>' +
  326. ' baz</p>'
  327. );
  328. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  329. expect( getViewData( view ) ).to.equal(
  330. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{a}r</span> baz</p>'
  331. );
  332. } );
  333. } );
  334. } );
  335. describe( 'exception cycling with the keyboard', () => {
  336. let model, view, domEvtDataStub;
  337. beforeEach( async () => {
  338. editor = await VirtualTestEditor.create( {
  339. plugins: [ Paragraph, RestrictedEditingEditing, BoldEditing ]
  340. } );
  341. model = editor.model;
  342. view = editor.editing.view;
  343. domEvtDataStub = {
  344. keyCode: getCode( 'Tab' ),
  345. preventDefault: sinon.spy(),
  346. stopPropagation: sinon.spy()
  347. };
  348. sinon.spy( editor, 'execute' );
  349. } );
  350. afterEach( () => {
  351. return editor.destroy();
  352. } );
  353. it( 'should move to the closest next exception on tab key', () => {
  354. setModelData( model, '<paragraph>[]foo bar baz qux</paragraph>' );
  355. const paragraph = model.document.getRoot().getChild( 0 );
  356. // <paragraph>[]foo <marker≥bar</marker> baz qux</paragraph>
  357. model.change( writer => {
  358. writer.addMarker( 'restricted-editing-exception:1', {
  359. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  360. usingOperation: true,
  361. affectsData: true
  362. } );
  363. } );
  364. // <paragraph>[]foo <marker≥bar</marker> <marker≥baz</marker≥ qux</paragraph>
  365. model.change( writer => {
  366. writer.addMarker( 'restricted-editing-exception:2', {
  367. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  368. usingOperation: true,
  369. affectsData: true
  370. } );
  371. } );
  372. view.document.fire( 'keydown', domEvtDataStub );
  373. sinon.assert.calledOnce( editor.execute );
  374. sinon.assert.calledWithExactly( editor.execute, 'goToNextRestrictedEditingRegion' );
  375. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  376. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  377. } );
  378. it( 'should not move to the closest next exception on tab key when there is none', () => {
  379. setModelData( model, '<paragraph>foo qux[]</paragraph>' );
  380. const paragraph = model.document.getRoot().getChild( 0 );
  381. // <paragraph><marker≥foo</marker> qux[]</paragraph>
  382. model.change( writer => {
  383. writer.addMarker( 'restricted-editing-exception:1', {
  384. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 3 ) ),
  385. usingOperation: true,
  386. affectsData: true
  387. } );
  388. } );
  389. view.document.fire( 'keydown', domEvtDataStub );
  390. sinon.assert.notCalled( editor.execute );
  391. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  392. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  393. } );
  394. it( 'should move to the closest previous exception on shift+tab key', () => {
  395. setModelData( model, '<paragraph>foo bar baz qux[]</paragraph>' );
  396. const paragraph = model.document.getRoot().getChild( 0 );
  397. // <paragraph>foo <marker≥bar</marker> baz qux[]</paragraph>
  398. model.change( writer => {
  399. writer.addMarker( 'restricted-editing-exception:1', {
  400. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  401. usingOperation: true,
  402. affectsData: true
  403. } );
  404. } );
  405. // <paragraph>foo <marker≥bar</marker> <marker≥baz</marker≥ qux[]</paragraph>
  406. model.change( writer => {
  407. writer.addMarker( 'restricted-editing-exception:2', {
  408. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  409. usingOperation: true,
  410. affectsData: true
  411. } );
  412. } );
  413. domEvtDataStub.keyCode += getCode( 'Shift' );
  414. view.document.fire( 'keydown', domEvtDataStub );
  415. sinon.assert.calledOnce( editor.execute );
  416. sinon.assert.calledWithExactly( editor.execute, 'goToPreviousRestrictedEditingRegion' );
  417. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  418. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  419. } );
  420. it( 'should not move to the closest previous exception on shift+tab key when there is none', () => {
  421. setModelData( model, '<paragraph>[]foo qux</paragraph>' );
  422. const paragraph = model.document.getRoot().getChild( 0 );
  423. // <paragraph>[]foo <marker≥qux</marker></paragraph>
  424. model.change( writer => {
  425. writer.addMarker( 'restricted-editing-exception:1', {
  426. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  427. usingOperation: true,
  428. affectsData: true
  429. } );
  430. } );
  431. domEvtDataStub.keyCode += getCode( 'Shift' );
  432. view.document.fire( 'keydown', domEvtDataStub );
  433. sinon.assert.notCalled( editor.execute );
  434. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  435. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  436. } );
  437. } );
  438. } );