8
0

restrictededitingmodeediting.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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;
  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( '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. let model;
  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. let model;
  136. beforeEach( async () => {
  137. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingModeEditing ] } );
  138. model = editor.model;
  139. } );
  140. afterEach( () => {
  141. return editor.destroy();
  142. } );
  143. it( 'should keep markers in the view when editable region is edited', () => {
  144. setModelData( model,
  145. '<paragraph>foo bar baz</paragraph>' +
  146. '<paragraph>xxx y[]yy zzz</paragraph>'
  147. );
  148. const firstParagraph = model.document.getRoot().getChild( 0 );
  149. const secondParagraph = model.document.getRoot().getChild( 1 );
  150. model.change( writer => {
  151. writer.addMarker( 'restrictedEditingException:1', {
  152. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  153. usingOperation: true,
  154. affectsData: true
  155. } );
  156. writer.addMarker( 'restrictedEditingException:2', {
  157. range: writer.createRange(
  158. writer.createPositionAt( secondParagraph, 4 ),
  159. writer.createPositionAt( secondParagraph, 7 )
  160. ),
  161. usingOperation: true,
  162. affectsData: true
  163. } );
  164. } );
  165. model.change( writer => {
  166. model.insertContent( writer.createText( 'R', model.document.selection.getAttributes() ) );
  167. } );
  168. expect( editor.getData() ).to.equal(
  169. '<p>foo <span class="restricted-editing-exception">bar</span> baz</p>' +
  170. '<p>xxx <span class="restricted-editing-exception">yRyy</span> zzz</p>' );
  171. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  172. '<p>foo <span class="restricted-editing-exception">bar</span> baz</p>' +
  173. '<p>xxx <span class="restricted-editing-exception restricted-editing-exception_selected">yRyy</span> zzz</p>' );
  174. } );
  175. it( 'should block user typing outside exception markers', () => {
  176. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  177. editor.execute( 'input', { text: 'X' } );
  178. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  179. } );
  180. it( 'should not block user typing inside exception marker', () => {
  181. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  182. const firstParagraph = model.document.getRoot().getChild( 0 );
  183. model.change( writer => {
  184. writer.addMarker( 'restrictedEditingException:1', {
  185. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  186. usingOperation: true,
  187. affectsData: true
  188. } );
  189. } );
  190. model.change( writer => {
  191. writer.setSelection( firstParagraph, 5 );
  192. } );
  193. editor.execute( 'input', { text: 'X' } );
  194. assertEqualMarkup( getModelData( model ), '<paragraph>foo bX[]ar baz</paragraph>' );
  195. } );
  196. it( 'should extend maker when typing on the marker boundary (end)', () => {
  197. setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
  198. const firstParagraph = model.document.getRoot().getChild( 0 );
  199. model.change( writer => {
  200. writer.addMarker( 'restrictedEditingException:1', {
  201. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  202. usingOperation: true,
  203. affectsData: true
  204. } );
  205. } );
  206. editor.execute( 'input', { text: 'X' } );
  207. assertEqualMarkup( getModelData( model ), '<paragraph>foo barX[] baz</paragraph>' );
  208. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  209. const expectedRange = model.createRange(
  210. model.createPositionAt( firstParagraph, 4 ),
  211. model.createPositionAt( firstParagraph, 8 )
  212. );
  213. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  214. } );
  215. it( 'should extend marker when typing on the marker boundary (start)', () => {
  216. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  217. const firstParagraph = model.document.getRoot().getChild( 0 );
  218. model.change( writer => {
  219. writer.addMarker( 'restrictedEditingException:1', {
  220. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  221. usingOperation: true,
  222. affectsData: true
  223. } );
  224. } );
  225. editor.execute( 'input', { text: 'X' } );
  226. assertEqualMarkup( getModelData( model ), '<paragraph>foo X[]bar baz</paragraph>' );
  227. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  228. const expectedRange = model.createRange(
  229. model.createPositionAt( firstParagraph, 4 ),
  230. model.createPositionAt( firstParagraph, 8 )
  231. );
  232. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  233. } );
  234. it( 'should extend marker when typing on the marker boundary (collapsed marker)', () => {
  235. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  236. const firstParagraph = model.document.getRoot().getChild( 0 );
  237. model.change( writer => {
  238. writer.addMarker( 'restrictedEditingException:1', {
  239. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ) ),
  240. usingOperation: true,
  241. affectsData: true
  242. } );
  243. } );
  244. model.change( writer => {
  245. writer.setSelection( writer.createPositionAt( firstParagraph, 4 ) );
  246. } );
  247. editor.execute( 'input', { text: 'X' } );
  248. assertEqualMarkup( getModelData( model ), '<paragraph>foo X[]bar baz</paragraph>' );
  249. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  250. const expectedRange = model.createRange(
  251. model.createPositionAt( firstParagraph, 4 ),
  252. model.createPositionAt( firstParagraph, 5 )
  253. );
  254. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  255. } );
  256. it( 'should not move collapsed marker to $graveyard', () => {
  257. setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
  258. const firstParagraph = model.document.getRoot().getChild( 0 );
  259. model.change( writer => {
  260. writer.addMarker( 'restrictedEditingException:1', {
  261. range: writer.createRange(
  262. writer.createPositionAt( firstParagraph, 4 ),
  263. writer.createPositionAt( firstParagraph, 5 )
  264. ),
  265. usingOperation: true,
  266. affectsData: true
  267. } );
  268. } );
  269. editor.execute( 'delete' );
  270. assertEqualMarkup( getModelData( model ), '<paragraph>foo []ar baz</paragraph>' );
  271. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  272. const expectedRange = model.createRange(
  273. model.createPositionAt( firstParagraph, 4 ),
  274. model.createPositionAt( firstParagraph, 4 )
  275. );
  276. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  277. } );
  278. } );
  279. describe( 'clipboard', () => {
  280. let model, viewDoc;
  281. beforeEach( async () => {
  282. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, Clipboard, RestrictedEditingModeEditing ] } );
  283. model = editor.model;
  284. viewDoc = editor.editing.view.document;
  285. } );
  286. afterEach( () => {
  287. return editor.destroy();
  288. } );
  289. describe( 'cut', () => {
  290. it( 'should be blocked outside exception markers', () => {
  291. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  292. const spy = sinon.spy();
  293. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  294. viewDoc.fire( 'clipboardOutput', {
  295. content: {
  296. isEmpty: true
  297. },
  298. method: 'cut'
  299. } );
  300. sinon.assert.notCalled( spy );
  301. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  302. } );
  303. it( 'should be blocked inside exception marker', () => {
  304. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  305. const firstParagraph = model.document.getRoot().getChild( 0 );
  306. const spy = sinon.spy();
  307. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  308. model.change( writer => {
  309. writer.addMarker( 'restrictedEditingException:1', {
  310. range: writer.createRange(
  311. writer.createPositionAt( firstParagraph, 4 ),
  312. writer.createPositionAt( firstParagraph, 7 )
  313. ),
  314. usingOperation: true,
  315. affectsData: true
  316. } );
  317. } );
  318. model.change( writer => {
  319. writer.setSelection( firstParagraph, 5 );
  320. } );
  321. viewDoc.fire( 'clipboardOutput', {
  322. content: {
  323. isEmpty: true
  324. },
  325. method: 'cut'
  326. } );
  327. sinon.assert.notCalled( spy );
  328. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  329. } );
  330. } );
  331. describe( 'copy', () => {
  332. it( 'should not be blocked outside exception markers', () => {
  333. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  334. const spy = sinon.spy();
  335. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  336. viewDoc.fire( 'clipboardOutput', {
  337. content: {
  338. isEmpty: true
  339. },
  340. method: 'copy'
  341. } );
  342. sinon.assert.calledOnce( spy );
  343. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  344. } );
  345. it( 'should not be blocked inside exception marker', () => {
  346. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  347. const firstParagraph = model.document.getRoot().getChild( 0 );
  348. const spy = sinon.spy();
  349. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  350. model.change( writer => {
  351. writer.addMarker( 'restrictedEditingException:1', {
  352. range: writer.createRange(
  353. writer.createPositionAt( firstParagraph, 4 ),
  354. writer.createPositionAt( firstParagraph, 7 )
  355. ),
  356. usingOperation: true,
  357. affectsData: true
  358. } );
  359. } );
  360. model.change( writer => {
  361. writer.setSelection( firstParagraph, 5 );
  362. } );
  363. viewDoc.fire( 'clipboardOutput', {
  364. content: {
  365. isEmpty: true
  366. },
  367. method: 'copy'
  368. } );
  369. sinon.assert.calledOnce( spy );
  370. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  371. } );
  372. } );
  373. describe( 'paste', () => {
  374. it( 'should be blocked outside exception markers', () => {
  375. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  376. const spy = sinon.spy();
  377. viewDoc.on( 'clipboardInput', spy, { priority: 'high' } );
  378. viewDoc.fire( 'clipboardInput', {
  379. dataTransfer: {
  380. getData: sinon.spy()
  381. }
  382. } );
  383. sinon.assert.notCalled( spy );
  384. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  385. } );
  386. it( 'should be blocked inside exception marker', () => {
  387. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  388. const firstParagraph = model.document.getRoot().getChild( 0 );
  389. const spy = sinon.spy();
  390. viewDoc.on( 'clipboardInput', spy, { priority: 'high' } );
  391. model.change( writer => {
  392. writer.addMarker( 'restrictedEditingException:1', {
  393. range: writer.createRange(
  394. writer.createPositionAt( firstParagraph, 4 ),
  395. writer.createPositionAt( firstParagraph, 7 )
  396. ),
  397. usingOperation: true,
  398. affectsData: true
  399. } );
  400. } );
  401. model.change( writer => {
  402. writer.setSelection( firstParagraph, 5 );
  403. } );
  404. viewDoc.fire( 'clipboardInput', {
  405. dataTransfer: {
  406. getData: sinon.spy()
  407. }
  408. } );
  409. sinon.assert.notCalled( spy );
  410. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  411. } );
  412. } );
  413. } );
  414. describe( 'exception highlighting', () => {
  415. let model, view;
  416. beforeEach( async () => {
  417. editor = await VirtualTestEditor.create( {
  418. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  419. } );
  420. model = editor.model;
  421. view = editor.editing.view;
  422. } );
  423. afterEach( () => {
  424. return editor.destroy();
  425. } );
  426. it( 'should convert the highlight to a proper view classes', () => {
  427. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  428. const paragraph = model.document.getRoot().getChild( 0 );
  429. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  430. model.change( writer => {
  431. writer.addMarker( 'restrictedEditingException:1', {
  432. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  433. usingOperation: true,
  434. affectsData: true
  435. } );
  436. } );
  437. expect( getViewData( view ) ).to.equal(
  438. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{a}r</span> baz</p>'
  439. );
  440. } );
  441. it( 'should remove classes when selection is moved away from an exception', () => {
  442. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  443. const paragraph = model.document.getRoot().getChild( 0 );
  444. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  445. model.change( writer => {
  446. writer.addMarker( 'restrictedEditingException:1', {
  447. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  448. usingOperation: true,
  449. affectsData: true
  450. } );
  451. } );
  452. expect( getViewData( view ) ).to.equal(
  453. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{a}r</span> baz</p>'
  454. );
  455. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  456. expect( getViewData( view ) ).to.equal(
  457. '<p>{}foo <span class="restricted-editing-exception">bar</span> baz</p>'
  458. );
  459. } );
  460. it( 'should work correctly when selection is moved inside an exception', () => {
  461. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  462. const paragraph = model.document.getRoot().getChild( 0 );
  463. // <paragraph>[]foo <$marker>bar</$marker> baz</paragraph>
  464. model.change( writer => {
  465. writer.addMarker( 'restrictedEditingException:1', {
  466. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  467. usingOperation: true,
  468. affectsData: true
  469. } );
  470. } );
  471. expect( getViewData( view ) ).to.equal(
  472. '<p>{}foo <span class="restricted-editing-exception">bar</span> baz</p>'
  473. );
  474. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 6 ) );
  475. expect( getViewData( view ) ).to.equal(
  476. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">ba{}r</span> baz</p>'
  477. );
  478. } );
  479. describe( 'editing downcast conversion integration', () => {
  480. it( 'works for the #insert event', () => {
  481. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  482. const paragraph = model.document.getRoot().getChild( 0 );
  483. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  484. model.change( writer => {
  485. writer.addMarker( 'restrictedEditingException:1', {
  486. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  487. usingOperation: true,
  488. affectsData: true
  489. } );
  490. } );
  491. model.change( writer => {
  492. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  493. } );
  494. expect( getViewData( view ) ).to.equal(
  495. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">bFOO{a}r</span> baz</p>'
  496. );
  497. } );
  498. it( 'works for the #remove event', () => {
  499. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  500. const paragraph = model.document.getRoot().getChild( 0 );
  501. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  502. model.change( writer => {
  503. writer.addMarker( 'restrictedEditingException:1', {
  504. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  505. usingOperation: true,
  506. affectsData: true
  507. } );
  508. } );
  509. model.change( writer => {
  510. writer.remove( writer.createRange(
  511. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 ),
  512. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 6 )
  513. ) );
  514. } );
  515. expect( getViewData( view ) ).to.equal(
  516. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{}r</span> baz</p>'
  517. );
  518. } );
  519. it( 'works for the #attribute event', () => {
  520. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  521. const paragraph = model.document.getRoot().getChild( 0 );
  522. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  523. model.change( writer => {
  524. writer.addMarker( 'restrictedEditingException:1', {
  525. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  526. usingOperation: true,
  527. affectsData: true
  528. } );
  529. } );
  530. model.change( writer => {
  531. writer.setAttribute( 'bold', true, writer.createRange(
  532. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  533. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  534. );
  535. } );
  536. expect( getViewData( view ) ).to.equal(
  537. '<p>foo ' +
  538. '<span class="restricted-editing-exception restricted-editing-exception_selected">' +
  539. '<strong>b{a</strong>' +
  540. '}r</span>' +
  541. ' baz</p>'
  542. );
  543. } );
  544. it( 'works for the #selection event', () => {
  545. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  546. const paragraph = model.document.getRoot().getChild( 0 );
  547. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  548. model.change( writer => {
  549. writer.addMarker( 'restrictedEditingException:1', {
  550. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  551. usingOperation: true,
  552. affectsData: true
  553. } );
  554. } );
  555. model.change( writer => {
  556. writer.setSelection( writer.createRange(
  557. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  558. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  559. );
  560. } );
  561. expect( getViewData( view ) ).to.equal(
  562. '<p>foo {<span class="restricted-editing-exception restricted-editing-exception_selected">ba}r</span> baz</p>'
  563. );
  564. } );
  565. it( 'works for the addMarker and removeMarker events', () => {
  566. editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
  567. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  568. const paragraph = model.document.getRoot().getChild( 0 );
  569. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  570. model.change( writer => {
  571. writer.addMarker( 'restrictedEditingException:1', {
  572. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  573. usingOperation: true,
  574. affectsData: true
  575. } );
  576. } );
  577. model.change( writer => {
  578. const range = writer.createRange(
  579. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  580. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  581. );
  582. writer.addMarker( 'fooMarker', { range, usingOperation: true } );
  583. } );
  584. expect( getViewData( view ) ).to.equal(
  585. '<p>' +
  586. '<span>foo </span>' +
  587. '<span class="restricted-editing-exception restricted-editing-exception_selected">' +
  588. '<span>b</span>{a}r' +
  589. '</span>' +
  590. ' baz</p>'
  591. );
  592. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  593. expect( getViewData( view ) ).to.equal(
  594. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{a}r</span> baz</p>'
  595. );
  596. } );
  597. } );
  598. } );
  599. describe( 'exception cycling with the keyboard', () => {
  600. let model, view, domEvtDataStub;
  601. beforeEach( async () => {
  602. editor = await VirtualTestEditor.create( {
  603. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  604. } );
  605. model = editor.model;
  606. view = editor.editing.view;
  607. domEvtDataStub = {
  608. keyCode: getCode( 'Tab' ),
  609. preventDefault: sinon.spy(),
  610. stopPropagation: sinon.spy()
  611. };
  612. sinon.spy( editor, 'execute' );
  613. } );
  614. afterEach( () => {
  615. return editor.destroy();
  616. } );
  617. it( 'should move to the closest next exception on tab key', () => {
  618. setModelData( model, '<paragraph>[]foo bar baz qux</paragraph>' );
  619. const paragraph = model.document.getRoot().getChild( 0 );
  620. // <paragraph>[]foo <marker>bar</marker> baz qux</paragraph>
  621. model.change( writer => {
  622. writer.addMarker( 'restrictedEditingException:1', {
  623. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  624. usingOperation: true,
  625. affectsData: true
  626. } );
  627. } );
  628. // <paragraph>[]foo <marker>bar</marker> <marker>baz</marker≥ qux</paragraph>
  629. model.change( writer => {
  630. writer.addMarker( 'restrictedEditingException:2', {
  631. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  632. usingOperation: true,
  633. affectsData: true
  634. } );
  635. } );
  636. view.document.fire( 'keydown', domEvtDataStub );
  637. sinon.assert.calledOnce( editor.execute );
  638. sinon.assert.calledWithExactly( editor.execute, 'goToNextRestrictedEditingException' );
  639. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  640. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  641. } );
  642. it( 'should not move to the closest next exception on tab key when there is none', () => {
  643. setModelData( model, '<paragraph>foo qux[]</paragraph>' );
  644. const paragraph = model.document.getRoot().getChild( 0 );
  645. // <paragraph><marker>foo</marker> qux[]</paragraph>
  646. model.change( writer => {
  647. writer.addMarker( 'restrictedEditingException:1', {
  648. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 3 ) ),
  649. usingOperation: true,
  650. affectsData: true
  651. } );
  652. } );
  653. view.document.fire( 'keydown', domEvtDataStub );
  654. sinon.assert.notCalled( editor.execute );
  655. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  656. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  657. } );
  658. it( 'should move to the closest previous exception on shift+tab key', () => {
  659. setModelData( model, '<paragraph>foo bar baz qux[]</paragraph>' );
  660. const paragraph = model.document.getRoot().getChild( 0 );
  661. // <paragraph>foo <marker>bar</marker> baz qux[]</paragraph>
  662. model.change( writer => {
  663. writer.addMarker( 'restrictedEditingException:1', {
  664. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  665. usingOperation: true,
  666. affectsData: true
  667. } );
  668. } );
  669. // <paragraph>foo <marker>bar</marker> <marker>baz</marker≥ qux[]</paragraph>
  670. model.change( writer => {
  671. writer.addMarker( 'restrictedEditingException:2', {
  672. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  673. usingOperation: true,
  674. affectsData: true
  675. } );
  676. } );
  677. domEvtDataStub.keyCode += getCode( 'Shift' );
  678. view.document.fire( 'keydown', domEvtDataStub );
  679. sinon.assert.calledOnce( editor.execute );
  680. sinon.assert.calledWithExactly( editor.execute, 'goToPreviousRestrictedEditingException' );
  681. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  682. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  683. } );
  684. it( 'should not move to the closest previous exception on shift+tab key when there is none', () => {
  685. setModelData( model, '<paragraph>[]foo qux</paragraph>' );
  686. const paragraph = model.document.getRoot().getChild( 0 );
  687. // <paragraph>[]foo <marker>qux</marker></paragraph>
  688. model.change( writer => {
  689. writer.addMarker( 'restrictedEditingException:1', {
  690. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  691. usingOperation: true,
  692. affectsData: true
  693. } );
  694. } );
  695. domEvtDataStub.keyCode += getCode( 'Shift' );
  696. view.document.fire( 'keydown', domEvtDataStub );
  697. sinon.assert.notCalled( editor.execute );
  698. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  699. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  700. } );
  701. } );
  702. } );