restrictededitingmodeediting.js 26 KB

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