restrictededitingmodeediting.js 39 KB

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