restrictededitingmodeediting.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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. beforeEach( async () => {
  45. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, RestrictedEditingModeEditing ] } );
  46. model = editor.model;
  47. } );
  48. afterEach( () => {
  49. return editor.destroy();
  50. } );
  51. describe( 'upcast', () => {
  52. it( 'should convert <span class="ck-restricted-editing-exception"> to marker', () => {
  53. editor.setData( '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' );
  54. expect( model.markers.has( 'restrictedEditingException:1' ) ).to.be.true;
  55. const marker = model.markers.get( 'restrictedEditingException:1' );
  56. expect( marker.getStart().path ).to.deep.equal( [ 0, 4 ] );
  57. expect( marker.getEnd().path ).to.deep.equal( [ 0, 7 ] );
  58. } );
  59. it( 'should convert multiple <span class="ck-restricted-editing-exception">', () => {
  60. editor.setData(
  61. '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  62. '<p>ABCDEF<span class="ck-restricted-editing-exception">GHIJK</span>LMNOPQRST</p>'
  63. );
  64. expect( model.markers.has( 'restrictedEditingException:1' ) ).to.be.true;
  65. expect( model.markers.has( 'restrictedEditingException:2' ) ).to.be.true;
  66. // Data for the first marker is the same as in previous tests so no need to test it again.
  67. const secondMarker = model.markers.get( 'restrictedEditingException:2' );
  68. expect( secondMarker.getStart().path ).to.deep.equal( [ 1, 6 ] );
  69. expect( secondMarker.getEnd().path ).to.deep.equal( [ 1, 11 ] );
  70. } );
  71. it( 'should not convert other <span> elements', () => {
  72. editor.setData( '<p>foo <span class="foo bar">bar</span> baz</p>' );
  73. expect( model.markers.has( 'restrictedEditingException:1' ) ).to.be.false;
  74. } );
  75. } );
  76. describe( 'downcast', () => {
  77. it( 'should convert model marker to <span>', () => {
  78. setModelData( model, '<paragraph>foo bar baz</paragraph>' );
  79. const paragraph = model.document.getRoot().getChild( 0 );
  80. model.change( writer => {
  81. writer.addMarker( 'restrictedEditingException:1', {
  82. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  83. usingOperation: true,
  84. affectsData: true
  85. } );
  86. } );
  87. const expectedView = '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>';
  88. expect( editor.getData() ).to.equal( expectedView );
  89. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  90. } );
  91. it( 'should convert collapsed model marker to <span>', () => {
  92. setModelData( model, '<paragraph>foo bar baz</paragraph>' );
  93. const paragraph = model.document.getRoot().getChild( 0 );
  94. model.change( writer => {
  95. writer.addMarker( 'restrictedEditingException:1', {
  96. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 4 ) ),
  97. usingOperation: true,
  98. affectsData: true
  99. } );
  100. } );
  101. expect( editor.getData() ).to.equal( '<p>foo <span class="ck-restricted-editing-exception"></span>bar baz</p>' );
  102. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  103. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_collapsed"></span>bar baz</p>'
  104. );
  105. } );
  106. it( 'converted <span> should be the outermost attribute element', () => {
  107. editor.conversion.for( 'downcast' ).attributeToElement( { model: 'bold', view: 'b' } );
  108. setModelData( model, '<paragraph><$text bold="true">foo bar baz</$text></paragraph>' );
  109. const paragraph = model.document.getRoot().getChild( 0 );
  110. model.change( writer => {
  111. writer.addMarker( 'restrictedEditingException:1', {
  112. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 'end' ) ),
  113. usingOperation: true,
  114. affectsData: true
  115. } );
  116. } );
  117. expect( editor.getData() ).to.equal(
  118. '<p><span class="ck-restricted-editing-exception"><b>foo bar baz</b></span></p>'
  119. );
  120. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  121. '<p>' +
  122. '<span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected"><b>foo bar baz</b></span>' +
  123. '</p>'
  124. );
  125. } );
  126. } );
  127. } );
  128. describe( 'editing behavior', () => {
  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( 'restrictedEditingException:1', {
  145. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  146. usingOperation: true,
  147. affectsData: true
  148. } );
  149. writer.addMarker( 'restrictedEditingException: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( 'restrictedEditingException: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( 'restrictedEditingException: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( 'restrictedEditingException: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( 'restrictedEditingException: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( 'restrictedEditingException: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( 'restrictedEditingException: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( 'restrictedEditingException: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( 'restrictedEditingException: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( 'restrictedEditingException: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( 'post-fixer', () => {
  273. beforeEach( async () => {
  274. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingModeEditing ] } );
  275. model = editor.model;
  276. } );
  277. afterEach( async () => {
  278. await editor.destroy();
  279. } );
  280. it( 'should not allow to delete content outside restricted area', () => {
  281. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  282. const firstParagraph = model.document.getRoot().getChild( 0 );
  283. addExceptionMarker( 3, 9, firstParagraph );
  284. model.change( writer => {
  285. writer.setSelection( firstParagraph, 2 );
  286. } );
  287. model.deleteContent( model.document.selection );
  288. assertEqualMarkup( getModelData( model ), '<paragraph>fo[]o bar baz</paragraph>' );
  289. } );
  290. it( 'should trim deleted content to a exception marker (focus in marker)', () => {
  291. setModelData( model, '<paragraph>[]foofoo bar baz</paragraph>' );
  292. const firstParagraph = model.document.getRoot().getChild( 0 );
  293. addExceptionMarker( 3, 9, firstParagraph );
  294. model.change( writer => {
  295. const selection = writer.createSelection( writer.createRange(
  296. writer.createPositionAt( firstParagraph, 0 ),
  297. writer.createPositionAt( firstParagraph, 6 )
  298. ) );
  299. model.deleteContent( selection );
  300. } );
  301. assertEqualMarkup( getModelData( model ), '<paragraph>[]foo bar baz</paragraph>' );
  302. } );
  303. it( 'should trim deleted content to a exception marker (anchor in marker)', () => {
  304. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  305. const firstParagraph = model.document.getRoot().getChild( 0 );
  306. addExceptionMarker( 4, 7, firstParagraph );
  307. model.change( writer => {
  308. const selection = writer.createSelection( writer.createRange(
  309. writer.createPositionAt( firstParagraph, 5 ),
  310. writer.createPositionAt( firstParagraph, 8 )
  311. ) );
  312. model.deleteContent( selection );
  313. } );
  314. assertEqualMarkup( getModelData( model ), '<paragraph>[]foo b baz</paragraph>' );
  315. } );
  316. it( 'should trim deleted content to a exception marker and alter the selection argument (delete command integration)', () => {
  317. setModelData( model, '<paragraph>[]foofoo bar baz</paragraph>' );
  318. const firstParagraph = model.document.getRoot().getChild( 0 );
  319. addExceptionMarker( 3, 9, firstParagraph );
  320. model.change( writer => {
  321. writer.setSelection( firstParagraph, 6 );
  322. } );
  323. editor.execute( 'delete', { unit: 'word' } );
  324. assertEqualMarkup( getModelData( model ), '<paragraph>foo[] bar baz</paragraph>' );
  325. } );
  326. } );
  327. describe( 'clipboard', () => {
  328. let model, viewDoc;
  329. beforeEach( async () => {
  330. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, Clipboard, RestrictedEditingModeEditing ] } );
  331. model = editor.model;
  332. viewDoc = editor.editing.view.document;
  333. } );
  334. afterEach( () => {
  335. return editor.destroy();
  336. } );
  337. describe( 'cut', () => {
  338. it( 'should be blocked outside exception markers', () => {
  339. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  340. const spy = sinon.spy();
  341. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  342. viewDoc.fire( 'clipboardOutput', {
  343. content: {
  344. isEmpty: true
  345. },
  346. method: 'cut'
  347. } );
  348. sinon.assert.notCalled( spy );
  349. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  350. } );
  351. it( 'should be blocked inside exception marker', () => {
  352. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  353. const firstParagraph = model.document.getRoot().getChild( 0 );
  354. const spy = sinon.spy();
  355. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  356. model.change( writer => {
  357. writer.addMarker( 'restrictedEditingException:1', {
  358. range: writer.createRange(
  359. writer.createPositionAt( firstParagraph, 4 ),
  360. writer.createPositionAt( firstParagraph, 7 )
  361. ),
  362. usingOperation: true,
  363. affectsData: true
  364. } );
  365. } );
  366. model.change( writer => {
  367. writer.setSelection( firstParagraph, 5 );
  368. } );
  369. viewDoc.fire( 'clipboardOutput', {
  370. content: {
  371. isEmpty: true
  372. },
  373. method: 'cut'
  374. } );
  375. sinon.assert.notCalled( spy );
  376. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  377. } );
  378. } );
  379. describe( 'copy', () => {
  380. it( 'should not be blocked outside exception markers', () => {
  381. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  382. const spy = sinon.spy();
  383. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  384. viewDoc.fire( 'clipboardOutput', {
  385. content: {
  386. isEmpty: true
  387. },
  388. method: 'copy'
  389. } );
  390. sinon.assert.calledOnce( spy );
  391. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  392. } );
  393. it( 'should not be blocked inside exception marker', () => {
  394. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  395. const firstParagraph = model.document.getRoot().getChild( 0 );
  396. const spy = sinon.spy();
  397. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  398. model.change( writer => {
  399. writer.addMarker( 'restrictedEditingException:1', {
  400. range: writer.createRange(
  401. writer.createPositionAt( firstParagraph, 4 ),
  402. writer.createPositionAt( firstParagraph, 7 )
  403. ),
  404. usingOperation: true,
  405. affectsData: true
  406. } );
  407. } );
  408. model.change( writer => {
  409. writer.setSelection( firstParagraph, 5 );
  410. } );
  411. viewDoc.fire( 'clipboardOutput', {
  412. content: {
  413. isEmpty: true
  414. },
  415. method: 'copy'
  416. } );
  417. sinon.assert.calledOnce( spy );
  418. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  419. } );
  420. } );
  421. describe( 'paste', () => {
  422. it( 'should be blocked outside exception markers', () => {
  423. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  424. const spy = sinon.spy();
  425. viewDoc.on( 'clipboardInput', spy, { priority: 'high' } );
  426. viewDoc.fire( 'clipboardInput', {
  427. dataTransfer: {
  428. getData: sinon.spy()
  429. }
  430. } );
  431. sinon.assert.notCalled( spy );
  432. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  433. } );
  434. it( 'should be blocked inside exception marker', () => {
  435. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  436. const firstParagraph = model.document.getRoot().getChild( 0 );
  437. const spy = sinon.spy();
  438. viewDoc.on( 'clipboardInput', spy, { priority: 'high' } );
  439. model.change( writer => {
  440. writer.addMarker( 'restrictedEditingException:1', {
  441. range: writer.createRange(
  442. writer.createPositionAt( firstParagraph, 4 ),
  443. writer.createPositionAt( firstParagraph, 7 )
  444. ),
  445. usingOperation: true,
  446. affectsData: true
  447. } );
  448. } );
  449. model.change( writer => {
  450. writer.setSelection( firstParagraph, 5 );
  451. } );
  452. viewDoc.fire( 'clipboardInput', {
  453. dataTransfer: {
  454. getData: sinon.spy()
  455. }
  456. } );
  457. sinon.assert.notCalled( spy );
  458. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  459. } );
  460. } );
  461. } );
  462. describe( 'exception highlighting', () => {
  463. let model, view;
  464. beforeEach( async () => {
  465. editor = await VirtualTestEditor.create( {
  466. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  467. } );
  468. model = editor.model;
  469. view = editor.editing.view;
  470. } );
  471. afterEach( () => {
  472. return editor.destroy();
  473. } );
  474. it( 'should convert the highlight to a proper view classes', () => {
  475. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  476. const paragraph = model.document.getRoot().getChild( 0 );
  477. // <paragraph>foo <$marker>b[a]r</$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 ck-restricted-editing-exception_selected">b{a}r</span> baz</p>'
  487. );
  488. } );
  489. it( 'should remove classes when selection is moved away from an exception', () => {
  490. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  491. const paragraph = model.document.getRoot().getChild( 0 );
  492. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  493. model.change( writer => {
  494. writer.addMarker( 'restrictedEditingException:1', {
  495. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  496. usingOperation: true,
  497. affectsData: true
  498. } );
  499. } );
  500. expect( getViewData( view ) ).to.equal(
  501. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{a}r</span> baz</p>'
  502. );
  503. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  504. expect( getViewData( view ) ).to.equal(
  505. '<p>{}foo <span class="ck-restricted-editing-exception">bar</span> baz</p>'
  506. );
  507. } );
  508. it( 'should work correctly when selection is moved inside an exception', () => {
  509. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  510. const paragraph = model.document.getRoot().getChild( 0 );
  511. // <paragraph>[]foo <$marker>bar</$marker> baz</paragraph>
  512. model.change( writer => {
  513. writer.addMarker( 'restrictedEditingException:1', {
  514. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  515. usingOperation: true,
  516. affectsData: true
  517. } );
  518. } );
  519. expect( getViewData( view ) ).to.equal(
  520. '<p>{}foo <span class="ck-restricted-editing-exception">bar</span> baz</p>'
  521. );
  522. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 6 ) );
  523. expect( getViewData( view ) ).to.equal(
  524. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">ba{}r</span> baz</p>'
  525. );
  526. } );
  527. describe( 'editing downcast conversion integration', () => {
  528. it( 'works for the #insert event', () => {
  529. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  530. const paragraph = model.document.getRoot().getChild( 0 );
  531. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  532. model.change( writer => {
  533. writer.addMarker( 'restrictedEditingException:1', {
  534. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  535. usingOperation: true,
  536. affectsData: true
  537. } );
  538. } );
  539. model.change( writer => {
  540. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  541. } );
  542. expect( getViewData( view ) ).to.equal(
  543. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">bFOO{a}r</span> baz</p>'
  544. );
  545. } );
  546. it( 'works for the #remove event', () => {
  547. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  548. const paragraph = model.document.getRoot().getChild( 0 );
  549. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  550. model.change( writer => {
  551. writer.addMarker( 'restrictedEditingException:1', {
  552. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  553. usingOperation: true,
  554. affectsData: true
  555. } );
  556. } );
  557. model.change( writer => {
  558. writer.remove( writer.createRange(
  559. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 ),
  560. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 6 )
  561. ) );
  562. } );
  563. expect( getViewData( view ) ).to.equal(
  564. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{}r</span> baz</p>'
  565. );
  566. } );
  567. it( 'works for the #attribute event', () => {
  568. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  569. const paragraph = model.document.getRoot().getChild( 0 );
  570. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  571. model.change( writer => {
  572. writer.addMarker( 'restrictedEditingException:1', {
  573. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  574. usingOperation: true,
  575. affectsData: true
  576. } );
  577. } );
  578. model.change( writer => {
  579. writer.setAttribute( 'bold', true, writer.createRange(
  580. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  581. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  582. );
  583. } );
  584. expect( getViewData( view ) ).to.equal(
  585. '<p>foo ' +
  586. '<span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">' +
  587. '<strong>b{a</strong>' +
  588. '}r</span>' +
  589. ' baz</p>'
  590. );
  591. } );
  592. it( 'works for the #selection event', () => {
  593. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  594. const paragraph = model.document.getRoot().getChild( 0 );
  595. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  596. model.change( writer => {
  597. writer.addMarker( 'restrictedEditingException:1', {
  598. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  599. usingOperation: true,
  600. affectsData: true
  601. } );
  602. } );
  603. model.change( writer => {
  604. writer.setSelection( writer.createRange(
  605. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  606. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  607. );
  608. } );
  609. expect( getViewData( view ) ).to.equal(
  610. '<p>foo {<span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">ba}r</span> baz</p>'
  611. );
  612. } );
  613. it( 'works for the addMarker and removeMarker events', () => {
  614. editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
  615. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  616. const paragraph = model.document.getRoot().getChild( 0 );
  617. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  618. model.change( writer => {
  619. writer.addMarker( 'restrictedEditingException:1', {
  620. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  621. usingOperation: true,
  622. affectsData: true
  623. } );
  624. } );
  625. model.change( writer => {
  626. const range = writer.createRange(
  627. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  628. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  629. );
  630. writer.addMarker( 'fooMarker', { range, usingOperation: true } );
  631. } );
  632. expect( getViewData( view ) ).to.equal(
  633. '<p>' +
  634. '<span>foo </span>' +
  635. '<span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">' +
  636. '<span>b</span>{a}r' +
  637. '</span>' +
  638. ' baz</p>'
  639. );
  640. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  641. expect( getViewData( view ) ).to.equal(
  642. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{a}r</span> baz</p>'
  643. );
  644. } );
  645. } );
  646. } );
  647. describe( 'exception cycling with the keyboard', () => {
  648. let model, view, domEvtDataStub;
  649. beforeEach( async () => {
  650. editor = await VirtualTestEditor.create( {
  651. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  652. } );
  653. model = editor.model;
  654. view = editor.editing.view;
  655. domEvtDataStub = {
  656. keyCode: getCode( 'Tab' ),
  657. preventDefault: sinon.spy(),
  658. stopPropagation: sinon.spy()
  659. };
  660. sinon.spy( editor, 'execute' );
  661. } );
  662. afterEach( () => {
  663. return editor.destroy();
  664. } );
  665. it( 'should move to the closest next exception on tab key', () => {
  666. setModelData( model, '<paragraph>[]foo bar baz qux</paragraph>' );
  667. const paragraph = model.document.getRoot().getChild( 0 );
  668. // <paragraph>[]foo <marker>bar</marker> baz qux</paragraph>
  669. model.change( writer => {
  670. writer.addMarker( 'restrictedEditingException:1', {
  671. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  672. usingOperation: true,
  673. affectsData: true
  674. } );
  675. } );
  676. // <paragraph>[]foo <marker>bar</marker> <marker>baz</marker≥ qux</paragraph>
  677. model.change( writer => {
  678. writer.addMarker( 'restrictedEditingException:2', {
  679. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  680. usingOperation: true,
  681. affectsData: true
  682. } );
  683. } );
  684. view.document.fire( 'keydown', domEvtDataStub );
  685. sinon.assert.calledOnce( editor.execute );
  686. sinon.assert.calledWithExactly( editor.execute, 'goToNextRestrictedEditingException' );
  687. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  688. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  689. } );
  690. it( 'should not move to the closest next exception on tab key when there is none', () => {
  691. setModelData( model, '<paragraph>foo qux[]</paragraph>' );
  692. const paragraph = model.document.getRoot().getChild( 0 );
  693. // <paragraph><marker>foo</marker> qux[]</paragraph>
  694. model.change( writer => {
  695. writer.addMarker( 'restrictedEditingException:1', {
  696. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 3 ) ),
  697. usingOperation: true,
  698. affectsData: true
  699. } );
  700. } );
  701. view.document.fire( 'keydown', domEvtDataStub );
  702. sinon.assert.notCalled( editor.execute );
  703. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  704. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  705. } );
  706. it( 'should move to the closest previous exception on shift+tab key', () => {
  707. setModelData( model, '<paragraph>foo bar baz qux[]</paragraph>' );
  708. const paragraph = model.document.getRoot().getChild( 0 );
  709. // <paragraph>foo <marker>bar</marker> baz qux[]</paragraph>
  710. model.change( writer => {
  711. writer.addMarker( 'restrictedEditingException:1', {
  712. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  713. usingOperation: true,
  714. affectsData: true
  715. } );
  716. } );
  717. // <paragraph>foo <marker>bar</marker> <marker>baz</marker≥ qux[]</paragraph>
  718. model.change( writer => {
  719. writer.addMarker( 'restrictedEditingException:2', {
  720. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  721. usingOperation: true,
  722. affectsData: true
  723. } );
  724. } );
  725. domEvtDataStub.keyCode += getCode( 'Shift' );
  726. view.document.fire( 'keydown', domEvtDataStub );
  727. sinon.assert.calledOnce( editor.execute );
  728. sinon.assert.calledWithExactly( editor.execute, 'goToPreviousRestrictedEditingException' );
  729. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  730. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  731. } );
  732. it( 'should not move to the closest previous exception on shift+tab key when there is none', () => {
  733. setModelData( model, '<paragraph>[]foo qux</paragraph>' );
  734. const paragraph = model.document.getRoot().getChild( 0 );
  735. // <paragraph>[]foo <marker>qux</marker></paragraph>
  736. model.change( writer => {
  737. writer.addMarker( 'restrictedEditingException:1', {
  738. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  739. usingOperation: true,
  740. affectsData: true
  741. } );
  742. } );
  743. domEvtDataStub.keyCode += getCode( 'Shift' );
  744. view.document.fire( 'keydown', domEvtDataStub );
  745. sinon.assert.notCalled( editor.execute );
  746. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  747. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  748. } );
  749. } );
  750. // Helper method that creates an exception marker inside given parent.
  751. // Marker range is set to given position offsets (start, end).
  752. function addExceptionMarker( startOffset, endOffset = startOffset, parent, id = 1 ) {
  753. model.change( writer => {
  754. writer.addMarker( `restrictedEditingException:${ id }`, {
  755. range: writer.createRange(
  756. writer.createPositionAt( parent, startOffset ),
  757. writer.createPositionAt( parent, endOffset )
  758. ),
  759. usingOperation: true,
  760. affectsData: true
  761. } );
  762. } );
  763. }
  764. } );