restrictededitingmodeediting.js 57 KB

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