8
0

restrictededitingediting.js 25 KB

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