restrictededitingmodeediting.js 27 KB

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