8
0

restrictededitingediting.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. const expectedView = '<p><span class="ck-restricted-editing-exception"><b>foo bar baz</b></span></p>';
  105. expect( editor.getData() ).to.equal( expectedView );
  106. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  107. } );
  108. } );
  109. } );
  110. describe( 'editing behavior', () => {
  111. let model;
  112. beforeEach( async () => {
  113. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingEditing ] } );
  114. model = editor.model;
  115. } );
  116. afterEach( () => {
  117. return editor.destroy();
  118. } );
  119. it( 'should keep markers in the view when editable region is edited', () => {
  120. setModelData( model,
  121. '<paragraph>foo bar baz</paragraph>' +
  122. '<paragraph>xxx y[]yy zzz</paragraph>'
  123. );
  124. const firstParagraph = model.document.getRoot().getChild( 0 );
  125. const secondParagraph = model.document.getRoot().getChild( 1 );
  126. model.change( writer => {
  127. writer.addMarker( 'restricted-editing-exception:1', {
  128. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  129. usingOperation: true,
  130. affectsData: true
  131. } );
  132. writer.addMarker( 'restricted-editing-exception:2', {
  133. range: writer.createRange(
  134. writer.createPositionAt( secondParagraph, 4 ),
  135. writer.createPositionAt( secondParagraph, 7 )
  136. ),
  137. usingOperation: true,
  138. affectsData: true
  139. } );
  140. } );
  141. model.change( writer => {
  142. model.insertContent( writer.createText( 'R', model.document.selection.getAttributes() ) );
  143. } );
  144. expect( editor.getData() ).to.equal(
  145. '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  146. '<p>xxx <span class="ck-restricted-editing-exception">yRyy</span> zzz</p>' );
  147. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  148. '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  149. '<p>xxx <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">yRyy</span> zzz</p>' );
  150. } );
  151. it( 'should block user typing outside exception markers', () => {
  152. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  153. editor.execute( 'input', { text: 'X' } );
  154. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  155. } );
  156. it( 'should not block user typing inside exception marker', () => {
  157. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  158. const firstParagraph = model.document.getRoot().getChild( 0 );
  159. model.change( writer => {
  160. writer.addMarker( 'restricted-editing-exception:1', {
  161. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  162. usingOperation: true,
  163. affectsData: true
  164. } );
  165. } );
  166. model.change( writer => {
  167. writer.setSelection( firstParagraph, 5 );
  168. } );
  169. editor.execute( 'input', { text: 'X' } );
  170. assertEqualMarkup( getModelData( model ), '<paragraph>foo bX[]ar baz</paragraph>' );
  171. } );
  172. it( 'should extend maker when typing on the marker boundary (end)', () => {
  173. setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
  174. const firstParagraph = model.document.getRoot().getChild( 0 );
  175. model.change( writer => {
  176. writer.addMarker( 'restricted-editing-exception:1', {
  177. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  178. usingOperation: true,
  179. affectsData: true
  180. } );
  181. } );
  182. editor.execute( 'input', { text: 'X' } );
  183. assertEqualMarkup( getModelData( model ), '<paragraph>foo barX[] baz</paragraph>' );
  184. const markerRange = editor.model.markers.get( 'restricted-editing-exception:1' ).getRange();
  185. const expectedRange = model.createRange(
  186. model.createPositionAt( firstParagraph, 4 ),
  187. model.createPositionAt( firstParagraph, 8 )
  188. );
  189. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  190. } );
  191. it( 'should extend marker when typing on the marker boundary (start)', () => {
  192. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  193. const firstParagraph = model.document.getRoot().getChild( 0 );
  194. model.change( writer => {
  195. writer.addMarker( 'restricted-editing-exception:1', {
  196. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  197. usingOperation: true,
  198. affectsData: true
  199. } );
  200. } );
  201. editor.execute( 'input', { text: 'X' } );
  202. assertEqualMarkup( getModelData( model ), '<paragraph>foo X[]bar baz</paragraph>' );
  203. const markerRange = editor.model.markers.get( 'restricted-editing-exception:1' ).getRange();
  204. const expectedRange = model.createRange(
  205. model.createPositionAt( firstParagraph, 4 ),
  206. model.createPositionAt( firstParagraph, 8 )
  207. );
  208. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  209. } );
  210. it( 'should extend marker when typing on the marker boundary (collapsed marker)', () => {
  211. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  212. const firstParagraph = model.document.getRoot().getChild( 0 );
  213. model.change( writer => {
  214. writer.addMarker( 'restricted-editing-exception:1', {
  215. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ) ),
  216. usingOperation: true,
  217. affectsData: true
  218. } );
  219. } );
  220. model.change( writer => {
  221. writer.setSelection( writer.createPositionAt( firstParagraph, 4 ) );
  222. } );
  223. editor.execute( 'input', { text: 'X' } );
  224. assertEqualMarkup( getModelData( model ), '<paragraph>foo X[]bar baz</paragraph>' );
  225. const markerRange = editor.model.markers.get( 'restricted-editing-exception:1' ).getRange();
  226. const expectedRange = model.createRange(
  227. model.createPositionAt( firstParagraph, 4 ),
  228. model.createPositionAt( firstParagraph, 5 )
  229. );
  230. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  231. } );
  232. it( 'should not move collapsed marker to $graveyard', () => {
  233. setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
  234. const firstParagraph = model.document.getRoot().getChild( 0 );
  235. model.change( writer => {
  236. writer.addMarker( 'restricted-editing-exception:1', {
  237. range: writer.createRange(
  238. writer.createPositionAt( firstParagraph, 4 ),
  239. writer.createPositionAt( firstParagraph, 5 )
  240. ),
  241. usingOperation: true,
  242. affectsData: true
  243. } );
  244. } );
  245. editor.execute( 'delete' );
  246. assertEqualMarkup( getModelData( model ), '<paragraph>foo []ar baz</paragraph>' );
  247. const markerRange = editor.model.markers.get( 'restricted-editing-exception:1' ).getRange();
  248. const expectedRange = model.createRange(
  249. model.createPositionAt( firstParagraph, 4 ),
  250. model.createPositionAt( firstParagraph, 4 )
  251. );
  252. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  253. } );
  254. } );
  255. describe( 'exception highlighting', () => {
  256. let model, view;
  257. beforeEach( async () => {
  258. editor = await VirtualTestEditor.create( {
  259. plugins: [ Paragraph, RestrictedEditingEditing, BoldEditing ]
  260. } );
  261. model = editor.model;
  262. view = editor.editing.view;
  263. } );
  264. afterEach( () => {
  265. return editor.destroy();
  266. } );
  267. it( 'should convert the highlight to a proper view classes', () => {
  268. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  269. const paragraph = model.document.getRoot().getChild( 0 );
  270. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  271. model.change( writer => {
  272. writer.addMarker( 'restricted-editing-exception:1', {
  273. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  274. usingOperation: true,
  275. affectsData: true
  276. } );
  277. } );
  278. expect( getViewData( view ) ).to.equal(
  279. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{a}r</span> baz</p>'
  280. );
  281. } );
  282. it( 'should remove classes when selection is moved away from an exception', () => {
  283. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  284. const paragraph = model.document.getRoot().getChild( 0 );
  285. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  286. model.change( writer => {
  287. writer.addMarker( 'restricted-editing-exception:1', {
  288. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  289. usingOperation: true,
  290. affectsData: true
  291. } );
  292. } );
  293. expect( getViewData( view ) ).to.equal(
  294. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{a}r</span> baz</p>'
  295. );
  296. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  297. expect( getViewData( view ) ).to.equal(
  298. '<p>{}foo <span class="ck-restricted-editing-exception">bar</span> baz</p>'
  299. );
  300. } );
  301. it( 'should work correctly when selection is moved inside an exception', () => {
  302. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  303. const paragraph = model.document.getRoot().getChild( 0 );
  304. // <paragraph>[]foo <$marker>bar</$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. expect( getViewData( view ) ).to.equal(
  313. '<p>{}foo <span class="ck-restricted-editing-exception">bar</span> baz</p>'
  314. );
  315. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 6 ) );
  316. expect( getViewData( view ) ).to.equal(
  317. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">ba{}r</span> baz</p>'
  318. );
  319. } );
  320. describe( 'editing downcast conversion integration', () => {
  321. it( 'works for the #insert event', () => {
  322. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  323. const paragraph = model.document.getRoot().getChild( 0 );
  324. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  325. model.change( writer => {
  326. writer.addMarker( 'restricted-editing-exception:1', {
  327. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  328. usingOperation: true,
  329. affectsData: true
  330. } );
  331. } );
  332. model.change( writer => {
  333. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  334. } );
  335. expect( getViewData( view ) ).to.equal(
  336. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">bFOO{a}r</span> baz</p>'
  337. );
  338. } );
  339. it( 'works for the #remove event', () => {
  340. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  341. const paragraph = model.document.getRoot().getChild( 0 );
  342. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  343. model.change( writer => {
  344. writer.addMarker( 'restricted-editing-exception:1', {
  345. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  346. usingOperation: true,
  347. affectsData: true
  348. } );
  349. } );
  350. model.change( writer => {
  351. writer.remove( writer.createRange(
  352. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 ),
  353. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 6 )
  354. ) );
  355. } );
  356. expect( getViewData( view ) ).to.equal(
  357. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{}r</span> baz</p>'
  358. );
  359. } );
  360. it( 'works for the #attribute event', () => {
  361. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  362. const paragraph = model.document.getRoot().getChild( 0 );
  363. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  364. model.change( writer => {
  365. writer.addMarker( 'restricted-editing-exception:1', {
  366. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  367. usingOperation: true,
  368. affectsData: true
  369. } );
  370. } );
  371. model.change( writer => {
  372. writer.setAttribute( 'bold', true, writer.createRange(
  373. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  374. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  375. );
  376. } );
  377. expect( getViewData( view ) ).to.equal(
  378. '<p>foo ' +
  379. '<span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">' +
  380. '<strong>b{a</strong>' +
  381. '}r</span>' +
  382. ' baz</p>'
  383. );
  384. } );
  385. it( 'works for the #selection event', () => {
  386. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  387. const paragraph = model.document.getRoot().getChild( 0 );
  388. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  389. model.change( writer => {
  390. writer.addMarker( 'restricted-editing-exception:1', {
  391. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  392. usingOperation: true,
  393. affectsData: true
  394. } );
  395. } );
  396. model.change( writer => {
  397. writer.setSelection( writer.createRange(
  398. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  399. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  400. );
  401. } );
  402. expect( getViewData( view ) ).to.equal(
  403. '<p>foo {<span class="ck-restricted-editing-exception">ba}r</span> baz</p>'
  404. );
  405. } );
  406. it( 'works for the addMarker and removeMarker events', () => {
  407. editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
  408. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  409. const paragraph = model.document.getRoot().getChild( 0 );
  410. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  411. model.change( writer => {
  412. writer.addMarker( 'restricted-editing-exception:1', {
  413. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  414. usingOperation: true,
  415. affectsData: true
  416. } );
  417. } );
  418. model.change( writer => {
  419. const range = writer.createRange(
  420. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  421. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  422. );
  423. writer.addMarker( 'fooMarker', { range, usingOperation: true } );
  424. } );
  425. expect( getViewData( view ) ).to.equal(
  426. '<p>' +
  427. '<span>foo </span>' +
  428. '<span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">' +
  429. '<span>b</span>{a}r' +
  430. '</span>' +
  431. ' baz</p>'
  432. );
  433. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  434. expect( getViewData( view ) ).to.equal(
  435. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{a}r</span> baz</p>'
  436. );
  437. } );
  438. } );
  439. } );
  440. describe( 'exception cycling with the keyboard', () => {
  441. let model, view, domEvtDataStub;
  442. beforeEach( async () => {
  443. editor = await VirtualTestEditor.create( {
  444. plugins: [ Paragraph, RestrictedEditingEditing, BoldEditing ]
  445. } );
  446. model = editor.model;
  447. view = editor.editing.view;
  448. domEvtDataStub = {
  449. keyCode: getCode( 'Tab' ),
  450. preventDefault: sinon.spy(),
  451. stopPropagation: sinon.spy()
  452. };
  453. sinon.spy( editor, 'execute' );
  454. } );
  455. afterEach( () => {
  456. return editor.destroy();
  457. } );
  458. it( 'should move to the closest next exception on tab key', () => {
  459. setModelData( model, '<paragraph>[]foo bar baz qux</paragraph>' );
  460. const paragraph = model.document.getRoot().getChild( 0 );
  461. // <paragraph>[]foo <marker>bar</marker> baz qux</paragraph>
  462. model.change( writer => {
  463. writer.addMarker( 'restricted-editing-exception:1', {
  464. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  465. usingOperation: true,
  466. affectsData: true
  467. } );
  468. } );
  469. // <paragraph>[]foo <marker>bar</marker> <marker>baz</marker≥ qux</paragraph>
  470. model.change( writer => {
  471. writer.addMarker( 'restricted-editing-exception:2', {
  472. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  473. usingOperation: true,
  474. affectsData: true
  475. } );
  476. } );
  477. view.document.fire( 'keydown', domEvtDataStub );
  478. sinon.assert.calledOnce( editor.execute );
  479. sinon.assert.calledWithExactly( editor.execute, 'goToNextRestrictedEditingRegion' );
  480. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  481. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  482. } );
  483. it( 'should not move to the closest next exception on tab key when there is none', () => {
  484. setModelData( model, '<paragraph>foo qux[]</paragraph>' );
  485. const paragraph = model.document.getRoot().getChild( 0 );
  486. // <paragraph><marker>foo</marker> qux[]</paragraph>
  487. model.change( writer => {
  488. writer.addMarker( 'restricted-editing-exception:1', {
  489. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 3 ) ),
  490. usingOperation: true,
  491. affectsData: true
  492. } );
  493. } );
  494. view.document.fire( 'keydown', domEvtDataStub );
  495. sinon.assert.notCalled( editor.execute );
  496. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  497. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  498. } );
  499. it( 'should move to the closest previous exception on shift+tab key', () => {
  500. setModelData( model, '<paragraph>foo bar baz qux[]</paragraph>' );
  501. const paragraph = model.document.getRoot().getChild( 0 );
  502. // <paragraph>foo <marker>bar</marker> baz qux[]</paragraph>
  503. model.change( writer => {
  504. writer.addMarker( 'restricted-editing-exception:1', {
  505. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  506. usingOperation: true,
  507. affectsData: true
  508. } );
  509. } );
  510. // <paragraph>foo <marker>bar</marker> <marker>baz</marker≥ qux[]</paragraph>
  511. model.change( writer => {
  512. writer.addMarker( 'restricted-editing-exception:2', {
  513. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  514. usingOperation: true,
  515. affectsData: true
  516. } );
  517. } );
  518. domEvtDataStub.keyCode += getCode( 'Shift' );
  519. view.document.fire( 'keydown', domEvtDataStub );
  520. sinon.assert.calledOnce( editor.execute );
  521. sinon.assert.calledWithExactly( editor.execute, 'goToPreviousRestrictedEditingRegion' );
  522. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  523. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  524. } );
  525. it( 'should not move to the closest previous exception on shift+tab key when there is none', () => {
  526. setModelData( model, '<paragraph>[]foo qux</paragraph>' );
  527. const paragraph = model.document.getRoot().getChild( 0 );
  528. // <paragraph>[]foo <marker>qux</marker></paragraph>
  529. model.change( writer => {
  530. writer.addMarker( 'restricted-editing-exception:1', {
  531. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  532. usingOperation: true,
  533. affectsData: true
  534. } );
  535. } );
  536. domEvtDataStub.keyCode += getCode( 'Shift' );
  537. view.document.fire( 'keydown', domEvtDataStub );
  538. sinon.assert.notCalled( editor.execute );
  539. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  540. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  541. } );
  542. } );
  543. } );