restrictededitingmodeediting.js 49 KB

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