restrictededitingmodeediting.js 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  1. /**
  2. * @license Copyright (c) 2003-2020, 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 { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
  13. import StrikethroughEditing from '@ckeditor/ckeditor5-basic-styles/src/strikethrough/strikethroughediting';
  14. import LinkEditing from '@ckeditor/ckeditor5-link/src/linkediting';
  15. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  16. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  17. import RestrictedEditingModeEditing from './../src/restrictededitingmodeediting';
  18. import RestrictedEditingModeNavigationCommand from '../src/restrictededitingmodenavigationcommand';
  19. import ItalicEditing from '@ckeditor/ckeditor5-basic-styles/src/italic/italicediting';
  20. import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
  21. import TableEditing from '@ckeditor/ckeditor5-table/src/tableediting';
  22. import Command from '@ckeditor/ckeditor5-core/src/command';
  23. describe( 'RestrictedEditingModeEditing', () => {
  24. let editor, model;
  25. testUtils.createSinonSandbox();
  26. describe( 'plugin', () => {
  27. beforeEach( async () => {
  28. editor = await VirtualTestEditor.create( { plugins: [ RestrictedEditingModeEditing ] } );
  29. } );
  30. afterEach( async () => {
  31. await editor.destroy();
  32. } );
  33. it( 'should be named', () => {
  34. expect( RestrictedEditingModeEditing.pluginName ).to.equal( 'RestrictedEditingModeEditing' );
  35. } );
  36. it( 'should be loaded', () => {
  37. expect( editor.plugins.get( RestrictedEditingModeEditing ) ).to.be.instanceOf( RestrictedEditingModeEditing );
  38. } );
  39. it( 'root should have "ck-restricted-editing_mode_restricted" class', () => {
  40. for ( const root of editor.editing.view.document.roots ) {
  41. expect( root.hasClass( 'ck-restricted-editing_mode_restricted' ) ).to.be.true;
  42. }
  43. } );
  44. it( 'adds a "goToPreviousRestrictedEditingException" command', () => {
  45. expect( editor.commands.get( 'goToPreviousRestrictedEditingException' ) )
  46. .to.be.instanceOf( RestrictedEditingModeNavigationCommand );
  47. } );
  48. it( 'adds a "goToNextRestrictedEditingException" command', () => {
  49. expect(
  50. editor.commands.get( 'goToNextRestrictedEditingException' )
  51. ).to.be.instanceOf( RestrictedEditingModeNavigationCommand );
  52. } );
  53. } );
  54. describe( 'enableCommand()', () => {
  55. let plugin, command;
  56. class FakeCommand extends Command {
  57. refresh() {
  58. this.isEnabled = true;
  59. }
  60. }
  61. beforeEach( async () => {
  62. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, RestrictedEditingModeEditing ] } );
  63. model = editor.model;
  64. plugin = editor.plugins.get( RestrictedEditingModeEditing );
  65. command = new FakeCommand( editor );
  66. editor.commands.add( 'fakeCommand', command );
  67. setModelData( editor.model, '<paragraph>[]foo bar baz qux</paragraph>' );
  68. addExceptionMarker( 4, 7, model.document.getRoot().getChild( 0 ) );
  69. } );
  70. it( 'should enable the command globally', () => {
  71. expect( command.isEnabled ).to.be.false;
  72. plugin.enableCommand( 'fakeCommand' );
  73. expect( command.isEnabled ).to.be.true;
  74. } );
  75. } );
  76. describe( 'conversion', () => {
  77. beforeEach( async () => {
  78. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, TableEditing, RestrictedEditingModeEditing ] } );
  79. model = editor.model;
  80. } );
  81. afterEach( async () => {
  82. await editor.destroy();
  83. } );
  84. describe( 'upcast', () => {
  85. it( 'should convert <span class="restricted-editing-exception"> to marker', () => {
  86. editor.setData( '<p>foo <span class="restricted-editing-exception">bar</span> baz</p>' );
  87. expect( model.markers.has( 'restrictedEditingException:1' ) ).to.be.true;
  88. assertMarkerRangePaths( [ 0, 4 ], [ 0, 7 ] );
  89. } );
  90. it( 'should convert multiple <span class="restricted-editing-exception">', () => {
  91. editor.setData(
  92. '<p>foo <span class="restricted-editing-exception">bar</span> baz</p>' +
  93. '<p>ABCDEF<span class="restricted-editing-exception">GHIJK</span>LMNOPQRST</p>'
  94. );
  95. expect( model.markers.has( 'restrictedEditingException:1' ) ).to.be.true;
  96. expect( model.markers.has( 'restrictedEditingException:2' ) ).to.be.true;
  97. // Data for the first marker is the same as in previous tests so no need to test it again.
  98. assertMarkerRangePaths( [ 1, 6 ], [ 1, 11 ], 2 );
  99. } );
  100. it( 'should convert <span class="restricted-editing-exception"> inside table to marker', () => {
  101. editor.setData(
  102. '<figure class="table">' +
  103. '<table><tbody><tr><td><span class="restricted-editing-exception">bar</span></td></tr></tbody></table>' +
  104. '</figure>'
  105. );
  106. expect( model.markers.has( 'restrictedEditingException:1' ) ).to.be.true;
  107. const marker = model.markers.get( 'restrictedEditingException:1' );
  108. expect( marker.getStart().path ).to.deep.equal( [ 0, 0, 0, 0, 0 ] );
  109. expect( marker.getEnd().path ).to.deep.equal( [ 0, 0, 0, 0, 3 ] );
  110. } );
  111. it( 'should not convert other <span> elements', () => {
  112. editor.setData( '<p>foo <span class="foo bar">bar</span> baz</p>' );
  113. expect( model.markers.has( 'restrictedEditingException:1' ) ).to.be.false;
  114. } );
  115. } );
  116. describe( 'downcast', () => {
  117. it( 'should convert model marker to <span>', () => {
  118. setModelData( model, '<paragraph>foo bar baz</paragraph>' );
  119. const paragraph = model.document.getRoot().getChild( 0 );
  120. model.change( writer => {
  121. writer.addMarker( 'restrictedEditingException:1', {
  122. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  123. usingOperation: true,
  124. affectsData: true
  125. } );
  126. } );
  127. const expectedView = '<p>foo <span class="restricted-editing-exception">bar</span> baz</p>';
  128. expect( editor.getData() ).to.equal( expectedView );
  129. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  130. } );
  131. it( 'should convert collapsed model marker to <span>', () => {
  132. setModelData( model, '<paragraph>foo bar baz</paragraph>' );
  133. const paragraph = model.document.getRoot().getChild( 0 );
  134. model.change( writer => {
  135. writer.addMarker( 'restrictedEditingException:1', {
  136. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 4 ) ),
  137. usingOperation: true,
  138. affectsData: true
  139. } );
  140. } );
  141. expect( editor.getData() ).to.equal( '<p>foo <span class="restricted-editing-exception"></span>bar baz</p>' );
  142. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  143. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_collapsed"></span>bar baz</p>'
  144. );
  145. } );
  146. it( 'converted <span> should be the outermost attribute element', () => {
  147. editor.conversion.for( 'downcast' ).attributeToElement( { model: 'bold', view: 'b' } );
  148. setModelData( model, '<paragraph><$text bold="true">foo bar baz</$text></paragraph>' );
  149. const paragraph = model.document.getRoot().getChild( 0 );
  150. model.change( writer => {
  151. writer.addMarker( 'restrictedEditingException:1', {
  152. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 'end' ) ),
  153. usingOperation: true,
  154. affectsData: true
  155. } );
  156. } );
  157. expect( editor.getData() ).to.equal(
  158. '<p><span class="restricted-editing-exception"><b>foo bar baz</b></span></p>'
  159. );
  160. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  161. '<p>' +
  162. '<span class="restricted-editing-exception restricted-editing-exception_selected"><b>foo bar baz</b></span>' +
  163. '</p>'
  164. );
  165. } );
  166. it( 'converted <span> should be the outermost attribute element', () => {
  167. editor.conversion.for( 'downcast' ).attributeToElement( { model: 'bold', view: 'b' } );
  168. setModelData( model,
  169. '<table><tableRow><tableCell>' +
  170. '<paragraph><$text bold="true">foo bar baz</$text></paragraph>' +
  171. '</tableCell></tableRow></table>'
  172. );
  173. const paragraph = model.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 0 ).getChild( 0 );
  174. model.change( writer => {
  175. writer.addMarker( 'restrictedEditingException:1', {
  176. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 'end' ) ),
  177. usingOperation: true,
  178. affectsData: true
  179. } );
  180. } );
  181. assertEqualMarkup( editor.getData(),
  182. '<figure class="table"><table><tbody><tr><td>' +
  183. '<span class="restricted-editing-exception"><b>foo bar baz</b></span>' +
  184. '</td></tr></tbody></table></figure>'
  185. );
  186. assertEqualMarkup( getViewData( editor.editing.view, { withoutSelection: true } ),
  187. '<figure class="ck-widget ck-widget_with-selection-handle table" contenteditable="false">' +
  188. '<div class="ck ck-widget__selection-handle"></div>' +
  189. '<table><tbody><tr><td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  190. '<span><span class="restricted-editing-exception"><b>foo bar baz</b></span></span>' +
  191. '</td></tr></tbody></table>' +
  192. '</figure>'
  193. );
  194. } );
  195. } );
  196. describe( 'flattening exception markers', () => {
  197. it( 'should fix non-flat marker range (start is higher in tree)', () => {
  198. setModelData( model, '<table><tableRow><tableCell><paragraph>foo bar baz</paragraph></tableCell></tableRow></table>' );
  199. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  200. const paragraph = model.document.getRoot().getNodeByPath( [ 0, 0, 0, 0 ] );
  201. model.change( writer => {
  202. writer.addMarker( `restrictedEditingException:${ 1 }`, {
  203. range: writer.createRange(
  204. writer.createPositionAt( paragraph, 0 ),
  205. writer.createPositionAt( tableCell, 'end' )
  206. ),
  207. usingOperation: true,
  208. affectsData: true
  209. } );
  210. } );
  211. const marker = model.markers.get( 'restrictedEditingException:1' );
  212. expect( marker.getStart().parent ).to.equal( marker.getEnd().parent );
  213. expect( marker.getStart().path ).to.deep.equal( [ 0, 0, 0, 0, 0 ] );
  214. expect( marker.getEnd().path ).to.deep.equal( [ 0, 0, 0, 0, 11 ] );
  215. } );
  216. it( 'should fix non-flat marker range (end is higher in tree)', () => {
  217. setModelData( model, '<table><tableRow><tableCell><paragraph>foo bar baz</paragraph></tableCell></tableRow></table>' );
  218. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  219. const paragraph = model.document.getRoot().getNodeByPath( [ 0, 0, 0, 0 ] );
  220. model.change( writer => {
  221. writer.addMarker( `restrictedEditingException:${ 1 }`, {
  222. range: writer.createRange(
  223. writer.createPositionAt( tableCell, 0 ),
  224. writer.createPositionAt( paragraph, 'end' )
  225. ),
  226. usingOperation: true,
  227. affectsData: true
  228. } );
  229. } );
  230. const marker = model.markers.get( 'restrictedEditingException:1' );
  231. expect( marker.getStart().parent ).to.equal( marker.getEnd().parent );
  232. expect( marker.getStart().path ).to.deep.equal( [ 0, 0, 0, 0, 0 ] );
  233. expect( marker.getEnd().path ).to.deep.equal( [ 0, 0, 0, 0, 11 ] );
  234. } );
  235. } );
  236. } );
  237. describe( 'editing behavior', () => {
  238. beforeEach( async () => {
  239. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingModeEditing ] } );
  240. model = editor.model;
  241. } );
  242. afterEach( () => {
  243. return editor.destroy();
  244. } );
  245. it( 'should keep markers in the view when editable region is edited', () => {
  246. setModelData( model,
  247. '<paragraph>foo bar baz</paragraph>' +
  248. '<paragraph>xxx y[]yy zzz</paragraph>'
  249. );
  250. const firstParagraph = model.document.getRoot().getChild( 0 );
  251. const secondParagraph = model.document.getRoot().getChild( 1 );
  252. model.change( writer => {
  253. writer.addMarker( 'restrictedEditingException:1', {
  254. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  255. usingOperation: true,
  256. affectsData: true
  257. } );
  258. writer.addMarker( 'restrictedEditingException:2', {
  259. range: writer.createRange(
  260. writer.createPositionAt( secondParagraph, 4 ),
  261. writer.createPositionAt( secondParagraph, 7 )
  262. ),
  263. usingOperation: true,
  264. affectsData: true
  265. } );
  266. } );
  267. model.change( writer => {
  268. model.insertContent( writer.createText( 'R', model.document.selection.getAttributes() ) );
  269. } );
  270. expect( editor.getData() ).to.equal(
  271. '<p>foo <span class="restricted-editing-exception">bar</span> baz</p>' +
  272. '<p>xxx <span class="restricted-editing-exception">yRyy</span> zzz</p>' );
  273. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  274. '<p>foo <span class="restricted-editing-exception">bar</span> baz</p>' +
  275. '<p>xxx <span class="restricted-editing-exception restricted-editing-exception_selected">yRyy</span> zzz</p>' );
  276. } );
  277. it( 'should block user typing outside exception markers', () => {
  278. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  279. editor.execute( 'input', { text: 'X' } );
  280. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  281. } );
  282. it( 'should not block user typing inside exception marker', () => {
  283. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  284. const firstParagraph = model.document.getRoot().getChild( 0 );
  285. model.change( writer => {
  286. writer.addMarker( 'restrictedEditingException:1', {
  287. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  288. usingOperation: true,
  289. affectsData: true
  290. } );
  291. } );
  292. model.change( writer => {
  293. writer.setSelection( firstParagraph, 5 );
  294. } );
  295. editor.execute( 'input', { text: 'X' } );
  296. assertEqualMarkup( getModelData( model ), '<paragraph>foo bX[]ar baz</paragraph>' );
  297. } );
  298. it( 'should extend maker when typing on the marker boundary (end)', () => {
  299. setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
  300. const firstParagraph = model.document.getRoot().getChild( 0 );
  301. model.change( writer => {
  302. writer.addMarker( 'restrictedEditingException:1', {
  303. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  304. usingOperation: true,
  305. affectsData: true
  306. } );
  307. } );
  308. editor.execute( 'input', { text: 'X' } );
  309. assertEqualMarkup( getModelData( model ), '<paragraph>foo barX[] baz</paragraph>' );
  310. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  311. const expectedRange = model.createRange(
  312. model.createPositionAt( firstParagraph, 4 ),
  313. model.createPositionAt( firstParagraph, 8 )
  314. );
  315. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  316. } );
  317. it( 'should extend marker when typing on the marker boundary (start)', () => {
  318. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  319. const firstParagraph = model.document.getRoot().getChild( 0 );
  320. model.change( writer => {
  321. writer.addMarker( 'restrictedEditingException:1', {
  322. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  323. usingOperation: true,
  324. affectsData: true
  325. } );
  326. } );
  327. editor.execute( 'input', { text: 'X' } );
  328. assertEqualMarkup( getModelData( model ), '<paragraph>foo X[]bar baz</paragraph>' );
  329. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  330. const expectedRange = model.createRange(
  331. model.createPositionAt( firstParagraph, 4 ),
  332. model.createPositionAt( firstParagraph, 8 )
  333. );
  334. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  335. } );
  336. it( 'should extend marker when typing on the marker boundary (collapsed marker)', () => {
  337. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  338. const firstParagraph = model.document.getRoot().getChild( 0 );
  339. model.change( writer => {
  340. writer.addMarker( 'restrictedEditingException:1', {
  341. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ) ),
  342. usingOperation: true,
  343. affectsData: true
  344. } );
  345. } );
  346. model.change( writer => {
  347. writer.setSelection( writer.createPositionAt( firstParagraph, 4 ) );
  348. } );
  349. editor.execute( 'input', { text: 'X' } );
  350. assertEqualMarkup( getModelData( model ), '<paragraph>foo X[]bar baz</paragraph>' );
  351. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  352. const expectedRange = model.createRange(
  353. model.createPositionAt( firstParagraph, 4 ),
  354. model.createPositionAt( firstParagraph, 5 )
  355. );
  356. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  357. } );
  358. it( 'should retain marker on non-typing change at the marker boundary (start)', () => {
  359. setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
  360. const firstParagraph = model.document.getRoot().getChild( 0 );
  361. addExceptionMarker( 4, 7, firstParagraph );
  362. model.change( writer => {
  363. editor.execute( 'delete', {
  364. selection: writer.createSelection( writer.createRange(
  365. writer.createPositionAt( firstParagraph, 4 ),
  366. writer.createPositionAt( firstParagraph, 6 )
  367. ) )
  368. } );
  369. editor.execute( 'input', {
  370. text: 'XX',
  371. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ) )
  372. } );
  373. } );
  374. assertEqualMarkup( getModelData( model ), '<paragraph>foo XX[]r baz</paragraph>' );
  375. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  376. const expectedRange = model.createRange(
  377. model.createPositionAt( firstParagraph, 4 ),
  378. model.createPositionAt( firstParagraph, 7 )
  379. );
  380. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  381. } );
  382. it( 'should retain marker on non-typing change at marker boundary (end)', () => {
  383. setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
  384. const firstParagraph = model.document.getRoot().getChild( 0 );
  385. addExceptionMarker( 4, 7, firstParagraph );
  386. model.change( writer => {
  387. editor.execute( 'delete', {
  388. selection: writer.createSelection( writer.createRange(
  389. writer.createPositionAt( firstParagraph, 5 ),
  390. writer.createPositionAt( firstParagraph, 7 )
  391. ) )
  392. } );
  393. editor.execute( 'input', {
  394. text: 'XX',
  395. range: writer.createRange( writer.createPositionAt( firstParagraph, 5 ) )
  396. } );
  397. } );
  398. assertEqualMarkup( getModelData( model ), '<paragraph>foo bXX[] baz</paragraph>' );
  399. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  400. const expectedRange = model.createRange(
  401. model.createPositionAt( firstParagraph, 4 ),
  402. model.createPositionAt( firstParagraph, 7 )
  403. );
  404. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  405. } );
  406. it( 'should not move collapsed marker to $graveyard', () => {
  407. setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
  408. const firstParagraph = model.document.getRoot().getChild( 0 );
  409. model.change( writer => {
  410. writer.addMarker( 'restrictedEditingException:1', {
  411. range: writer.createRange(
  412. writer.createPositionAt( firstParagraph, 4 ),
  413. writer.createPositionAt( firstParagraph, 5 )
  414. ),
  415. usingOperation: true,
  416. affectsData: true
  417. } );
  418. } );
  419. editor.execute( 'delete' );
  420. assertEqualMarkup( getModelData( model ), '<paragraph>foo []ar baz</paragraph>' );
  421. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  422. const expectedRange = model.createRange(
  423. model.createPositionAt( firstParagraph, 4 ),
  424. model.createPositionAt( firstParagraph, 4 )
  425. );
  426. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  427. } );
  428. } );
  429. describe( 'enforcing restrictions on deleteContent', () => {
  430. beforeEach( async () => {
  431. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingModeEditing ] } );
  432. model = editor.model;
  433. } );
  434. afterEach( async () => {
  435. await editor.destroy();
  436. } );
  437. it( 'should not allow to delete content outside restricted area', () => {
  438. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  439. const firstParagraph = model.document.getRoot().getChild( 0 );
  440. addExceptionMarker( 3, 9, firstParagraph );
  441. model.change( writer => {
  442. writer.setSelection( firstParagraph, 2 );
  443. } );
  444. model.deleteContent( model.document.selection );
  445. assertEqualMarkup( getModelData( model ), '<paragraph>fo[]o bar baz</paragraph>' );
  446. } );
  447. it( 'should trim deleted content to a exception marker (focus in marker)', () => {
  448. setModelData( model, '<paragraph>[]foofoo bar baz</paragraph>' );
  449. const firstParagraph = model.document.getRoot().getChild( 0 );
  450. addExceptionMarker( 3, 9, firstParagraph );
  451. model.change( writer => {
  452. const selection = writer.createSelection( writer.createRange(
  453. writer.createPositionAt( firstParagraph, 0 ),
  454. writer.createPositionAt( firstParagraph, 6 )
  455. ) );
  456. model.deleteContent( selection );
  457. } );
  458. assertEqualMarkup( getModelData( model ), '<paragraph>[]foo bar baz</paragraph>' );
  459. } );
  460. it( 'should trim deleted content to a exception marker (anchor in marker)', () => {
  461. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  462. const firstParagraph = model.document.getRoot().getChild( 0 );
  463. addExceptionMarker( 4, 7, firstParagraph );
  464. model.change( writer => {
  465. const selection = writer.createSelection( writer.createRange(
  466. writer.createPositionAt( firstParagraph, 5 ),
  467. writer.createPositionAt( firstParagraph, 8 )
  468. ) );
  469. model.deleteContent( selection );
  470. } );
  471. assertEqualMarkup( getModelData( model ), '<paragraph>[]foo b baz</paragraph>' );
  472. } );
  473. it( 'should trim deleted content to a exception marker and alter the selection argument (delete command integration)', () => {
  474. setModelData( model, '<paragraph>[]foofoo bar baz</paragraph>' );
  475. const firstParagraph = model.document.getRoot().getChild( 0 );
  476. addExceptionMarker( 3, 9, firstParagraph );
  477. model.change( writer => {
  478. writer.setSelection( firstParagraph, 6 );
  479. } );
  480. editor.execute( 'delete', { unit: 'word' } );
  481. assertEqualMarkup( getModelData( model ), '<paragraph>foo[] bar baz</paragraph>' );
  482. } );
  483. it( 'should work with document selection', () => {
  484. setModelData( model, '<paragraph>f[oo bar] baz</paragraph>' );
  485. const firstParagraph = model.document.getRoot().getChild( 0 );
  486. addExceptionMarker( 2, 'end', firstParagraph );
  487. model.change( () => {
  488. model.deleteContent( model.document.selection );
  489. } );
  490. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), '<paragraph>fo baz</paragraph>' );
  491. } );
  492. } );
  493. describe( 'enforcing restrictions on input command', () => {
  494. let firstParagraph;
  495. beforeEach( async () => {
  496. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingModeEditing ] } );
  497. model = editor.model;
  498. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  499. firstParagraph = model.document.getRoot().getChild( 0 );
  500. } );
  501. afterEach( async () => {
  502. await editor.destroy();
  503. } );
  504. it( 'should prevent changing text before exception marker', () => {
  505. addExceptionMarker( 4, 7, firstParagraph );
  506. model.change( writer => {
  507. writer.setSelection( firstParagraph, 5 );
  508. } );
  509. // Simulate native spell-check action.
  510. editor.execute( 'input', {
  511. text: 'xxxxxxx',
  512. range: model.createRange(
  513. model.createPositionAt( firstParagraph, 0 ),
  514. model.createPositionAt( firstParagraph, 7 )
  515. )
  516. } );
  517. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  518. } );
  519. it( 'should prevent changing text before exception marker', () => {
  520. addExceptionMarker( 4, 7, firstParagraph );
  521. model.change( writer => {
  522. writer.setSelection( firstParagraph, 5 );
  523. } );
  524. // Simulate native spell-check action.
  525. editor.execute( 'input', {
  526. text: 'xxxxxxx',
  527. range: model.createRange(
  528. model.createPositionAt( firstParagraph, 4 ),
  529. model.createPositionAt( firstParagraph, 9 )
  530. )
  531. } );
  532. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  533. } );
  534. it( 'should prevent changing text before (change crossing different markers)', () => {
  535. addExceptionMarker( 0, 4, firstParagraph );
  536. addExceptionMarker( 7, 9, firstParagraph, 2 );
  537. model.change( writer => {
  538. writer.setSelection( firstParagraph, 2 );
  539. } );
  540. // Simulate native spell-check action.
  541. editor.execute( 'input', {
  542. text: 'xxxxxxx',
  543. range: model.createRange(
  544. model.createPositionAt( firstParagraph, 2 ),
  545. model.createPositionAt( firstParagraph, 8 )
  546. )
  547. } );
  548. assertEqualMarkup( getModelData( model ), '<paragraph>fo[]o bar baz</paragraph>' );
  549. } );
  550. it( 'should allow changing text inside single marker', () => {
  551. addExceptionMarker( 0, 9, firstParagraph );
  552. model.change( writer => {
  553. writer.setSelection( firstParagraph, 2 );
  554. } );
  555. // Simulate native spell-check action.
  556. editor.execute( 'input', {
  557. text: 'xxxxxxx',
  558. range: model.createRange(
  559. model.createPositionAt( firstParagraph, 2 ),
  560. model.createPositionAt( firstParagraph, 8 )
  561. )
  562. } );
  563. assertEqualMarkup( getModelData( model ), '<paragraph>foxxxxxxx[]baz</paragraph>' );
  564. } );
  565. } );
  566. describe( 'clipboard', () => {
  567. let viewDoc;
  568. beforeEach( async () => {
  569. editor = await VirtualTestEditor.create( {
  570. plugins: [ Paragraph, BoldEditing, ItalicEditing, StrikethroughEditing, BlockQuoteEditing, LinkEditing, Typing, Clipboard,
  571. RestrictedEditingModeEditing
  572. ]
  573. } );
  574. model = editor.model;
  575. viewDoc = editor.editing.view.document;
  576. } );
  577. afterEach( async () => {
  578. await editor.destroy();
  579. } );
  580. describe( 'cut', () => {
  581. it( 'should be blocked outside exception markers', () => {
  582. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  583. const spy = sinon.spy();
  584. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  585. viewDoc.fire( 'clipboardOutput', {
  586. content: {
  587. isEmpty: true
  588. },
  589. method: 'cut'
  590. } );
  591. sinon.assert.notCalled( spy );
  592. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  593. } );
  594. it( 'should cut selected content inside exception marker (selection inside marker)', () => {
  595. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  596. const firstParagraph = model.document.getRoot().getChild( 0 );
  597. addExceptionMarker( 4, 7, firstParagraph );
  598. viewDoc.fire( 'clipboardOutput', {
  599. content: {
  600. isEmpty: true
  601. },
  602. method: 'cut'
  603. } );
  604. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]r baz</paragraph>' );
  605. } );
  606. it( 'should cut selected content inside exception marker (selection touching marker start)', () => {
  607. setModelData( model, '<paragraph>foo [ba]r baz</paragraph>' );
  608. const firstParagraph = model.document.getRoot().getChild( 0 );
  609. addExceptionMarker( 4, 7, firstParagraph );
  610. viewDoc.fire( 'clipboardOutput', {
  611. content: {
  612. isEmpty: true
  613. },
  614. method: 'cut'
  615. } );
  616. assertEqualMarkup( getModelData( model ), '<paragraph>foo []r baz</paragraph>' );
  617. } );
  618. it( 'should cut selected content inside exception marker (selection touching marker end)', () => {
  619. setModelData( model, '<paragraph>foo b[ar] baz</paragraph>' );
  620. const firstParagraph = model.document.getRoot().getChild( 0 );
  621. addExceptionMarker( 4, 7, firstParagraph );
  622. viewDoc.fire( 'clipboardOutput', {
  623. content: {
  624. isEmpty: true
  625. },
  626. method: 'cut'
  627. } );
  628. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[] baz</paragraph>' );
  629. } );
  630. } );
  631. describe( 'copy', () => {
  632. it( 'should not be blocked outside exception markers', () => {
  633. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  634. const spy = sinon.spy();
  635. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  636. viewDoc.fire( 'clipboardOutput', {
  637. content: {
  638. isEmpty: true
  639. },
  640. method: 'copy'
  641. } );
  642. sinon.assert.calledOnce( spy );
  643. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  644. } );
  645. it( 'should not be blocked inside exception marker', () => {
  646. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  647. const firstParagraph = model.document.getRoot().getChild( 0 );
  648. const spy = sinon.spy();
  649. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  650. model.change( writer => {
  651. writer.addMarker( 'restrictedEditingException:1', {
  652. range: writer.createRange(
  653. writer.createPositionAt( firstParagraph, 4 ),
  654. writer.createPositionAt( firstParagraph, 7 )
  655. ),
  656. usingOperation: true,
  657. affectsData: true
  658. } );
  659. } );
  660. model.change( writer => {
  661. writer.setSelection( firstParagraph, 5 );
  662. } );
  663. viewDoc.fire( 'clipboardOutput', {
  664. content: {
  665. isEmpty: true
  666. },
  667. method: 'copy'
  668. } );
  669. sinon.assert.calledOnce( spy );
  670. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  671. } );
  672. } );
  673. describe( 'paste', () => {
  674. beforeEach( () => {
  675. // Required when testing without DOM using VirtualTestEditor - Clipboard feature scrolls after paste event.
  676. sinon.stub( editor.editing.view, 'scrollToTheSelection' );
  677. } );
  678. it( 'should be blocked outside exception markers (collapsed selection)', () => {
  679. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  680. const spy = sinon.spy();
  681. viewDoc.on( 'clipboardInput', spy, { priority: 'high' } );
  682. viewDoc.fire( 'clipboardInput', {
  683. dataTransfer: {
  684. getData: sinon.spy()
  685. }
  686. } );
  687. sinon.assert.notCalled( spy );
  688. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  689. } );
  690. it( 'should be blocked outside exception markers (non-collapsed selection)', () => {
  691. setModelData( model, '<paragraph>[foo bar baz]</paragraph>' );
  692. const spy = sinon.spy();
  693. viewDoc.on( 'clipboardInput', spy, { priority: 'high' } );
  694. viewDoc.fire( 'clipboardInput', {
  695. dataTransfer: {
  696. getData: sinon.spy()
  697. }
  698. } );
  699. sinon.assert.notCalled( spy );
  700. assertEqualMarkup( getModelData( model ), '<paragraph>[foo bar baz]</paragraph>' );
  701. } );
  702. it( 'should be blocked outside exception markers (non-collapsed selection, starts inside exception marker)', () => {
  703. setModelData( model, '<paragraph>foo b[ar baz]</paragraph>' );
  704. const spy = sinon.spy();
  705. viewDoc.on( 'clipboardInput', spy, { priority: 'high' } );
  706. viewDoc.fire( 'clipboardInput', {
  707. dataTransfer: {
  708. getData: sinon.spy()
  709. }
  710. } );
  711. sinon.assert.notCalled( spy );
  712. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[ar baz]</paragraph>' );
  713. } );
  714. it( 'should be blocked outside exception markers (non-collapsed selection, ends inside exception marker)', () => {
  715. setModelData( model, '<paragraph>[foo ba]r baz</paragraph>' );
  716. const spy = sinon.spy();
  717. viewDoc.on( 'clipboardInput', spy, { priority: 'high' } );
  718. viewDoc.fire( 'clipboardInput', {
  719. dataTransfer: {
  720. getData: sinon.spy()
  721. }
  722. } );
  723. sinon.assert.notCalled( spy );
  724. assertEqualMarkup( getModelData( model ), '<paragraph>[foo ba]r baz</paragraph>' );
  725. } );
  726. describe( 'collapsed selection', () => {
  727. it( 'should paste text inside exception marker', () => {
  728. setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
  729. const firstParagraph = model.document.getRoot().getChild( 0 );
  730. addExceptionMarker( 4, 7, firstParagraph );
  731. viewDoc.fire( 'clipboardInput', {
  732. dataTransfer: createDataTransfer( { 'text/html': '<p>XXX</p>', 'text/plain': 'XXX' } )
  733. } );
  734. assertEqualMarkup( getModelData( model ), '<paragraph>foo bXXX[]ar baz</paragraph>' );
  735. assertMarkerRangePaths( [ 0, 4 ], [ 0, 10 ] );
  736. } );
  737. it( 'should paste allowed text attributes inside exception marker', () => {
  738. setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
  739. const firstParagraph = model.document.getRoot().getChild( 0 );
  740. addExceptionMarker( 4, 7, firstParagraph );
  741. viewDoc.fire( 'clipboardInput', {
  742. dataTransfer: createDataTransfer( {
  743. 'text/html': '<p><a href="foo"><b><i>XXX</i></b></a></p>',
  744. 'text/plain': 'XXX'
  745. } )
  746. } );
  747. assertEqualMarkup( getModelData( model ),
  748. '<paragraph>foo b<$text bold="true" italic="true" linkHref="foo">XXX[]</$text>ar baz</paragraph>'
  749. );
  750. assertMarkerRangePaths( [ 0, 4 ], [ 0, 10 ] );
  751. } );
  752. it( 'should not allow to paste disallowed text attributes inside exception marker', () => {
  753. setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
  754. const firstParagraph = model.document.getRoot().getChild( 0 );
  755. addExceptionMarker( 4, 7, firstParagraph );
  756. viewDoc.fire( 'clipboardInput', {
  757. dataTransfer: createDataTransfer( { 'text/html': '<p><s>XXX</s></p>', 'text/plain': 'XXX' } )
  758. } );
  759. assertEqualMarkup( getModelData( model ), '<paragraph>foo bXXX[]ar baz</paragraph>' );
  760. assertMarkerRangePaths( [ 0, 4 ], [ 0, 10 ] );
  761. } );
  762. it( 'should filter out disallowed attributes from other text attributes when pasting inside exception marker', () => {
  763. setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
  764. const firstParagraph = model.document.getRoot().getChild( 0 );
  765. addExceptionMarker( 4, 7, firstParagraph );
  766. viewDoc.fire( 'clipboardInput', {
  767. dataTransfer: createDataTransfer( { 'text/html': '<p><b><s><i>XXX</i></s></b></p>', 'text/plain': 'XXX' } )
  768. } );
  769. assertEqualMarkup(
  770. getModelData( model ),
  771. '<paragraph>foo b<$text bold="true" italic="true">XXX[]</$text>ar baz</paragraph>'
  772. );
  773. assertMarkerRangePaths( [ 0, 4 ], [ 0, 10 ] );
  774. } );
  775. it( 'should not allow pasting block elements other then paragraph', () => {
  776. setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
  777. const firstParagraph = model.document.getRoot().getChild( 0 );
  778. addExceptionMarker( 4, 7, firstParagraph );
  779. viewDoc.fire( 'clipboardInput', {
  780. dataTransfer: createDataTransfer( { 'text/html': '<blockquote><p>XXX</p></blockquote>', 'text/plain': 'XXX' } )
  781. } );
  782. assertEqualMarkup( getModelData( model ), '<paragraph>foo bXXX[]ar baz</paragraph>' );
  783. assertMarkerRangePaths( [ 0, 4 ], [ 0, 10 ] );
  784. } );
  785. } );
  786. describe( 'non-collapsed selection', () => {
  787. it( 'should paste text inside exception marker', () => {
  788. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  789. const firstParagraph = model.document.getRoot().getChild( 0 );
  790. addExceptionMarker( 4, 7, firstParagraph );
  791. viewDoc.fire( 'clipboardInput', {
  792. dataTransfer: createDataTransfer( { 'text/html': '<p>XXX</p>', 'text/plain': 'XXX' } )
  793. } );
  794. assertEqualMarkup( getModelData( model ), '<paragraph>foo bXXX[]r baz</paragraph>' );
  795. assertMarkerRangePaths( [ 0, 4 ], [ 0, 9 ] );
  796. } );
  797. it( 'should paste allowed text attributes inside exception marker', () => {
  798. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  799. const firstParagraph = model.document.getRoot().getChild( 0 );
  800. addExceptionMarker( 4, 7, firstParagraph );
  801. viewDoc.fire( 'clipboardInput', {
  802. dataTransfer: createDataTransfer( { 'text/html': '<p><b>XXX</b></p>', 'text/plain': 'XXX' } )
  803. } );
  804. assertEqualMarkup( getModelData( model ), '<paragraph>foo b<$text bold="true">XXX[]</$text>r baz</paragraph>' );
  805. assertMarkerRangePaths( [ 0, 4 ], [ 0, 9 ] );
  806. } );
  807. it( 'should not allow to paste disallowed text attributes inside exception marker', () => {
  808. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  809. const firstParagraph = model.document.getRoot().getChild( 0 );
  810. addExceptionMarker( 4, 7, firstParagraph );
  811. viewDoc.fire( 'clipboardInput', {
  812. dataTransfer: createDataTransfer( { 'text/html': '<p><s>XXX</s></p>', 'text/plain': 'XXX' } )
  813. } );
  814. assertEqualMarkup( getModelData( model ), '<paragraph>foo bXXX[]r baz</paragraph>' );
  815. assertMarkerRangePaths( [ 0, 4 ], [ 0, 9 ] );
  816. } );
  817. it( 'should filter out disallowed attributes from other text attributes when pasting inside exception marker', () => {
  818. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  819. const firstParagraph = model.document.getRoot().getChild( 0 );
  820. addExceptionMarker( 4, 7, firstParagraph );
  821. viewDoc.fire( 'clipboardInput', {
  822. dataTransfer: createDataTransfer( { 'text/html': '<p><b><s><i>XXX</i></s></b></p>', 'text/plain': 'XXX' } )
  823. } );
  824. assertEqualMarkup(
  825. getModelData( model ),
  826. '<paragraph>foo b<$text bold="true" italic="true">XXX[]</$text>r baz</paragraph>'
  827. );
  828. assertMarkerRangePaths( [ 0, 4 ], [ 0, 9 ] );
  829. } );
  830. } );
  831. } );
  832. } );
  833. describe( 'exception highlighting', () => {
  834. let view;
  835. beforeEach( async () => {
  836. editor = await VirtualTestEditor.create( {
  837. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  838. } );
  839. model = editor.model;
  840. view = editor.editing.view;
  841. } );
  842. afterEach( () => {
  843. return editor.destroy();
  844. } );
  845. it( 'should convert the highlight to a proper view classes', () => {
  846. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  847. const paragraph = model.document.getRoot().getChild( 0 );
  848. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  849. model.change( writer => {
  850. writer.addMarker( 'restrictedEditingException:1', {
  851. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  852. usingOperation: true,
  853. affectsData: true
  854. } );
  855. } );
  856. expect( getViewData( view ) ).to.equal(
  857. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{a}r</span> baz</p>'
  858. );
  859. } );
  860. it( 'should remove classes when selection is moved away from an exception', () => {
  861. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  862. const paragraph = model.document.getRoot().getChild( 0 );
  863. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  864. model.change( writer => {
  865. writer.addMarker( 'restrictedEditingException:1', {
  866. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  867. usingOperation: true,
  868. affectsData: true
  869. } );
  870. } );
  871. expect( getViewData( view ) ).to.equal(
  872. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{a}r</span> baz</p>'
  873. );
  874. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  875. expect( getViewData( view ) ).to.equal(
  876. '<p>{}foo <span class="restricted-editing-exception">bar</span> baz</p>'
  877. );
  878. } );
  879. it( 'should work correctly when selection is moved inside an exception', () => {
  880. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  881. const paragraph = model.document.getRoot().getChild( 0 );
  882. // <paragraph>[]foo <$marker>bar</$marker> baz</paragraph>
  883. model.change( writer => {
  884. writer.addMarker( 'restrictedEditingException:1', {
  885. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  886. usingOperation: true,
  887. affectsData: true
  888. } );
  889. } );
  890. expect( getViewData( view ) ).to.equal(
  891. '<p>{}foo <span class="restricted-editing-exception">bar</span> baz</p>'
  892. );
  893. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 6 ) );
  894. expect( getViewData( view ) ).to.equal(
  895. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">ba{}r</span> baz</p>'
  896. );
  897. } );
  898. describe( 'editing downcast conversion integration', () => {
  899. it( 'works for the #insert event', () => {
  900. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  901. const paragraph = model.document.getRoot().getChild( 0 );
  902. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  903. model.change( writer => {
  904. writer.addMarker( 'restrictedEditingException:1', {
  905. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  906. usingOperation: true,
  907. affectsData: true
  908. } );
  909. } );
  910. model.change( writer => {
  911. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  912. } );
  913. expect( getViewData( view ) ).to.equal(
  914. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">bFOO{a}r</span> baz</p>'
  915. );
  916. } );
  917. it( 'works for the #remove event', () => {
  918. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  919. const paragraph = model.document.getRoot().getChild( 0 );
  920. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  921. model.change( writer => {
  922. writer.addMarker( 'restrictedEditingException:1', {
  923. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  924. usingOperation: true,
  925. affectsData: true
  926. } );
  927. } );
  928. model.change( writer => {
  929. writer.remove( writer.createRange(
  930. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 ),
  931. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 6 )
  932. ) );
  933. } );
  934. expect( getViewData( view ) ).to.equal(
  935. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{}r</span> baz</p>'
  936. );
  937. } );
  938. it( 'works for the #attribute event', () => {
  939. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  940. const paragraph = model.document.getRoot().getChild( 0 );
  941. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  942. model.change( writer => {
  943. writer.addMarker( 'restrictedEditingException:1', {
  944. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  945. usingOperation: true,
  946. affectsData: true
  947. } );
  948. } );
  949. model.change( writer => {
  950. writer.setAttribute( 'bold', true, writer.createRange(
  951. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  952. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  953. );
  954. } );
  955. expect( getViewData( view ) ).to.equal(
  956. '<p>foo ' +
  957. '<span class="restricted-editing-exception restricted-editing-exception_selected">' +
  958. '<strong>b{a</strong>' +
  959. '}r</span>' +
  960. ' baz</p>'
  961. );
  962. } );
  963. it( 'works for the #selection event', () => {
  964. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  965. const paragraph = model.document.getRoot().getChild( 0 );
  966. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  967. model.change( writer => {
  968. writer.addMarker( 'restrictedEditingException:1', {
  969. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  970. usingOperation: true,
  971. affectsData: true
  972. } );
  973. } );
  974. model.change( writer => {
  975. writer.setSelection( writer.createRange(
  976. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  977. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  978. );
  979. } );
  980. expect( getViewData( view ) ).to.equal(
  981. '<p>foo {<span class="restricted-editing-exception restricted-editing-exception_selected">ba}r</span> baz</p>'
  982. );
  983. } );
  984. it( 'works for the addMarker and removeMarker events', () => {
  985. editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
  986. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  987. const paragraph = model.document.getRoot().getChild( 0 );
  988. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  989. model.change( writer => {
  990. writer.addMarker( 'restrictedEditingException:1', {
  991. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  992. usingOperation: true,
  993. affectsData: true
  994. } );
  995. } );
  996. model.change( writer => {
  997. const range = writer.createRange(
  998. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  999. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  1000. );
  1001. writer.addMarker( 'fooMarker', { range, usingOperation: true } );
  1002. } );
  1003. expect( getViewData( view ) ).to.equal(
  1004. '<p>' +
  1005. '<span>foo </span>' +
  1006. '<span class="restricted-editing-exception restricted-editing-exception_selected">' +
  1007. '<span>b</span>{a}r' +
  1008. '</span>' +
  1009. ' baz</p>'
  1010. );
  1011. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  1012. expect( getViewData( view ) ).to.equal(
  1013. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{a}r</span> baz</p>'
  1014. );
  1015. } );
  1016. } );
  1017. } );
  1018. describe( 'exception cycling with the keyboard', () => {
  1019. let view, domEvtDataStub;
  1020. beforeEach( async () => {
  1021. editor = await VirtualTestEditor.create( {
  1022. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  1023. } );
  1024. model = editor.model;
  1025. view = editor.editing.view;
  1026. domEvtDataStub = {
  1027. keyCode: getCode( 'Tab' ),
  1028. preventDefault: sinon.spy(),
  1029. stopPropagation: sinon.spy()
  1030. };
  1031. sinon.spy( editor, 'execute' );
  1032. } );
  1033. afterEach( () => {
  1034. return editor.destroy();
  1035. } );
  1036. it( 'should move to the closest next exception on tab key', () => {
  1037. setModelData( model, '<paragraph>[]foo bar baz qux</paragraph>' );
  1038. const paragraph = model.document.getRoot().getChild( 0 );
  1039. // <paragraph>[]foo <marker>bar</marker> baz qux</paragraph>
  1040. model.change( writer => {
  1041. writer.addMarker( 'restrictedEditingException:1', {
  1042. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  1043. usingOperation: true,
  1044. affectsData: true
  1045. } );
  1046. } );
  1047. // <paragraph>[]foo <marker>bar</marker> <marker>baz</marker≥ qux</paragraph>
  1048. model.change( writer => {
  1049. writer.addMarker( 'restrictedEditingException:2', {
  1050. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  1051. usingOperation: true,
  1052. affectsData: true
  1053. } );
  1054. } );
  1055. view.document.fire( 'keydown', domEvtDataStub );
  1056. sinon.assert.calledOnce( editor.execute );
  1057. sinon.assert.calledWithExactly( editor.execute, 'goToNextRestrictedEditingException' );
  1058. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  1059. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  1060. } );
  1061. it( 'should let the focus go outside the editor on tab key when in the last exception', () => {
  1062. setModelData( model, '<paragraph>foo qux[]</paragraph>' );
  1063. const paragraph = model.document.getRoot().getChild( 0 );
  1064. // <paragraph><marker>foo</marker> qux[]</paragraph>
  1065. model.change( writer => {
  1066. writer.addMarker( 'restrictedEditingException:1', {
  1067. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 3 ) ),
  1068. usingOperation: true,
  1069. affectsData: true
  1070. } );
  1071. } );
  1072. view.document.fire( 'keydown', domEvtDataStub );
  1073. sinon.assert.notCalled( editor.execute );
  1074. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  1075. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  1076. } );
  1077. it( 'should move to the closest previous exception on shift+tab key', () => {
  1078. setModelData( model, '<paragraph>foo bar baz qux[]</paragraph>' );
  1079. const paragraph = model.document.getRoot().getChild( 0 );
  1080. // <paragraph>foo <marker>bar</marker> baz qux[]</paragraph>
  1081. model.change( writer => {
  1082. writer.addMarker( 'restrictedEditingException:1', {
  1083. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  1084. usingOperation: true,
  1085. affectsData: true
  1086. } );
  1087. } );
  1088. // <paragraph>foo <marker>bar</marker> <marker>baz</marker≥ qux[]</paragraph>
  1089. model.change( writer => {
  1090. writer.addMarker( 'restrictedEditingException:2', {
  1091. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  1092. usingOperation: true,
  1093. affectsData: true
  1094. } );
  1095. } );
  1096. domEvtDataStub.keyCode += getCode( 'Shift' );
  1097. view.document.fire( 'keydown', domEvtDataStub );
  1098. sinon.assert.calledOnce( editor.execute );
  1099. sinon.assert.calledWithExactly( editor.execute, 'goToPreviousRestrictedEditingException' );
  1100. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  1101. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  1102. } );
  1103. it( 'should let the focus go outside the editor on shift+tab when in the first exception', () => {
  1104. setModelData( model, '<paragraph>[]foo qux</paragraph>' );
  1105. const paragraph = model.document.getRoot().getChild( 0 );
  1106. // <paragraph>[]foo <marker>qux</marker></paragraph>
  1107. model.change( writer => {
  1108. writer.addMarker( 'restrictedEditingException:1', {
  1109. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  1110. usingOperation: true,
  1111. affectsData: true
  1112. } );
  1113. } );
  1114. domEvtDataStub.keyCode += getCode( 'Shift' );
  1115. view.document.fire( 'keydown', domEvtDataStub );
  1116. sinon.assert.notCalled( editor.execute );
  1117. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  1118. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  1119. } );
  1120. } );
  1121. // Helper method that creates an exception marker inside given parent.
  1122. // Marker range is set to given position offsets (start, end).
  1123. function addExceptionMarker( startOffset, endOffset = startOffset, parent, id = 1 ) {
  1124. model.change( writer => {
  1125. writer.addMarker( `restrictedEditingException:${ id }`, {
  1126. range: writer.createRange(
  1127. writer.createPositionAt( parent, startOffset ),
  1128. writer.createPositionAt( parent, endOffset )
  1129. ),
  1130. usingOperation: true,
  1131. affectsData: true
  1132. } );
  1133. } );
  1134. }
  1135. function createDataTransfer( data ) {
  1136. return {
  1137. getData( type ) {
  1138. return data[ type ];
  1139. }
  1140. };
  1141. }
  1142. function assertMarkerRangePaths( startPath, endPath, markerId = 1 ) {
  1143. const marker = model.markers.get( `restrictedEditingException:${ markerId }` );
  1144. expect( marker.getStart().path ).to.deep.equal( startPath );
  1145. expect( marker.getEnd().path ).to.deep.equal( endPath );
  1146. }
  1147. } );