8
0

restrictededitingmodeediting.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  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( 'root should have "ck-restricted-editing_mode_restricted" class', () => {
  34. for ( const root of editor.editing.view.document.roots ) {
  35. expect( root.hasClass( 'ck-restricted-editing_mode_restricted' ) ).to.be.true;
  36. }
  37. } );
  38. it( 'adds a "goToPreviousRestrictedEditingException" command', () => {
  39. expect( editor.commands.get( 'goToPreviousRestrictedEditingException' ) )
  40. .to.be.instanceOf( RestrictedEditingModeNavigationCommand );
  41. } );
  42. it( 'adds a "goToNextRestrictedEditingException" command', () => {
  43. expect(
  44. editor.commands.get( 'goToNextRestrictedEditingException' )
  45. ).to.be.instanceOf( RestrictedEditingModeNavigationCommand );
  46. } );
  47. } );
  48. describe( 'conversion', () => {
  49. beforeEach( async () => {
  50. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, RestrictedEditingModeEditing ] } );
  51. model = editor.model;
  52. } );
  53. afterEach( () => {
  54. return editor.destroy();
  55. } );
  56. describe( 'upcast', () => {
  57. it( 'should convert <span class="restricted-editing-exception"> to marker', () => {
  58. editor.setData( '<p>foo <span class="restricted-editing-exception">bar</span> baz</p>' );
  59. expect( model.markers.has( 'restrictedEditingException:1' ) ).to.be.true;
  60. assertMarkerRangePaths( [ 0, 4 ], [ 0, 7 ] );
  61. } );
  62. it( 'should convert multiple <span class="restricted-editing-exception">', () => {
  63. editor.setData(
  64. '<p>foo <span class="restricted-editing-exception">bar</span> baz</p>' +
  65. '<p>ABCDEF<span class="restricted-editing-exception">GHIJK</span>LMNOPQRST</p>'
  66. );
  67. expect( model.markers.has( 'restrictedEditingException:1' ) ).to.be.true;
  68. expect( model.markers.has( 'restrictedEditingException:2' ) ).to.be.true;
  69. // Data for the first marker is the same as in previous tests so no need to test it again.
  70. assertMarkerRangePaths( [ 1, 6 ], [ 1, 11 ], 2 );
  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="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="restricted-editing-exception"></span>bar baz</p>' );
  103. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  104. '<p>foo <span class="restricted-editing-exception 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="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="restricted-editing-exception restricted-editing-exception_selected"><b>foo bar baz</b></span>' +
  124. '</p>'
  125. );
  126. } );
  127. } );
  128. } );
  129. describe( 'editing behavior', () => {
  130. beforeEach( async () => {
  131. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingModeEditing ] } );
  132. model = editor.model;
  133. } );
  134. afterEach( () => {
  135. return editor.destroy();
  136. } );
  137. it( 'should keep markers in the view when editable region is edited', () => {
  138. setModelData( model,
  139. '<paragraph>foo bar baz</paragraph>' +
  140. '<paragraph>xxx y[]yy zzz</paragraph>'
  141. );
  142. const firstParagraph = model.document.getRoot().getChild( 0 );
  143. const secondParagraph = model.document.getRoot().getChild( 1 );
  144. model.change( writer => {
  145. writer.addMarker( 'restrictedEditingException:1', {
  146. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  147. usingOperation: true,
  148. affectsData: true
  149. } );
  150. writer.addMarker( 'restrictedEditingException:2', {
  151. range: writer.createRange(
  152. writer.createPositionAt( secondParagraph, 4 ),
  153. writer.createPositionAt( secondParagraph, 7 )
  154. ),
  155. usingOperation: true,
  156. affectsData: true
  157. } );
  158. } );
  159. model.change( writer => {
  160. model.insertContent( writer.createText( 'R', model.document.selection.getAttributes() ) );
  161. } );
  162. expect( editor.getData() ).to.equal(
  163. '<p>foo <span class="restricted-editing-exception">bar</span> baz</p>' +
  164. '<p>xxx <span class="restricted-editing-exception">yRyy</span> zzz</p>' );
  165. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  166. '<p>foo <span class="restricted-editing-exception">bar</span> baz</p>' +
  167. '<p>xxx <span class="restricted-editing-exception restricted-editing-exception_selected">yRyy</span> zzz</p>' );
  168. } );
  169. it( 'should block user typing outside exception markers', () => {
  170. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  171. editor.execute( 'input', { text: 'X' } );
  172. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  173. } );
  174. it( 'should not block user typing inside exception marker', () => {
  175. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  176. const firstParagraph = model.document.getRoot().getChild( 0 );
  177. model.change( writer => {
  178. writer.addMarker( 'restrictedEditingException:1', {
  179. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  180. usingOperation: true,
  181. affectsData: true
  182. } );
  183. } );
  184. model.change( writer => {
  185. writer.setSelection( firstParagraph, 5 );
  186. } );
  187. editor.execute( 'input', { text: 'X' } );
  188. assertEqualMarkup( getModelData( model ), '<paragraph>foo bX[]ar baz</paragraph>' );
  189. } );
  190. it( 'should extend maker when typing on the marker boundary (end)', () => {
  191. setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
  192. const firstParagraph = model.document.getRoot().getChild( 0 );
  193. model.change( writer => {
  194. writer.addMarker( 'restrictedEditingException:1', {
  195. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  196. usingOperation: true,
  197. affectsData: true
  198. } );
  199. } );
  200. editor.execute( 'input', { text: 'X' } );
  201. assertEqualMarkup( getModelData( model ), '<paragraph>foo barX[] baz</paragraph>' );
  202. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  203. const expectedRange = model.createRange(
  204. model.createPositionAt( firstParagraph, 4 ),
  205. model.createPositionAt( firstParagraph, 8 )
  206. );
  207. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  208. } );
  209. it( 'should extend marker when typing on the marker boundary (start)', () => {
  210. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  211. const firstParagraph = model.document.getRoot().getChild( 0 );
  212. model.change( writer => {
  213. writer.addMarker( 'restrictedEditingException:1', {
  214. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  215. usingOperation: true,
  216. affectsData: true
  217. } );
  218. } );
  219. editor.execute( 'input', { text: 'X' } );
  220. assertEqualMarkup( getModelData( model ), '<paragraph>foo X[]bar baz</paragraph>' );
  221. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  222. const expectedRange = model.createRange(
  223. model.createPositionAt( firstParagraph, 4 ),
  224. model.createPositionAt( firstParagraph, 8 )
  225. );
  226. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  227. } );
  228. it( 'should extend marker when typing on the marker boundary (collapsed marker)', () => {
  229. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  230. const firstParagraph = model.document.getRoot().getChild( 0 );
  231. model.change( writer => {
  232. writer.addMarker( 'restrictedEditingException:1', {
  233. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ) ),
  234. usingOperation: true,
  235. affectsData: true
  236. } );
  237. } );
  238. model.change( writer => {
  239. writer.setSelection( writer.createPositionAt( firstParagraph, 4 ) );
  240. } );
  241. editor.execute( 'input', { text: 'X' } );
  242. assertEqualMarkup( getModelData( model ), '<paragraph>foo X[]bar baz</paragraph>' );
  243. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  244. const expectedRange = model.createRange(
  245. model.createPositionAt( firstParagraph, 4 ),
  246. model.createPositionAt( firstParagraph, 5 )
  247. );
  248. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  249. } );
  250. it( 'should retain marker on non-typing change at the marker boundary (start)', () => {
  251. setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
  252. const firstParagraph = model.document.getRoot().getChild( 0 );
  253. addExceptionMarker( 4, 7, firstParagraph );
  254. model.change( writer => {
  255. editor.execute( 'delete', {
  256. selection: writer.createSelection( writer.createRange(
  257. writer.createPositionAt( firstParagraph, 4 ),
  258. writer.createPositionAt( firstParagraph, 6 )
  259. ) )
  260. } );
  261. editor.execute( 'input', {
  262. text: 'XX',
  263. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ) )
  264. } );
  265. } );
  266. assertEqualMarkup( getModelData( model ), '<paragraph>foo XX[]r baz</paragraph>' );
  267. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  268. const expectedRange = model.createRange(
  269. model.createPositionAt( firstParagraph, 4 ),
  270. model.createPositionAt( firstParagraph, 7 )
  271. );
  272. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  273. } );
  274. it( 'should retain marker on non-typing change at marker boundary (end)', () => {
  275. setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
  276. const firstParagraph = model.document.getRoot().getChild( 0 );
  277. addExceptionMarker( 4, 7, firstParagraph );
  278. model.change( writer => {
  279. editor.execute( 'delete', {
  280. selection: writer.createSelection( writer.createRange(
  281. writer.createPositionAt( firstParagraph, 5 ),
  282. writer.createPositionAt( firstParagraph, 7 )
  283. ) )
  284. } );
  285. editor.execute( 'input', {
  286. text: 'XX',
  287. range: writer.createRange( writer.createPositionAt( firstParagraph, 5 ) )
  288. } );
  289. } );
  290. assertEqualMarkup( getModelData( model ), '<paragraph>foo bXX[] baz</paragraph>' );
  291. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  292. const expectedRange = model.createRange(
  293. model.createPositionAt( firstParagraph, 4 ),
  294. model.createPositionAt( firstParagraph, 7 )
  295. );
  296. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  297. } );
  298. it( 'should not move collapsed marker to $graveyard', () => {
  299. setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
  300. const firstParagraph = model.document.getRoot().getChild( 0 );
  301. model.change( writer => {
  302. writer.addMarker( 'restrictedEditingException:1', {
  303. range: writer.createRange(
  304. writer.createPositionAt( firstParagraph, 4 ),
  305. writer.createPositionAt( firstParagraph, 5 )
  306. ),
  307. usingOperation: true,
  308. affectsData: true
  309. } );
  310. } );
  311. editor.execute( 'delete' );
  312. assertEqualMarkup( getModelData( model ), '<paragraph>foo []ar baz</paragraph>' );
  313. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  314. const expectedRange = model.createRange(
  315. model.createPositionAt( firstParagraph, 4 ),
  316. model.createPositionAt( firstParagraph, 4 )
  317. );
  318. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  319. } );
  320. } );
  321. describe( 'enforcing restrictions on deleteContent', () => {
  322. beforeEach( async () => {
  323. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingModeEditing ] } );
  324. model = editor.model;
  325. } );
  326. afterEach( async () => {
  327. await editor.destroy();
  328. } );
  329. it( 'should not allow to delete content outside restricted area', () => {
  330. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  331. const firstParagraph = model.document.getRoot().getChild( 0 );
  332. addExceptionMarker( 3, 9, firstParagraph );
  333. model.change( writer => {
  334. writer.setSelection( firstParagraph, 2 );
  335. } );
  336. model.deleteContent( model.document.selection );
  337. assertEqualMarkup( getModelData( model ), '<paragraph>fo[]o bar baz</paragraph>' );
  338. } );
  339. it( 'should trim deleted content to a exception marker (focus in marker)', () => {
  340. setModelData( model, '<paragraph>[]foofoo bar baz</paragraph>' );
  341. const firstParagraph = model.document.getRoot().getChild( 0 );
  342. addExceptionMarker( 3, 9, firstParagraph );
  343. model.change( writer => {
  344. const selection = writer.createSelection( writer.createRange(
  345. writer.createPositionAt( firstParagraph, 0 ),
  346. writer.createPositionAt( firstParagraph, 6 )
  347. ) );
  348. model.deleteContent( selection );
  349. } );
  350. assertEqualMarkup( getModelData( model ), '<paragraph>[]foo bar baz</paragraph>' );
  351. } );
  352. it( 'should trim deleted content to a exception marker (anchor in marker)', () => {
  353. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  354. const firstParagraph = model.document.getRoot().getChild( 0 );
  355. addExceptionMarker( 4, 7, firstParagraph );
  356. model.change( writer => {
  357. const selection = writer.createSelection( writer.createRange(
  358. writer.createPositionAt( firstParagraph, 5 ),
  359. writer.createPositionAt( firstParagraph, 8 )
  360. ) );
  361. model.deleteContent( selection );
  362. } );
  363. assertEqualMarkup( getModelData( model ), '<paragraph>[]foo b baz</paragraph>' );
  364. } );
  365. it( 'should trim deleted content to a exception marker and alter the selection argument (delete command integration)', () => {
  366. setModelData( model, '<paragraph>[]foofoo bar baz</paragraph>' );
  367. const firstParagraph = model.document.getRoot().getChild( 0 );
  368. addExceptionMarker( 3, 9, firstParagraph );
  369. model.change( writer => {
  370. writer.setSelection( firstParagraph, 6 );
  371. } );
  372. editor.execute( 'delete', { unit: 'word' } );
  373. assertEqualMarkup( getModelData( model ), '<paragraph>foo[] bar baz</paragraph>' );
  374. } );
  375. it( 'should work with document selection', () => {
  376. setModelData( model, '<paragraph>f[oo bar] baz</paragraph>' );
  377. const firstParagraph = model.document.getRoot().getChild( 0 );
  378. addExceptionMarker( 2, 'end', firstParagraph );
  379. model.change( () => {
  380. model.deleteContent( model.document.selection );
  381. } );
  382. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), '<paragraph>fo baz</paragraph>' );
  383. } );
  384. } );
  385. describe( 'enforcing restrictions on input command', () => {
  386. let firstParagraph;
  387. beforeEach( async () => {
  388. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingModeEditing ] } );
  389. model = editor.model;
  390. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  391. firstParagraph = model.document.getRoot().getChild( 0 );
  392. } );
  393. afterEach( async () => {
  394. await editor.destroy();
  395. } );
  396. it( 'should prevent changing text before exception marker', () => {
  397. addExceptionMarker( 4, 7, firstParagraph );
  398. model.change( writer => {
  399. writer.setSelection( firstParagraph, 5 );
  400. } );
  401. // Simulate native spell-check action.
  402. editor.execute( 'input', {
  403. text: 'xxxxxxx',
  404. range: model.createRange(
  405. model.createPositionAt( firstParagraph, 0 ),
  406. model.createPositionAt( firstParagraph, 7 )
  407. )
  408. } );
  409. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  410. } );
  411. it( 'should prevent changing text before exception marker', () => {
  412. addExceptionMarker( 4, 7, firstParagraph );
  413. model.change( writer => {
  414. writer.setSelection( firstParagraph, 5 );
  415. } );
  416. // Simulate native spell-check action.
  417. editor.execute( 'input', {
  418. text: 'xxxxxxx',
  419. range: model.createRange(
  420. model.createPositionAt( firstParagraph, 4 ),
  421. model.createPositionAt( firstParagraph, 9 )
  422. )
  423. } );
  424. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  425. } );
  426. it( 'should prevent changing text before (change crossing different markers)', () => {
  427. addExceptionMarker( 0, 4, firstParagraph );
  428. addExceptionMarker( 7, 9, firstParagraph, 2 );
  429. model.change( writer => {
  430. writer.setSelection( firstParagraph, 2 );
  431. } );
  432. // Simulate native spell-check action.
  433. editor.execute( 'input', {
  434. text: 'xxxxxxx',
  435. range: model.createRange(
  436. model.createPositionAt( firstParagraph, 2 ),
  437. model.createPositionAt( firstParagraph, 8 )
  438. )
  439. } );
  440. assertEqualMarkup( getModelData( model ), '<paragraph>fo[]o bar baz</paragraph>' );
  441. } );
  442. it( 'should allow changing text inside single marker', () => {
  443. addExceptionMarker( 0, 9, firstParagraph );
  444. model.change( writer => {
  445. writer.setSelection( firstParagraph, 2 );
  446. } );
  447. // Simulate native spell-check action.
  448. editor.execute( 'input', {
  449. text: 'xxxxxxx',
  450. range: model.createRange(
  451. model.createPositionAt( firstParagraph, 2 ),
  452. model.createPositionAt( firstParagraph, 8 )
  453. )
  454. } );
  455. assertEqualMarkup( getModelData( model ), '<paragraph>foxxxxxxx[]baz</paragraph>' );
  456. } );
  457. } );
  458. describe( 'clipboard', () => {
  459. let viewDoc;
  460. beforeEach( async () => {
  461. editor = await VirtualTestEditor.create( {
  462. plugins: [ Paragraph, BoldEditing, Typing, Clipboard, RestrictedEditingModeEditing ]
  463. } );
  464. model = editor.model;
  465. viewDoc = editor.editing.view.document;
  466. } );
  467. afterEach( async () => {
  468. await editor.destroy();
  469. } );
  470. describe( 'cut', () => {
  471. it( 'should be blocked outside exception markers', () => {
  472. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  473. const spy = sinon.spy();
  474. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  475. viewDoc.fire( 'clipboardOutput', {
  476. content: {
  477. isEmpty: true
  478. },
  479. method: 'cut'
  480. } );
  481. sinon.assert.notCalled( spy );
  482. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  483. } );
  484. it( 'should cut selected content inside exception marker (selection inside marker)', () => {
  485. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  486. const firstParagraph = model.document.getRoot().getChild( 0 );
  487. addExceptionMarker( 4, 7, firstParagraph );
  488. viewDoc.fire( 'clipboardOutput', {
  489. content: {
  490. isEmpty: true
  491. },
  492. method: 'cut'
  493. } );
  494. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]r baz</paragraph>' );
  495. } );
  496. it( 'should cut selected content inside exception marker (selection touching marker start)', () => {
  497. setModelData( model, '<paragraph>foo [ba]r baz</paragraph>' );
  498. const firstParagraph = model.document.getRoot().getChild( 0 );
  499. addExceptionMarker( 4, 7, firstParagraph );
  500. viewDoc.fire( 'clipboardOutput', {
  501. content: {
  502. isEmpty: true
  503. },
  504. method: 'cut'
  505. } );
  506. assertEqualMarkup( getModelData( model ), '<paragraph>foo []r baz</paragraph>' );
  507. } );
  508. it( 'should cut selected content inside exception marker (selection touching marker end)', () => {
  509. setModelData( model, '<paragraph>foo b[ar] baz</paragraph>' );
  510. const firstParagraph = model.document.getRoot().getChild( 0 );
  511. addExceptionMarker( 4, 7, firstParagraph );
  512. viewDoc.fire( 'clipboardOutput', {
  513. content: {
  514. isEmpty: true
  515. },
  516. method: 'cut'
  517. } );
  518. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[] baz</paragraph>' );
  519. } );
  520. } );
  521. describe( 'copy', () => {
  522. it( 'should not be blocked outside exception markers', () => {
  523. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  524. const spy = sinon.spy();
  525. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  526. viewDoc.fire( 'clipboardOutput', {
  527. content: {
  528. isEmpty: true
  529. },
  530. method: 'copy'
  531. } );
  532. sinon.assert.calledOnce( spy );
  533. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  534. } );
  535. it( 'should not be blocked inside exception marker', () => {
  536. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  537. const firstParagraph = model.document.getRoot().getChild( 0 );
  538. const spy = sinon.spy();
  539. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  540. model.change( writer => {
  541. writer.addMarker( 'restrictedEditingException:1', {
  542. range: writer.createRange(
  543. writer.createPositionAt( firstParagraph, 4 ),
  544. writer.createPositionAt( firstParagraph, 7 )
  545. ),
  546. usingOperation: true,
  547. affectsData: true
  548. } );
  549. } );
  550. model.change( writer => {
  551. writer.setSelection( firstParagraph, 5 );
  552. } );
  553. viewDoc.fire( 'clipboardOutput', {
  554. content: {
  555. isEmpty: true
  556. },
  557. method: 'copy'
  558. } );
  559. sinon.assert.calledOnce( spy );
  560. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  561. } );
  562. } );
  563. describe( 'paste', () => {
  564. beforeEach( () => {
  565. // Required when testing without DOM using VirtualTestEditor - Clipboard feature scrolls after paste event.
  566. sinon.stub( editor.editing.view, 'scrollToTheSelection' );
  567. } );
  568. it( 'should be blocked outside exception markers', () => {
  569. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  570. const spy = sinon.spy();
  571. viewDoc.on( 'clipboardInput', spy, { priority: 'high' } );
  572. viewDoc.fire( 'clipboardInput', {
  573. dataTransfer: {
  574. getData: sinon.spy()
  575. }
  576. } );
  577. sinon.assert.notCalled( spy );
  578. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  579. } );
  580. describe( 'collapsed selection', () => {
  581. it( 'should paste text inside exception marker', () => {
  582. setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
  583. const firstParagraph = model.document.getRoot().getChild( 0 );
  584. addExceptionMarker( 4, 7, firstParagraph );
  585. viewDoc.fire( 'clipboardInput', {
  586. dataTransfer: createDataTransfer( { 'text/html': '<p>XXX</p>', 'text/plain': 'XXX' } )
  587. } );
  588. assertEqualMarkup( getModelData( model ), '<paragraph>foo bXXX[]ar baz</paragraph>' );
  589. assertMarkerRangePaths( [ 0, 4 ], [ 0, 10 ] );
  590. } );
  591. it( 'should paste allowed text attributes inside exception marker', () => {
  592. setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
  593. const firstParagraph = model.document.getRoot().getChild( 0 );
  594. addExceptionMarker( 4, 7, firstParagraph );
  595. viewDoc.fire( 'clipboardInput', {
  596. dataTransfer: createDataTransfer( { 'text/html': '<p><b>XXX</b></p>', 'text/plain': 'XXX' } )
  597. } );
  598. assertEqualMarkup( getModelData( model ), '<paragraph>foo b<$text bold="true">XXX[]</$text>ar baz</paragraph>' );
  599. assertMarkerRangePaths( [ 0, 4 ], [ 0, 10 ] );
  600. } );
  601. } );
  602. describe( 'non-collapsed selection', () => {
  603. it( 'should paste text inside exception marker', () => {
  604. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  605. const firstParagraph = model.document.getRoot().getChild( 0 );
  606. addExceptionMarker( 4, 7, firstParagraph );
  607. viewDoc.fire( 'clipboardInput', {
  608. dataTransfer: createDataTransfer( { 'text/html': '<p>XXX</p>', 'text/plain': 'XXX' } )
  609. } );
  610. assertEqualMarkup( getModelData( model ), '<paragraph>foo bXXX[]r baz</paragraph>' );
  611. assertMarkerRangePaths( [ 0, 4 ], [ 0, 9 ] );
  612. } );
  613. it( 'should paste allowed text attributes inside exception marker', () => {
  614. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  615. const firstParagraph = model.document.getRoot().getChild( 0 );
  616. addExceptionMarker( 4, 7, firstParagraph );
  617. viewDoc.fire( 'clipboardInput', {
  618. dataTransfer: createDataTransfer( { 'text/html': '<p><b>XXX</b></p>', 'text/plain': 'XXX' } )
  619. } );
  620. assertEqualMarkup( getModelData( model ), '<paragraph>foo b<$text bold="true">XXX[]</$text>r baz</paragraph>' );
  621. assertMarkerRangePaths( [ 0, 4 ], [ 0, 9 ] );
  622. } );
  623. } );
  624. } );
  625. } );
  626. describe( 'exception highlighting', () => {
  627. let view;
  628. beforeEach( async () => {
  629. editor = await VirtualTestEditor.create( {
  630. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  631. } );
  632. model = editor.model;
  633. view = editor.editing.view;
  634. } );
  635. afterEach( () => {
  636. return editor.destroy();
  637. } );
  638. it( 'should convert the highlight to a proper view classes', () => {
  639. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  640. const paragraph = model.document.getRoot().getChild( 0 );
  641. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  642. model.change( writer => {
  643. writer.addMarker( 'restrictedEditingException:1', {
  644. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  645. usingOperation: true,
  646. affectsData: true
  647. } );
  648. } );
  649. expect( getViewData( view ) ).to.equal(
  650. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{a}r</span> baz</p>'
  651. );
  652. } );
  653. it( 'should remove classes when selection is moved away from an exception', () => {
  654. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  655. const paragraph = model.document.getRoot().getChild( 0 );
  656. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  657. model.change( writer => {
  658. writer.addMarker( 'restrictedEditingException:1', {
  659. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  660. usingOperation: true,
  661. affectsData: true
  662. } );
  663. } );
  664. expect( getViewData( view ) ).to.equal(
  665. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{a}r</span> baz</p>'
  666. );
  667. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  668. expect( getViewData( view ) ).to.equal(
  669. '<p>{}foo <span class="restricted-editing-exception">bar</span> baz</p>'
  670. );
  671. } );
  672. it( 'should work correctly when selection is moved inside an exception', () => {
  673. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  674. const paragraph = model.document.getRoot().getChild( 0 );
  675. // <paragraph>[]foo <$marker>bar</$marker> baz</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. expect( getViewData( view ) ).to.equal(
  684. '<p>{}foo <span class="restricted-editing-exception">bar</span> baz</p>'
  685. );
  686. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 6 ) );
  687. expect( getViewData( view ) ).to.equal(
  688. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">ba{}r</span> baz</p>'
  689. );
  690. } );
  691. describe( 'editing downcast conversion integration', () => {
  692. it( 'works for the #insert event', () => {
  693. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  694. const paragraph = model.document.getRoot().getChild( 0 );
  695. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  696. model.change( writer => {
  697. writer.addMarker( 'restrictedEditingException:1', {
  698. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  699. usingOperation: true,
  700. affectsData: true
  701. } );
  702. } );
  703. model.change( writer => {
  704. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  705. } );
  706. expect( getViewData( view ) ).to.equal(
  707. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">bFOO{a}r</span> baz</p>'
  708. );
  709. } );
  710. it( 'works for the #remove event', () => {
  711. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  712. const paragraph = model.document.getRoot().getChild( 0 );
  713. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  714. model.change( writer => {
  715. writer.addMarker( 'restrictedEditingException:1', {
  716. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  717. usingOperation: true,
  718. affectsData: true
  719. } );
  720. } );
  721. model.change( writer => {
  722. writer.remove( writer.createRange(
  723. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 ),
  724. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 6 )
  725. ) );
  726. } );
  727. expect( getViewData( view ) ).to.equal(
  728. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{}r</span> baz</p>'
  729. );
  730. } );
  731. it( 'works for the #attribute event', () => {
  732. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  733. const paragraph = model.document.getRoot().getChild( 0 );
  734. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  735. model.change( writer => {
  736. writer.addMarker( 'restrictedEditingException:1', {
  737. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  738. usingOperation: true,
  739. affectsData: true
  740. } );
  741. } );
  742. model.change( writer => {
  743. writer.setAttribute( 'bold', true, writer.createRange(
  744. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  745. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  746. );
  747. } );
  748. expect( getViewData( view ) ).to.equal(
  749. '<p>foo ' +
  750. '<span class="restricted-editing-exception restricted-editing-exception_selected">' +
  751. '<strong>b{a</strong>' +
  752. '}r</span>' +
  753. ' baz</p>'
  754. );
  755. } );
  756. it( 'works for the #selection event', () => {
  757. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  758. const paragraph = model.document.getRoot().getChild( 0 );
  759. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  760. model.change( writer => {
  761. writer.addMarker( 'restrictedEditingException:1', {
  762. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  763. usingOperation: true,
  764. affectsData: true
  765. } );
  766. } );
  767. model.change( writer => {
  768. writer.setSelection( writer.createRange(
  769. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  770. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  771. );
  772. } );
  773. expect( getViewData( view ) ).to.equal(
  774. '<p>foo {<span class="restricted-editing-exception restricted-editing-exception_selected">ba}r</span> baz</p>'
  775. );
  776. } );
  777. it( 'works for the addMarker and removeMarker events', () => {
  778. editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
  779. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  780. const paragraph = model.document.getRoot().getChild( 0 );
  781. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  782. model.change( writer => {
  783. writer.addMarker( 'restrictedEditingException:1', {
  784. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  785. usingOperation: true,
  786. affectsData: true
  787. } );
  788. } );
  789. model.change( writer => {
  790. const range = writer.createRange(
  791. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  792. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  793. );
  794. writer.addMarker( 'fooMarker', { range, usingOperation: true } );
  795. } );
  796. expect( getViewData( view ) ).to.equal(
  797. '<p>' +
  798. '<span>foo </span>' +
  799. '<span class="restricted-editing-exception restricted-editing-exception_selected">' +
  800. '<span>b</span>{a}r' +
  801. '</span>' +
  802. ' baz</p>'
  803. );
  804. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  805. expect( getViewData( view ) ).to.equal(
  806. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{a}r</span> baz</p>'
  807. );
  808. } );
  809. } );
  810. } );
  811. describe( 'exception cycling with the keyboard', () => {
  812. let view, domEvtDataStub;
  813. beforeEach( async () => {
  814. editor = await VirtualTestEditor.create( {
  815. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  816. } );
  817. model = editor.model;
  818. view = editor.editing.view;
  819. domEvtDataStub = {
  820. keyCode: getCode( 'Tab' ),
  821. preventDefault: sinon.spy(),
  822. stopPropagation: sinon.spy()
  823. };
  824. sinon.spy( editor, 'execute' );
  825. } );
  826. afterEach( () => {
  827. return editor.destroy();
  828. } );
  829. it( 'should move to the closest next exception on tab key', () => {
  830. setModelData( model, '<paragraph>[]foo bar baz qux</paragraph>' );
  831. const paragraph = model.document.getRoot().getChild( 0 );
  832. // <paragraph>[]foo <marker>bar</marker> baz qux</paragraph>
  833. model.change( writer => {
  834. writer.addMarker( 'restrictedEditingException:1', {
  835. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  836. usingOperation: true,
  837. affectsData: true
  838. } );
  839. } );
  840. // <paragraph>[]foo <marker>bar</marker> <marker>baz</marker≥ qux</paragraph>
  841. model.change( writer => {
  842. writer.addMarker( 'restrictedEditingException:2', {
  843. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  844. usingOperation: true,
  845. affectsData: true
  846. } );
  847. } );
  848. view.document.fire( 'keydown', domEvtDataStub );
  849. sinon.assert.calledOnce( editor.execute );
  850. sinon.assert.calledWithExactly( editor.execute, 'goToNextRestrictedEditingException' );
  851. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  852. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  853. } );
  854. it( 'should not move to the closest next exception on tab key when there is none', () => {
  855. setModelData( model, '<paragraph>foo qux[]</paragraph>' );
  856. const paragraph = model.document.getRoot().getChild( 0 );
  857. // <paragraph><marker>foo</marker> qux[]</paragraph>
  858. model.change( writer => {
  859. writer.addMarker( 'restrictedEditingException:1', {
  860. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 3 ) ),
  861. usingOperation: true,
  862. affectsData: true
  863. } );
  864. } );
  865. view.document.fire( 'keydown', domEvtDataStub );
  866. sinon.assert.notCalled( editor.execute );
  867. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  868. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  869. } );
  870. it( 'should move to the closest previous exception on shift+tab key', () => {
  871. setModelData( model, '<paragraph>foo bar baz qux[]</paragraph>' );
  872. const paragraph = model.document.getRoot().getChild( 0 );
  873. // <paragraph>foo <marker>bar</marker> baz qux[]</paragraph>
  874. model.change( writer => {
  875. writer.addMarker( 'restrictedEditingException:1', {
  876. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  877. usingOperation: true,
  878. affectsData: true
  879. } );
  880. } );
  881. // <paragraph>foo <marker>bar</marker> <marker>baz</marker≥ qux[]</paragraph>
  882. model.change( writer => {
  883. writer.addMarker( 'restrictedEditingException:2', {
  884. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  885. usingOperation: true,
  886. affectsData: true
  887. } );
  888. } );
  889. domEvtDataStub.keyCode += getCode( 'Shift' );
  890. view.document.fire( 'keydown', domEvtDataStub );
  891. sinon.assert.calledOnce( editor.execute );
  892. sinon.assert.calledWithExactly( editor.execute, 'goToPreviousRestrictedEditingException' );
  893. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  894. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  895. } );
  896. it( 'should not move to the closest previous exception on shift+tab key when there is none', () => {
  897. setModelData( model, '<paragraph>[]foo qux</paragraph>' );
  898. const paragraph = model.document.getRoot().getChild( 0 );
  899. // <paragraph>[]foo <marker>qux</marker></paragraph>
  900. model.change( writer => {
  901. writer.addMarker( 'restrictedEditingException:1', {
  902. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  903. usingOperation: true,
  904. affectsData: true
  905. } );
  906. } );
  907. domEvtDataStub.keyCode += getCode( 'Shift' );
  908. view.document.fire( 'keydown', domEvtDataStub );
  909. sinon.assert.notCalled( editor.execute );
  910. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  911. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  912. } );
  913. } );
  914. // Helper method that creates an exception marker inside given parent.
  915. // Marker range is set to given position offsets (start, end).
  916. function addExceptionMarker( startOffset, endOffset = startOffset, parent, id = 1 ) {
  917. model.change( writer => {
  918. writer.addMarker( `restrictedEditingException:${ id }`, {
  919. range: writer.createRange(
  920. writer.createPositionAt( parent, startOffset ),
  921. writer.createPositionAt( parent, endOffset )
  922. ),
  923. usingOperation: true,
  924. affectsData: true
  925. } );
  926. } );
  927. }
  928. function createDataTransfer( data ) {
  929. return {
  930. getData( type ) {
  931. return data[ type ];
  932. }
  933. };
  934. }
  935. function assertMarkerRangePaths( startPath, endPath, markerId = 1 ) {
  936. const marker = model.markers.get( `restrictedEditingException:${ markerId }` );
  937. expect( marker.getStart().path ).to.deep.equal( startPath );
  938. expect( marker.getEnd().path ).to.deep.equal( endPath );
  939. }
  940. } );