restrictededitingmodeediting.js 30 KB

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