restrictededitingmodeediting.js 38 KB

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