restrictededitingmodeediting.js 39 KB

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