8
0

restrictededitingmodeediting.js 39 KB

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