restrictededitingmodeediting.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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, model;
  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 "goToPreviousRestrictedEditingException" command', () => {
  34. expect( editor.commands.get( 'goToPreviousRestrictedEditingException' ) )
  35. .to.be.instanceOf( RestrictedEditingModeNavigationCommand );
  36. } );
  37. it( 'adds a "goToNextRestrictedEditingException" command', () => {
  38. expect(
  39. editor.commands.get( 'goToNextRestrictedEditingException' )
  40. ).to.be.instanceOf( RestrictedEditingModeNavigationCommand );
  41. } );
  42. } );
  43. describe( 'conversion', () => {
  44. let model;
  45. beforeEach( async () => {
  46. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, RestrictedEditingModeEditing ] } );
  47. model = editor.model;
  48. } );
  49. afterEach( () => {
  50. return editor.destroy();
  51. } );
  52. describe( 'upcast', () => {
  53. it( 'should convert <span class="ck-restricted-editing-exception"> to marker', () => {
  54. editor.setData( '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' );
  55. expect( model.markers.has( 'restrictedEditingException:1' ) ).to.be.true;
  56. const marker = model.markers.get( 'restrictedEditingException:1' );
  57. expect( marker.getStart().path ).to.deep.equal( [ 0, 4 ] );
  58. expect( marker.getEnd().path ).to.deep.equal( [ 0, 7 ] );
  59. } );
  60. it( 'should convert multiple <span class="ck-restricted-editing-exception">', () => {
  61. editor.setData(
  62. '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  63. '<p>ABCDEF<span class="ck-restricted-editing-exception">GHIJK</span>LMNOPQRST</p>'
  64. );
  65. expect( model.markers.has( 'restrictedEditingException:1' ) ).to.be.true;
  66. expect( model.markers.has( 'restrictedEditingException:2' ) ).to.be.true;
  67. // Data for the first marker is the same as in previous tests so no need to test it again.
  68. const secondMarker = model.markers.get( 'restrictedEditingException:2' );
  69. expect( secondMarker.getStart().path ).to.deep.equal( [ 1, 6 ] );
  70. expect( secondMarker.getEnd().path ).to.deep.equal( [ 1, 11 ] );
  71. } );
  72. it( 'should not convert other <span> elements', () => {
  73. editor.setData( '<p>foo <span class="foo bar">bar</span> baz</p>' );
  74. expect( model.markers.has( 'restrictedEditingException:1' ) ).to.be.false;
  75. } );
  76. } );
  77. describe( 'downcast', () => {
  78. it( 'should convert model marker to <span>', () => {
  79. setModelData( model, '<paragraph>foo bar baz</paragraph>' );
  80. const paragraph = model.document.getRoot().getChild( 0 );
  81. model.change( writer => {
  82. writer.addMarker( 'restrictedEditingException:1', {
  83. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  84. usingOperation: true,
  85. affectsData: true
  86. } );
  87. } );
  88. const expectedView = '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>';
  89. expect( editor.getData() ).to.equal( expectedView );
  90. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  91. } );
  92. it( 'should convert collapsed model marker to <span>', () => {
  93. setModelData( model, '<paragraph>foo bar baz</paragraph>' );
  94. const paragraph = model.document.getRoot().getChild( 0 );
  95. model.change( writer => {
  96. writer.addMarker( 'restrictedEditingException:1', {
  97. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 4 ) ),
  98. usingOperation: true,
  99. affectsData: true
  100. } );
  101. } );
  102. expect( editor.getData() ).to.equal( '<p>foo <span class="ck-restricted-editing-exception"></span>bar baz</p>' );
  103. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  104. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_collapsed"></span>bar baz</p>'
  105. );
  106. } );
  107. it( 'converted <span> should be the outermost attribute element', () => {
  108. editor.conversion.for( 'downcast' ).attributeToElement( { model: 'bold', view: 'b' } );
  109. setModelData( model, '<paragraph><$text bold="true">foo bar baz</$text></paragraph>' );
  110. const paragraph = model.document.getRoot().getChild( 0 );
  111. model.change( writer => {
  112. writer.addMarker( 'restrictedEditingException:1', {
  113. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 'end' ) ),
  114. usingOperation: true,
  115. affectsData: true
  116. } );
  117. } );
  118. expect( editor.getData() ).to.equal(
  119. '<p><span class="ck-restricted-editing-exception"><b>foo bar baz</b></span></p>'
  120. );
  121. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  122. '<p>' +
  123. '<span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected"><b>foo bar baz</b></span>' +
  124. '</p>'
  125. );
  126. } );
  127. } );
  128. } );
  129. describe( 'editing behavior', () => {
  130. let model;
  131. beforeEach( async () => {
  132. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingModeEditing ] } );
  133. model = editor.model;
  134. } );
  135. afterEach( () => {
  136. return editor.destroy();
  137. } );
  138. it( 'should keep markers in the view when editable region is edited', () => {
  139. setModelData( model,
  140. '<paragraph>foo bar baz</paragraph>' +
  141. '<paragraph>xxx y[]yy zzz</paragraph>'
  142. );
  143. const firstParagraph = model.document.getRoot().getChild( 0 );
  144. const secondParagraph = model.document.getRoot().getChild( 1 );
  145. model.change( writer => {
  146. writer.addMarker( 'restrictedEditingException:1', {
  147. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  148. usingOperation: true,
  149. affectsData: true
  150. } );
  151. writer.addMarker( 'restrictedEditingException:2', {
  152. range: writer.createRange(
  153. writer.createPositionAt( secondParagraph, 4 ),
  154. writer.createPositionAt( secondParagraph, 7 )
  155. ),
  156. usingOperation: true,
  157. affectsData: true
  158. } );
  159. } );
  160. model.change( writer => {
  161. model.insertContent( writer.createText( 'R', model.document.selection.getAttributes() ) );
  162. } );
  163. expect( editor.getData() ).to.equal(
  164. '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  165. '<p>xxx <span class="ck-restricted-editing-exception">yRyy</span> zzz</p>' );
  166. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  167. '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  168. '<p>xxx <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">yRyy</span> zzz</p>' );
  169. } );
  170. it( 'should block user typing outside exception markers', () => {
  171. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  172. editor.execute( 'input', { text: 'X' } );
  173. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  174. } );
  175. it( 'should not block user typing inside exception marker', () => {
  176. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  177. const firstParagraph = model.document.getRoot().getChild( 0 );
  178. model.change( writer => {
  179. writer.addMarker( 'restrictedEditingException:1', {
  180. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  181. usingOperation: true,
  182. affectsData: true
  183. } );
  184. } );
  185. model.change( writer => {
  186. writer.setSelection( firstParagraph, 5 );
  187. } );
  188. editor.execute( 'input', { text: 'X' } );
  189. assertEqualMarkup( getModelData( model ), '<paragraph>foo bX[]ar baz</paragraph>' );
  190. } );
  191. it( 'should extend maker when typing on the marker boundary (end)', () => {
  192. setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
  193. const firstParagraph = model.document.getRoot().getChild( 0 );
  194. model.change( writer => {
  195. writer.addMarker( 'restrictedEditingException: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 barX[] baz</paragraph>' );
  203. const markerRange = editor.model.markers.get( 'restrictedEditingException: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 (start)', () => {
  211. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  212. const firstParagraph = model.document.getRoot().getChild( 0 );
  213. model.change( writer => {
  214. writer.addMarker( 'restrictedEditingException:1', {
  215. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  216. usingOperation: true,
  217. affectsData: true
  218. } );
  219. } );
  220. editor.execute( 'input', { text: 'X' } );
  221. assertEqualMarkup( getModelData( model ), '<paragraph>foo X[]bar baz</paragraph>' );
  222. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  223. const expectedRange = model.createRange(
  224. model.createPositionAt( firstParagraph, 4 ),
  225. model.createPositionAt( firstParagraph, 8 )
  226. );
  227. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  228. } );
  229. it( 'should extend marker when typing on the marker boundary (collapsed marker)', () => {
  230. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  231. const firstParagraph = model.document.getRoot().getChild( 0 );
  232. model.change( writer => {
  233. writer.addMarker( 'restrictedEditingException:1', {
  234. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ) ),
  235. usingOperation: true,
  236. affectsData: true
  237. } );
  238. } );
  239. model.change( writer => {
  240. writer.setSelection( writer.createPositionAt( firstParagraph, 4 ) );
  241. } );
  242. editor.execute( 'input', { text: 'X' } );
  243. assertEqualMarkup( getModelData( model ), '<paragraph>foo X[]bar baz</paragraph>' );
  244. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  245. const expectedRange = model.createRange(
  246. model.createPositionAt( firstParagraph, 4 ),
  247. model.createPositionAt( firstParagraph, 5 )
  248. );
  249. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  250. } );
  251. it( 'should not move collapsed marker to $graveyard', () => {
  252. setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
  253. const firstParagraph = model.document.getRoot().getChild( 0 );
  254. model.change( writer => {
  255. writer.addMarker( 'restrictedEditingException:1', {
  256. range: writer.createRange(
  257. writer.createPositionAt( firstParagraph, 4 ),
  258. writer.createPositionAt( firstParagraph, 5 )
  259. ),
  260. usingOperation: true,
  261. affectsData: true
  262. } );
  263. } );
  264. editor.execute( 'delete' );
  265. assertEqualMarkup( getModelData( model ), '<paragraph>foo []ar baz</paragraph>' );
  266. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  267. const expectedRange = model.createRange(
  268. model.createPositionAt( firstParagraph, 4 ),
  269. model.createPositionAt( firstParagraph, 4 )
  270. );
  271. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  272. } );
  273. } );
  274. describe.only( 'post-fixer', () => {
  275. beforeEach( async () => {
  276. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingModeEditing ] } );
  277. model = editor.model;
  278. } );
  279. afterEach( () => {
  280. return editor.destroy();
  281. } );
  282. it( 'should not allow to change text outside restricted area', () => {
  283. setModelData( model, '<paragraph>[]foofoo bar baz</paragraph>' );
  284. const firstParagraph = model.document.getRoot().getChild( 0 );
  285. addExceptionMarker( 3, 9, firstParagraph );
  286. model.change( writer => {
  287. writer.setSelection( firstParagraph, 6 );
  288. } );
  289. editor.execute( 'delete', { unit: 'word' } );
  290. assertEqualMarkup( getModelData( model ), '<paragraph>foo[] bar baz</paragraph>' );
  291. } );
  292. } );
  293. describe( 'clipboard', () => {
  294. let model, viewDoc;
  295. beforeEach( async () => {
  296. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, Clipboard, RestrictedEditingModeEditing ] } );
  297. model = editor.model;
  298. viewDoc = editor.editing.view.document;
  299. } );
  300. afterEach( () => {
  301. return editor.destroy();
  302. } );
  303. describe( 'cut', () => {
  304. it( 'should be blocked outside exception markers', () => {
  305. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  306. const spy = sinon.spy();
  307. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  308. viewDoc.fire( 'clipboardOutput', {
  309. content: {
  310. isEmpty: true
  311. },
  312. method: 'cut'
  313. } );
  314. sinon.assert.notCalled( spy );
  315. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  316. } );
  317. it( 'should be blocked inside exception marker', () => {
  318. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  319. const firstParagraph = model.document.getRoot().getChild( 0 );
  320. const spy = sinon.spy();
  321. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  322. model.change( writer => {
  323. writer.addMarker( 'restrictedEditingException:1', {
  324. range: writer.createRange(
  325. writer.createPositionAt( firstParagraph, 4 ),
  326. writer.createPositionAt( firstParagraph, 7 )
  327. ),
  328. usingOperation: true,
  329. affectsData: true
  330. } );
  331. } );
  332. model.change( writer => {
  333. writer.setSelection( firstParagraph, 5 );
  334. } );
  335. viewDoc.fire( 'clipboardOutput', {
  336. content: {
  337. isEmpty: true
  338. },
  339. method: 'cut'
  340. } );
  341. sinon.assert.notCalled( spy );
  342. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  343. } );
  344. } );
  345. describe( 'copy', () => {
  346. it( 'should not be blocked outside exception markers', () => {
  347. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  348. const spy = sinon.spy();
  349. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  350. viewDoc.fire( 'clipboardOutput', {
  351. content: {
  352. isEmpty: true
  353. },
  354. method: 'copy'
  355. } );
  356. sinon.assert.calledOnce( spy );
  357. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  358. } );
  359. it( 'should not be blocked inside exception marker', () => {
  360. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  361. const firstParagraph = model.document.getRoot().getChild( 0 );
  362. const spy = sinon.spy();
  363. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  364. model.change( writer => {
  365. writer.addMarker( 'restrictedEditingException:1', {
  366. range: writer.createRange(
  367. writer.createPositionAt( firstParagraph, 4 ),
  368. writer.createPositionAt( firstParagraph, 7 )
  369. ),
  370. usingOperation: true,
  371. affectsData: true
  372. } );
  373. } );
  374. model.change( writer => {
  375. writer.setSelection( firstParagraph, 5 );
  376. } );
  377. viewDoc.fire( 'clipboardOutput', {
  378. content: {
  379. isEmpty: true
  380. },
  381. method: 'copy'
  382. } );
  383. sinon.assert.calledOnce( spy );
  384. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  385. } );
  386. } );
  387. describe( 'paste', () => {
  388. it( 'should be blocked outside exception markers', () => {
  389. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  390. const spy = sinon.spy();
  391. viewDoc.on( 'clipboardInput', spy, { priority: 'high' } );
  392. viewDoc.fire( 'clipboardInput', {
  393. dataTransfer: {
  394. getData: sinon.spy()
  395. }
  396. } );
  397. sinon.assert.notCalled( spy );
  398. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  399. } );
  400. it( 'should be blocked inside exception marker', () => {
  401. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  402. const firstParagraph = model.document.getRoot().getChild( 0 );
  403. const spy = sinon.spy();
  404. viewDoc.on( 'clipboardInput', spy, { priority: 'high' } );
  405. model.change( writer => {
  406. writer.addMarker( 'restrictedEditingException:1', {
  407. range: writer.createRange(
  408. writer.createPositionAt( firstParagraph, 4 ),
  409. writer.createPositionAt( firstParagraph, 7 )
  410. ),
  411. usingOperation: true,
  412. affectsData: true
  413. } );
  414. } );
  415. model.change( writer => {
  416. writer.setSelection( firstParagraph, 5 );
  417. } );
  418. viewDoc.fire( 'clipboardInput', {
  419. dataTransfer: {
  420. getData: sinon.spy()
  421. }
  422. } );
  423. sinon.assert.notCalled( spy );
  424. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  425. } );
  426. } );
  427. } );
  428. describe( 'exception highlighting', () => {
  429. let model, view;
  430. beforeEach( async () => {
  431. editor = await VirtualTestEditor.create( {
  432. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  433. } );
  434. model = editor.model;
  435. view = editor.editing.view;
  436. } );
  437. afterEach( () => {
  438. return editor.destroy();
  439. } );
  440. it( 'should convert the highlight to a proper view classes', () => {
  441. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  442. const paragraph = model.document.getRoot().getChild( 0 );
  443. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  444. model.change( writer => {
  445. writer.addMarker( 'restrictedEditingException:1', {
  446. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  447. usingOperation: true,
  448. affectsData: true
  449. } );
  450. } );
  451. expect( getViewData( view ) ).to.equal(
  452. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{a}r</span> baz</p>'
  453. );
  454. } );
  455. it( 'should remove classes when selection is moved away from an exception', () => {
  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( 'restrictedEditingException:1', {
  461. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  462. usingOperation: true,
  463. affectsData: true
  464. } );
  465. } );
  466. expect( getViewData( view ) ).to.equal(
  467. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{a}r</span> baz</p>'
  468. );
  469. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  470. expect( getViewData( view ) ).to.equal(
  471. '<p>{}foo <span class="ck-restricted-editing-exception">bar</span> baz</p>'
  472. );
  473. } );
  474. it( 'should work correctly when selection is moved inside an exception', () => {
  475. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  476. const paragraph = model.document.getRoot().getChild( 0 );
  477. // <paragraph>[]foo <$marker>bar</$marker> baz</paragraph>
  478. model.change( writer => {
  479. writer.addMarker( 'restrictedEditingException:1', {
  480. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  481. usingOperation: true,
  482. affectsData: true
  483. } );
  484. } );
  485. expect( getViewData( view ) ).to.equal(
  486. '<p>{}foo <span class="ck-restricted-editing-exception">bar</span> baz</p>'
  487. );
  488. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 6 ) );
  489. expect( getViewData( view ) ).to.equal(
  490. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">ba{}r</span> baz</p>'
  491. );
  492. } );
  493. describe( 'editing downcast conversion integration', () => {
  494. it( 'works for the #insert event', () => {
  495. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  496. const paragraph = model.document.getRoot().getChild( 0 );
  497. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  498. model.change( writer => {
  499. writer.addMarker( 'restrictedEditingException:1', {
  500. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  501. usingOperation: true,
  502. affectsData: true
  503. } );
  504. } );
  505. model.change( writer => {
  506. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  507. } );
  508. expect( getViewData( view ) ).to.equal(
  509. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">bFOO{a}r</span> baz</p>'
  510. );
  511. } );
  512. it( 'works for the #remove event', () => {
  513. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  514. const paragraph = model.document.getRoot().getChild( 0 );
  515. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  516. model.change( writer => {
  517. writer.addMarker( 'restrictedEditingException:1', {
  518. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  519. usingOperation: true,
  520. affectsData: true
  521. } );
  522. } );
  523. model.change( writer => {
  524. writer.remove( writer.createRange(
  525. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 ),
  526. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 6 )
  527. ) );
  528. } );
  529. expect( getViewData( view ) ).to.equal(
  530. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{}r</span> baz</p>'
  531. );
  532. } );
  533. it( 'works for the #attribute event', () => {
  534. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  535. const paragraph = model.document.getRoot().getChild( 0 );
  536. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  537. model.change( writer => {
  538. writer.addMarker( 'restrictedEditingException:1', {
  539. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  540. usingOperation: true,
  541. affectsData: true
  542. } );
  543. } );
  544. model.change( writer => {
  545. writer.setAttribute( 'bold', true, writer.createRange(
  546. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  547. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  548. );
  549. } );
  550. expect( getViewData( view ) ).to.equal(
  551. '<p>foo ' +
  552. '<span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">' +
  553. '<strong>b{a</strong>' +
  554. '}r</span>' +
  555. ' baz</p>'
  556. );
  557. } );
  558. it( 'works for the #selection event', () => {
  559. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  560. const paragraph = model.document.getRoot().getChild( 0 );
  561. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  562. model.change( writer => {
  563. writer.addMarker( 'restrictedEditingException:1', {
  564. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  565. usingOperation: true,
  566. affectsData: true
  567. } );
  568. } );
  569. model.change( writer => {
  570. writer.setSelection( writer.createRange(
  571. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  572. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  573. );
  574. } );
  575. expect( getViewData( view ) ).to.equal(
  576. '<p>foo {<span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">ba}r</span> baz</p>'
  577. );
  578. } );
  579. it( 'works for the addMarker and removeMarker events', () => {
  580. editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
  581. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  582. const paragraph = model.document.getRoot().getChild( 0 );
  583. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  584. model.change( writer => {
  585. writer.addMarker( 'restrictedEditingException:1', {
  586. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  587. usingOperation: true,
  588. affectsData: true
  589. } );
  590. } );
  591. model.change( writer => {
  592. const range = writer.createRange(
  593. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  594. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  595. );
  596. writer.addMarker( 'fooMarker', { range, usingOperation: true } );
  597. } );
  598. expect( getViewData( view ) ).to.equal(
  599. '<p>' +
  600. '<span>foo </span>' +
  601. '<span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">' +
  602. '<span>b</span>{a}r' +
  603. '</span>' +
  604. ' baz</p>'
  605. );
  606. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  607. expect( getViewData( view ) ).to.equal(
  608. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{a}r</span> baz</p>'
  609. );
  610. } );
  611. } );
  612. } );
  613. describe( 'exception cycling with the keyboard', () => {
  614. let model, view, domEvtDataStub;
  615. beforeEach( async () => {
  616. editor = await VirtualTestEditor.create( {
  617. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  618. } );
  619. model = editor.model;
  620. view = editor.editing.view;
  621. domEvtDataStub = {
  622. keyCode: getCode( 'Tab' ),
  623. preventDefault: sinon.spy(),
  624. stopPropagation: sinon.spy()
  625. };
  626. sinon.spy( editor, 'execute' );
  627. } );
  628. afterEach( () => {
  629. return editor.destroy();
  630. } );
  631. it( 'should move to the closest next exception on tab key', () => {
  632. setModelData( model, '<paragraph>[]foo bar baz qux</paragraph>' );
  633. const paragraph = model.document.getRoot().getChild( 0 );
  634. // <paragraph>[]foo <marker>bar</marker> baz qux</paragraph>
  635. model.change( writer => {
  636. writer.addMarker( 'restrictedEditingException:1', {
  637. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  638. usingOperation: true,
  639. affectsData: true
  640. } );
  641. } );
  642. // <paragraph>[]foo <marker>bar</marker> <marker>baz</marker≥ qux</paragraph>
  643. model.change( writer => {
  644. writer.addMarker( 'restrictedEditingException:2', {
  645. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  646. usingOperation: true,
  647. affectsData: true
  648. } );
  649. } );
  650. view.document.fire( 'keydown', domEvtDataStub );
  651. sinon.assert.calledOnce( editor.execute );
  652. sinon.assert.calledWithExactly( editor.execute, 'goToNextRestrictedEditingException' );
  653. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  654. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  655. } );
  656. it( 'should not move to the closest next exception on tab key when there is none', () => {
  657. setModelData( model, '<paragraph>foo qux[]</paragraph>' );
  658. const paragraph = model.document.getRoot().getChild( 0 );
  659. // <paragraph><marker>foo</marker> qux[]</paragraph>
  660. model.change( writer => {
  661. writer.addMarker( 'restrictedEditingException:1', {
  662. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 3 ) ),
  663. usingOperation: true,
  664. affectsData: true
  665. } );
  666. } );
  667. view.document.fire( 'keydown', domEvtDataStub );
  668. sinon.assert.notCalled( editor.execute );
  669. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  670. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  671. } );
  672. it( 'should move to the closest previous exception on shift+tab key', () => {
  673. setModelData( model, '<paragraph>foo bar baz qux[]</paragraph>' );
  674. const paragraph = model.document.getRoot().getChild( 0 );
  675. // <paragraph>foo <marker>bar</marker> baz qux[]</paragraph>
  676. model.change( writer => {
  677. writer.addMarker( 'restrictedEditingException:1', {
  678. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  679. usingOperation: true,
  680. affectsData: true
  681. } );
  682. } );
  683. // <paragraph>foo <marker>bar</marker> <marker>baz</marker≥ qux[]</paragraph>
  684. model.change( writer => {
  685. writer.addMarker( 'restrictedEditingException:2', {
  686. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  687. usingOperation: true,
  688. affectsData: true
  689. } );
  690. } );
  691. domEvtDataStub.keyCode += getCode( 'Shift' );
  692. view.document.fire( 'keydown', domEvtDataStub );
  693. sinon.assert.calledOnce( editor.execute );
  694. sinon.assert.calledWithExactly( editor.execute, 'goToPreviousRestrictedEditingException' );
  695. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  696. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  697. } );
  698. it( 'should not move to the closest previous exception on shift+tab key when there is none', () => {
  699. setModelData( model, '<paragraph>[]foo qux</paragraph>' );
  700. const paragraph = model.document.getRoot().getChild( 0 );
  701. // <paragraph>[]foo <marker>qux</marker></paragraph>
  702. model.change( writer => {
  703. writer.addMarker( 'restrictedEditingException:1', {
  704. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  705. usingOperation: true,
  706. affectsData: true
  707. } );
  708. } );
  709. domEvtDataStub.keyCode += getCode( 'Shift' );
  710. view.document.fire( 'keydown', domEvtDataStub );
  711. sinon.assert.notCalled( editor.execute );
  712. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  713. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  714. } );
  715. } );
  716. function addExceptionMarker( start, end = start, parent, id = 1 ) {
  717. model.change( writer => {
  718. writer.addMarker( `restrictedEditingException:${ id }`, {
  719. range: writer.createRange( writer.createPositionAt( parent, start ), writer.createPositionAt( parent, end ) ),
  720. usingOperation: true,
  721. affectsData: true
  722. } );
  723. } );
  724. }
  725. } );