restrictededitingmodeediting.js 38 KB

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