restrictededitingmodeediting.js 49 KB

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