restrictededitingmodeediting.js 38 KB

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