8
0

restrictededitingmodeediting.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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( 'clipboard', () => {
  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. describe( 'cut', () => {
  283. it( 'should be blocked outside exception markers', () => {
  284. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  285. const spy = sinon.spy();
  286. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  287. viewDoc.fire( 'clipboardOutput', {
  288. content: {
  289. isEmpty: true
  290. },
  291. method: 'cut'
  292. } );
  293. sinon.assert.notCalled( spy );
  294. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  295. } );
  296. it( 'should be blocked inside exception marker', () => {
  297. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  298. const firstParagraph = model.document.getRoot().getChild( 0 );
  299. const spy = sinon.spy();
  300. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  301. model.change( writer => {
  302. writer.addMarker( 'restricted-editing-exception:1', {
  303. range: writer.createRange(
  304. writer.createPositionAt( firstParagraph, 4 ),
  305. writer.createPositionAt( firstParagraph, 7 )
  306. ),
  307. usingOperation: true,
  308. affectsData: true
  309. } );
  310. } );
  311. model.change( writer => {
  312. writer.setSelection( firstParagraph, 5 );
  313. } );
  314. viewDoc.fire( 'clipboardOutput', {
  315. content: {
  316. isEmpty: true
  317. },
  318. method: 'cut'
  319. } );
  320. sinon.assert.notCalled( spy );
  321. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  322. } );
  323. } );
  324. describe( 'copy', () => {
  325. it( 'should not be blocked outside exception markers', () => {
  326. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  327. const spy = sinon.spy();
  328. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  329. viewDoc.fire( 'clipboardOutput', {
  330. content: {
  331. isEmpty: true
  332. },
  333. method: 'copy'
  334. } );
  335. sinon.assert.calledOnce( spy );
  336. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  337. } );
  338. it( 'should not be blocked inside exception marker', () => {
  339. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  340. const firstParagraph = model.document.getRoot().getChild( 0 );
  341. const spy = sinon.spy();
  342. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  343. model.change( writer => {
  344. writer.addMarker( 'restricted-editing-exception:1', {
  345. range: writer.createRange(
  346. writer.createPositionAt( firstParagraph, 4 ),
  347. writer.createPositionAt( firstParagraph, 7 )
  348. ),
  349. usingOperation: true,
  350. affectsData: true
  351. } );
  352. } );
  353. model.change( writer => {
  354. writer.setSelection( firstParagraph, 5 );
  355. } );
  356. viewDoc.fire( 'clipboardOutput', {
  357. content: {
  358. isEmpty: true
  359. },
  360. method: 'copy'
  361. } );
  362. sinon.assert.calledOnce( spy );
  363. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  364. } );
  365. } );
  366. describe( 'paste', () => {
  367. it( 'should be blocked outside exception markers', () => {
  368. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  369. const spy = sinon.spy();
  370. viewDoc.on( 'clipboardInput', spy, { priority: 'high' } );
  371. viewDoc.fire( 'clipboardInput', {
  372. dataTransfer: {
  373. getData: sinon.spy()
  374. }
  375. } );
  376. sinon.assert.notCalled( spy );
  377. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  378. } );
  379. it( 'should be blocked inside exception marker', () => {
  380. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  381. const firstParagraph = model.document.getRoot().getChild( 0 );
  382. const spy = sinon.spy();
  383. viewDoc.on( 'clipboardInput', spy, { priority: 'high' } );
  384. model.change( writer => {
  385. writer.addMarker( 'restricted-editing-exception:1', {
  386. range: writer.createRange(
  387. writer.createPositionAt( firstParagraph, 4 ),
  388. writer.createPositionAt( firstParagraph, 7 )
  389. ),
  390. usingOperation: true,
  391. affectsData: true
  392. } );
  393. } );
  394. model.change( writer => {
  395. writer.setSelection( firstParagraph, 5 );
  396. } );
  397. viewDoc.fire( 'clipboardInput', {
  398. dataTransfer: {
  399. getData: sinon.spy()
  400. }
  401. } );
  402. sinon.assert.notCalled( spy );
  403. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  404. } );
  405. } );
  406. } );
  407. describe( 'exception highlighting', () => {
  408. let model, view;
  409. beforeEach( async () => {
  410. editor = await VirtualTestEditor.create( {
  411. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  412. } );
  413. model = editor.model;
  414. view = editor.editing.view;
  415. } );
  416. afterEach( () => {
  417. return editor.destroy();
  418. } );
  419. it( 'should convert the highlight to a proper view classes', () => {
  420. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  421. const paragraph = model.document.getRoot().getChild( 0 );
  422. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  423. model.change( writer => {
  424. writer.addMarker( 'restricted-editing-exception:1', {
  425. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  426. usingOperation: true,
  427. affectsData: true
  428. } );
  429. } );
  430. expect( getViewData( view ) ).to.equal(
  431. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{a}r</span> baz</p>'
  432. );
  433. } );
  434. it( 'should remove classes when selection is moved away from an exception', () => {
  435. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  436. const paragraph = model.document.getRoot().getChild( 0 );
  437. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  438. model.change( writer => {
  439. writer.addMarker( 'restricted-editing-exception:1', {
  440. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  441. usingOperation: true,
  442. affectsData: true
  443. } );
  444. } );
  445. expect( getViewData( view ) ).to.equal(
  446. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{a}r</span> baz</p>'
  447. );
  448. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  449. expect( getViewData( view ) ).to.equal(
  450. '<p>{}foo <span class="ck-restricted-editing-exception">bar</span> baz</p>'
  451. );
  452. } );
  453. it( 'should work correctly when selection is moved inside an exception', () => {
  454. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  455. const paragraph = model.document.getRoot().getChild( 0 );
  456. // <paragraph>[]foo <$marker>bar</$marker> baz</paragraph>
  457. model.change( writer => {
  458. writer.addMarker( 'restricted-editing-exception:1', {
  459. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  460. usingOperation: true,
  461. affectsData: true
  462. } );
  463. } );
  464. expect( getViewData( view ) ).to.equal(
  465. '<p>{}foo <span class="ck-restricted-editing-exception">bar</span> baz</p>'
  466. );
  467. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 6 ) );
  468. expect( getViewData( view ) ).to.equal(
  469. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">ba{}r</span> baz</p>'
  470. );
  471. } );
  472. describe( 'editing downcast conversion integration', () => {
  473. it( 'works for the #insert event', () => {
  474. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  475. const paragraph = model.document.getRoot().getChild( 0 );
  476. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  477. model.change( writer => {
  478. writer.addMarker( 'restricted-editing-exception:1', {
  479. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  480. usingOperation: true,
  481. affectsData: true
  482. } );
  483. } );
  484. model.change( writer => {
  485. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  486. } );
  487. expect( getViewData( view ) ).to.equal(
  488. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">bFOO{a}r</span> baz</p>'
  489. );
  490. } );
  491. it( 'works for the #remove event', () => {
  492. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  493. const paragraph = model.document.getRoot().getChild( 0 );
  494. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  495. model.change( writer => {
  496. writer.addMarker( 'restricted-editing-exception:1', {
  497. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  498. usingOperation: true,
  499. affectsData: true
  500. } );
  501. } );
  502. model.change( writer => {
  503. writer.remove( writer.createRange(
  504. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 ),
  505. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 6 )
  506. ) );
  507. } );
  508. expect( getViewData( view ) ).to.equal(
  509. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{}r</span> baz</p>'
  510. );
  511. } );
  512. it( 'works for the #attribute 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( 'restricted-editing-exception: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.setAttribute( 'bold', true, writer.createRange(
  525. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  526. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  527. );
  528. } );
  529. expect( getViewData( view ) ).to.equal(
  530. '<p>foo ' +
  531. '<span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">' +
  532. '<strong>b{a</strong>' +
  533. '}r</span>' +
  534. ' baz</p>'
  535. );
  536. } );
  537. it( 'works for the #selection event', () => {
  538. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  539. const paragraph = model.document.getRoot().getChild( 0 );
  540. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  541. model.change( writer => {
  542. writer.addMarker( 'restricted-editing-exception:1', {
  543. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  544. usingOperation: true,
  545. affectsData: true
  546. } );
  547. } );
  548. model.change( writer => {
  549. writer.setSelection( writer.createRange(
  550. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  551. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  552. );
  553. } );
  554. expect( getViewData( view ) ).to.equal(
  555. '<p>foo {<span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">ba}r</span> baz</p>'
  556. );
  557. } );
  558. it( 'works for the addMarker and removeMarker events', () => {
  559. editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
  560. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  561. const paragraph = model.document.getRoot().getChild( 0 );
  562. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  563. model.change( writer => {
  564. writer.addMarker( 'restricted-editing-exception:1', {
  565. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  566. usingOperation: true,
  567. affectsData: true
  568. } );
  569. } );
  570. model.change( writer => {
  571. const range = writer.createRange(
  572. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  573. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  574. );
  575. writer.addMarker( 'fooMarker', { range, usingOperation: true } );
  576. } );
  577. expect( getViewData( view ) ).to.equal(
  578. '<p>' +
  579. '<span>foo </span>' +
  580. '<span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">' +
  581. '<span>b</span>{a}r' +
  582. '</span>' +
  583. ' baz</p>'
  584. );
  585. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  586. expect( getViewData( view ) ).to.equal(
  587. '<p>foo <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">b{a}r</span> baz</p>'
  588. );
  589. } );
  590. } );
  591. } );
  592. describe( 'exception cycling with the keyboard', () => {
  593. let model, view, domEvtDataStub;
  594. beforeEach( async () => {
  595. editor = await VirtualTestEditor.create( {
  596. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  597. } );
  598. model = editor.model;
  599. view = editor.editing.view;
  600. domEvtDataStub = {
  601. keyCode: getCode( 'Tab' ),
  602. preventDefault: sinon.spy(),
  603. stopPropagation: sinon.spy()
  604. };
  605. sinon.spy( editor, 'execute' );
  606. } );
  607. afterEach( () => {
  608. return editor.destroy();
  609. } );
  610. it( 'should move to the closest next exception on tab key', () => {
  611. setModelData( model, '<paragraph>[]foo bar baz qux</paragraph>' );
  612. const paragraph = model.document.getRoot().getChild( 0 );
  613. // <paragraph>[]foo <marker>bar</marker> baz qux</paragraph>
  614. model.change( writer => {
  615. writer.addMarker( 'restricted-editing-exception:1', {
  616. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  617. usingOperation: true,
  618. affectsData: true
  619. } );
  620. } );
  621. // <paragraph>[]foo <marker>bar</marker> <marker>baz</marker≥ qux</paragraph>
  622. model.change( writer => {
  623. writer.addMarker( 'restricted-editing-exception:2', {
  624. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  625. usingOperation: true,
  626. affectsData: true
  627. } );
  628. } );
  629. view.document.fire( 'keydown', domEvtDataStub );
  630. sinon.assert.calledOnce( editor.execute );
  631. sinon.assert.calledWithExactly( editor.execute, 'goToNextRestrictedEditingRegion' );
  632. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  633. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  634. } );
  635. it( 'should not move to the closest next exception on tab key when there is none', () => {
  636. setModelData( model, '<paragraph>foo qux[]</paragraph>' );
  637. const paragraph = model.document.getRoot().getChild( 0 );
  638. // <paragraph><marker>foo</marker> qux[]</paragraph>
  639. model.change( writer => {
  640. writer.addMarker( 'restricted-editing-exception:1', {
  641. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 3 ) ),
  642. usingOperation: true,
  643. affectsData: true
  644. } );
  645. } );
  646. view.document.fire( 'keydown', domEvtDataStub );
  647. sinon.assert.notCalled( editor.execute );
  648. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  649. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  650. } );
  651. it( 'should move to the closest previous exception on shift+tab key', () => {
  652. setModelData( model, '<paragraph>foo bar baz qux[]</paragraph>' );
  653. const paragraph = model.document.getRoot().getChild( 0 );
  654. // <paragraph>foo <marker>bar</marker> baz qux[]</paragraph>
  655. model.change( writer => {
  656. writer.addMarker( 'restricted-editing-exception:1', {
  657. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  658. usingOperation: true,
  659. affectsData: true
  660. } );
  661. } );
  662. // <paragraph>foo <marker>bar</marker> <marker>baz</marker≥ qux[]</paragraph>
  663. model.change( writer => {
  664. writer.addMarker( 'restricted-editing-exception:2', {
  665. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  666. usingOperation: true,
  667. affectsData: true
  668. } );
  669. } );
  670. domEvtDataStub.keyCode += getCode( 'Shift' );
  671. view.document.fire( 'keydown', domEvtDataStub );
  672. sinon.assert.calledOnce( editor.execute );
  673. sinon.assert.calledWithExactly( editor.execute, 'goToPreviousRestrictedEditingRegion' );
  674. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  675. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  676. } );
  677. it( 'should not move to the closest previous exception on shift+tab key when there is none', () => {
  678. setModelData( model, '<paragraph>[]foo qux</paragraph>' );
  679. const paragraph = model.document.getRoot().getChild( 0 );
  680. // <paragraph>[]foo <marker>qux</marker></paragraph>
  681. model.change( writer => {
  682. writer.addMarker( 'restricted-editing-exception:1', {
  683. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  684. usingOperation: true,
  685. affectsData: true
  686. } );
  687. } );
  688. domEvtDataStub.keyCode += getCode( 'Shift' );
  689. view.document.fire( 'keydown', domEvtDataStub );
  690. sinon.assert.notCalled( editor.execute );
  691. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  692. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  693. } );
  694. } );
  695. } );