8
0

restrictededitingmodeediting.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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 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( async () => {
  470. await 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 cut selected content inside exception marker', () => {
  487. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  488. const firstParagraph = model.document.getRoot().getChild( 0 );
  489. addExceptionMarker( 4, 7, firstParagraph, 1 );
  490. model.change( writer => {
  491. writer.setSelection( writer.createRange(
  492. writer.createPositionAt( firstParagraph, 5 ),
  493. writer.createPositionAt( firstParagraph, 6 )
  494. ) );
  495. } );
  496. viewDoc.fire( 'clipboardOutput', {
  497. content: {
  498. isEmpty: true
  499. },
  500. method: 'cut'
  501. } );
  502. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]r baz</paragraph>' );
  503. } );
  504. } );
  505. describe( 'copy', () => {
  506. it( 'should not be blocked outside exception markers', () => {
  507. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  508. const spy = sinon.spy();
  509. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  510. viewDoc.fire( 'clipboardOutput', {
  511. content: {
  512. isEmpty: true
  513. },
  514. method: 'copy'
  515. } );
  516. sinon.assert.calledOnce( spy );
  517. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  518. } );
  519. it( 'should not be blocked inside exception marker', () => {
  520. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  521. const firstParagraph = model.document.getRoot().getChild( 0 );
  522. const spy = sinon.spy();
  523. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  524. model.change( writer => {
  525. writer.addMarker( 'restrictedEditingException:1', {
  526. range: writer.createRange(
  527. writer.createPositionAt( firstParagraph, 4 ),
  528. writer.createPositionAt( firstParagraph, 7 )
  529. ),
  530. usingOperation: true,
  531. affectsData: true
  532. } );
  533. } );
  534. model.change( writer => {
  535. writer.setSelection( firstParagraph, 5 );
  536. } );
  537. viewDoc.fire( 'clipboardOutput', {
  538. content: {
  539. isEmpty: true
  540. },
  541. method: 'copy'
  542. } );
  543. sinon.assert.calledOnce( spy );
  544. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  545. } );
  546. } );
  547. describe( 'paste', () => {
  548. it( 'should be blocked outside exception markers', () => {
  549. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  550. const spy = sinon.spy();
  551. viewDoc.on( 'clipboardInput', spy, { priority: 'high' } );
  552. viewDoc.fire( 'clipboardInput', {
  553. dataTransfer: {
  554. getData: sinon.spy()
  555. }
  556. } );
  557. sinon.assert.notCalled( spy );
  558. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  559. } );
  560. it( 'should be blocked inside exception marker', () => {
  561. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  562. const firstParagraph = model.document.getRoot().getChild( 0 );
  563. const spy = sinon.spy();
  564. viewDoc.on( 'clipboardInput', spy, { priority: 'high' } );
  565. model.change( writer => {
  566. writer.addMarker( 'restrictedEditingException:1', {
  567. range: writer.createRange(
  568. writer.createPositionAt( firstParagraph, 4 ),
  569. writer.createPositionAt( firstParagraph, 7 )
  570. ),
  571. usingOperation: true,
  572. affectsData: true
  573. } );
  574. } );
  575. model.change( writer => {
  576. writer.setSelection( firstParagraph, 5 );
  577. } );
  578. viewDoc.fire( 'clipboardInput', {
  579. dataTransfer: {
  580. getData: sinon.spy()
  581. }
  582. } );
  583. sinon.assert.notCalled( spy );
  584. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  585. } );
  586. } );
  587. } );
  588. describe( 'exception highlighting', () => {
  589. let view;
  590. beforeEach( async () => {
  591. editor = await VirtualTestEditor.create( {
  592. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  593. } );
  594. model = editor.model;
  595. view = editor.editing.view;
  596. } );
  597. afterEach( () => {
  598. return editor.destroy();
  599. } );
  600. it( 'should convert the highlight to a proper view classes', () => {
  601. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  602. const paragraph = model.document.getRoot().getChild( 0 );
  603. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  604. model.change( writer => {
  605. writer.addMarker( 'restrictedEditingException:1', {
  606. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  607. usingOperation: true,
  608. affectsData: true
  609. } );
  610. } );
  611. expect( getViewData( view ) ).to.equal(
  612. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{a}r</span> baz</p>'
  613. );
  614. } );
  615. it( 'should remove classes when selection is moved away from an exception', () => {
  616. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  617. const paragraph = model.document.getRoot().getChild( 0 );
  618. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  619. model.change( writer => {
  620. writer.addMarker( 'restrictedEditingException:1', {
  621. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  622. usingOperation: true,
  623. affectsData: true
  624. } );
  625. } );
  626. expect( getViewData( view ) ).to.equal(
  627. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{a}r</span> baz</p>'
  628. );
  629. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  630. expect( getViewData( view ) ).to.equal(
  631. '<p>{}foo <span class="restricted-editing-exception">bar</span> baz</p>'
  632. );
  633. } );
  634. it( 'should work correctly when selection is moved inside an exception', () => {
  635. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  636. const paragraph = model.document.getRoot().getChild( 0 );
  637. // <paragraph>[]foo <$marker>bar</$marker> baz</paragraph>
  638. model.change( writer => {
  639. writer.addMarker( 'restrictedEditingException:1', {
  640. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  641. usingOperation: true,
  642. affectsData: true
  643. } );
  644. } );
  645. expect( getViewData( view ) ).to.equal(
  646. '<p>{}foo <span class="restricted-editing-exception">bar</span> baz</p>'
  647. );
  648. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 6 ) );
  649. expect( getViewData( view ) ).to.equal(
  650. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">ba{}r</span> baz</p>'
  651. );
  652. } );
  653. describe( 'editing downcast conversion integration', () => {
  654. it( 'works for the #insert event', () => {
  655. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  656. const paragraph = model.document.getRoot().getChild( 0 );
  657. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  658. model.change( writer => {
  659. writer.addMarker( 'restrictedEditingException:1', {
  660. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  661. usingOperation: true,
  662. affectsData: true
  663. } );
  664. } );
  665. model.change( writer => {
  666. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  667. } );
  668. expect( getViewData( view ) ).to.equal(
  669. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">bFOO{a}r</span> baz</p>'
  670. );
  671. } );
  672. it( 'works for the #remove event', () => {
  673. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  674. const paragraph = model.document.getRoot().getChild( 0 );
  675. // <paragraph>foo <$marker>b[a]r</$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. model.change( writer => {
  684. writer.remove( writer.createRange(
  685. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 ),
  686. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 6 )
  687. ) );
  688. } );
  689. expect( getViewData( view ) ).to.equal(
  690. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{}r</span> baz</p>'
  691. );
  692. } );
  693. it( 'works for the #attribute event', () => {
  694. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  695. const paragraph = model.document.getRoot().getChild( 0 );
  696. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  697. model.change( writer => {
  698. writer.addMarker( 'restrictedEditingException:1', {
  699. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  700. usingOperation: true,
  701. affectsData: true
  702. } );
  703. } );
  704. model.change( writer => {
  705. writer.setAttribute( 'bold', true, writer.createRange(
  706. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  707. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  708. );
  709. } );
  710. expect( getViewData( view ) ).to.equal(
  711. '<p>foo ' +
  712. '<span class="restricted-editing-exception restricted-editing-exception_selected">' +
  713. '<strong>b{a</strong>' +
  714. '}r</span>' +
  715. ' baz</p>'
  716. );
  717. } );
  718. it( 'works for the #selection event', () => {
  719. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  720. const paragraph = model.document.getRoot().getChild( 0 );
  721. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  722. model.change( writer => {
  723. writer.addMarker( 'restrictedEditingException:1', {
  724. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  725. usingOperation: true,
  726. affectsData: true
  727. } );
  728. } );
  729. model.change( writer => {
  730. writer.setSelection( writer.createRange(
  731. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  732. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  733. );
  734. } );
  735. expect( getViewData( view ) ).to.equal(
  736. '<p>foo {<span class="restricted-editing-exception restricted-editing-exception_selected">ba}r</span> baz</p>'
  737. );
  738. } );
  739. it( 'works for the addMarker and removeMarker events', () => {
  740. editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
  741. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  742. const paragraph = model.document.getRoot().getChild( 0 );
  743. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  744. model.change( writer => {
  745. writer.addMarker( 'restrictedEditingException:1', {
  746. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  747. usingOperation: true,
  748. affectsData: true
  749. } );
  750. } );
  751. model.change( writer => {
  752. const range = writer.createRange(
  753. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  754. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  755. );
  756. writer.addMarker( 'fooMarker', { range, usingOperation: true } );
  757. } );
  758. expect( getViewData( view ) ).to.equal(
  759. '<p>' +
  760. '<span>foo </span>' +
  761. '<span class="restricted-editing-exception restricted-editing-exception_selected">' +
  762. '<span>b</span>{a}r' +
  763. '</span>' +
  764. ' baz</p>'
  765. );
  766. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  767. expect( getViewData( view ) ).to.equal(
  768. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{a}r</span> baz</p>'
  769. );
  770. } );
  771. } );
  772. } );
  773. describe( 'exception cycling with the keyboard', () => {
  774. let view, domEvtDataStub;
  775. beforeEach( async () => {
  776. editor = await VirtualTestEditor.create( {
  777. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  778. } );
  779. model = editor.model;
  780. view = editor.editing.view;
  781. domEvtDataStub = {
  782. keyCode: getCode( 'Tab' ),
  783. preventDefault: sinon.spy(),
  784. stopPropagation: sinon.spy()
  785. };
  786. sinon.spy( editor, 'execute' );
  787. } );
  788. afterEach( () => {
  789. return editor.destroy();
  790. } );
  791. it( 'should move to the closest next exception on tab key', () => {
  792. setModelData( model, '<paragraph>[]foo bar baz qux</paragraph>' );
  793. const paragraph = model.document.getRoot().getChild( 0 );
  794. // <paragraph>[]foo <marker>bar</marker> baz qux</paragraph>
  795. model.change( writer => {
  796. writer.addMarker( 'restrictedEditingException:1', {
  797. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  798. usingOperation: true,
  799. affectsData: true
  800. } );
  801. } );
  802. // <paragraph>[]foo <marker>bar</marker> <marker>baz</marker≥ qux</paragraph>
  803. model.change( writer => {
  804. writer.addMarker( 'restrictedEditingException:2', {
  805. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  806. usingOperation: true,
  807. affectsData: true
  808. } );
  809. } );
  810. view.document.fire( 'keydown', domEvtDataStub );
  811. sinon.assert.calledOnce( editor.execute );
  812. sinon.assert.calledWithExactly( editor.execute, 'goToNextRestrictedEditingException' );
  813. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  814. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  815. } );
  816. it( 'should not move to the closest next exception on tab key when there is none', () => {
  817. setModelData( model, '<paragraph>foo qux[]</paragraph>' );
  818. const paragraph = model.document.getRoot().getChild( 0 );
  819. // <paragraph><marker>foo</marker> qux[]</paragraph>
  820. model.change( writer => {
  821. writer.addMarker( 'restrictedEditingException:1', {
  822. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 3 ) ),
  823. usingOperation: true,
  824. affectsData: true
  825. } );
  826. } );
  827. view.document.fire( 'keydown', domEvtDataStub );
  828. sinon.assert.notCalled( editor.execute );
  829. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  830. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  831. } );
  832. it( 'should move to the closest previous exception on shift+tab key', () => {
  833. setModelData( model, '<paragraph>foo bar baz qux[]</paragraph>' );
  834. const paragraph = model.document.getRoot().getChild( 0 );
  835. // <paragraph>foo <marker>bar</marker> baz qux[]</paragraph>
  836. model.change( writer => {
  837. writer.addMarker( 'restrictedEditingException:1', {
  838. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  839. usingOperation: true,
  840. affectsData: true
  841. } );
  842. } );
  843. // <paragraph>foo <marker>bar</marker> <marker>baz</marker≥ qux[]</paragraph>
  844. model.change( writer => {
  845. writer.addMarker( 'restrictedEditingException:2', {
  846. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  847. usingOperation: true,
  848. affectsData: true
  849. } );
  850. } );
  851. domEvtDataStub.keyCode += getCode( 'Shift' );
  852. view.document.fire( 'keydown', domEvtDataStub );
  853. sinon.assert.calledOnce( editor.execute );
  854. sinon.assert.calledWithExactly( editor.execute, 'goToPreviousRestrictedEditingException' );
  855. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  856. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  857. } );
  858. it( 'should not move to the closest previous exception on shift+tab key when there is none', () => {
  859. setModelData( model, '<paragraph>[]foo qux</paragraph>' );
  860. const paragraph = model.document.getRoot().getChild( 0 );
  861. // <paragraph>[]foo <marker>qux</marker></paragraph>
  862. model.change( writer => {
  863. writer.addMarker( 'restrictedEditingException:1', {
  864. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  865. usingOperation: true,
  866. affectsData: true
  867. } );
  868. } );
  869. domEvtDataStub.keyCode += getCode( 'Shift' );
  870. view.document.fire( 'keydown', domEvtDataStub );
  871. sinon.assert.notCalled( editor.execute );
  872. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  873. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  874. } );
  875. } );
  876. // Helper method that creates an exception marker inside given parent.
  877. // Marker range is set to given position offsets (start, end).
  878. function addExceptionMarker( startOffset, endOffset = startOffset, parent, id = 1 ) {
  879. model.change( writer => {
  880. writer.addMarker( `restrictedEditingException:${ id }`, {
  881. range: writer.createRange(
  882. writer.createPositionAt( parent, startOffset ),
  883. writer.createPositionAt( parent, endOffset )
  884. ),
  885. usingOperation: true,
  886. affectsData: true
  887. } );
  888. } );
  889. }
  890. } );