restrictededitingmodeediting.js 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  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 (native spell-check simulation)', () => {
  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>' +
  749. // The link attribute is removed from selection after pasting.
  750. // See https://github.com/ckeditor/ckeditor5/issues/6053.
  751. '<$text bold="true" italic="true">[]</$text>ar baz</paragraph>'
  752. );
  753. assertMarkerRangePaths( [ 0, 4 ], [ 0, 10 ] );
  754. } );
  755. it( 'should not allow to paste disallowed text attributes inside exception marker', () => {
  756. setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
  757. const firstParagraph = model.document.getRoot().getChild( 0 );
  758. addExceptionMarker( 4, 7, firstParagraph );
  759. viewDoc.fire( 'clipboardInput', {
  760. dataTransfer: createDataTransfer( { 'text/html': '<p><s>XXX</s></p>', 'text/plain': 'XXX' } )
  761. } );
  762. assertEqualMarkup( getModelData( model ), '<paragraph>foo bXXX[]ar baz</paragraph>' );
  763. assertMarkerRangePaths( [ 0, 4 ], [ 0, 10 ] );
  764. } );
  765. it( 'should filter out disallowed attributes from other text attributes when pasting inside exception marker', () => {
  766. setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
  767. const firstParagraph = model.document.getRoot().getChild( 0 );
  768. addExceptionMarker( 4, 7, firstParagraph );
  769. viewDoc.fire( 'clipboardInput', {
  770. dataTransfer: createDataTransfer( { 'text/html': '<p><b><s><i>XXX</i></s></b></p>', 'text/plain': 'XXX' } )
  771. } );
  772. assertEqualMarkup(
  773. getModelData( model ),
  774. '<paragraph>foo b<$text bold="true" italic="true">XXX[]</$text>ar baz</paragraph>'
  775. );
  776. assertMarkerRangePaths( [ 0, 4 ], [ 0, 10 ] );
  777. } );
  778. it( 'should not allow pasting block elements other then paragraph', () => {
  779. setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
  780. const firstParagraph = model.document.getRoot().getChild( 0 );
  781. addExceptionMarker( 4, 7, firstParagraph );
  782. viewDoc.fire( 'clipboardInput', {
  783. dataTransfer: createDataTransfer( { 'text/html': '<blockquote><p>XXX</p></blockquote>', 'text/plain': 'XXX' } )
  784. } );
  785. assertEqualMarkup( getModelData( model ), '<paragraph>foo bXXX[]ar baz</paragraph>' );
  786. assertMarkerRangePaths( [ 0, 4 ], [ 0, 10 ] );
  787. } );
  788. } );
  789. describe( 'non-collapsed selection', () => {
  790. it( 'should paste text inside exception marker', () => {
  791. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  792. const firstParagraph = model.document.getRoot().getChild( 0 );
  793. addExceptionMarker( 4, 7, firstParagraph );
  794. viewDoc.fire( 'clipboardInput', {
  795. dataTransfer: createDataTransfer( { 'text/html': '<p>XXX</p>', 'text/plain': 'XXX' } )
  796. } );
  797. assertEqualMarkup( getModelData( model ), '<paragraph>foo bXXX[]r baz</paragraph>' );
  798. assertMarkerRangePaths( [ 0, 4 ], [ 0, 9 ] );
  799. } );
  800. it( 'should paste allowed text attributes inside exception marker', () => {
  801. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  802. const firstParagraph = model.document.getRoot().getChild( 0 );
  803. addExceptionMarker( 4, 7, firstParagraph );
  804. viewDoc.fire( 'clipboardInput', {
  805. dataTransfer: createDataTransfer( { 'text/html': '<p><b>XXX</b></p>', 'text/plain': 'XXX' } )
  806. } );
  807. assertEqualMarkup( getModelData( model ), '<paragraph>foo b<$text bold="true">XXX[]</$text>r baz</paragraph>' );
  808. assertMarkerRangePaths( [ 0, 4 ], [ 0, 9 ] );
  809. } );
  810. it( 'should not allow to paste disallowed text attributes inside exception marker', () => {
  811. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  812. const firstParagraph = model.document.getRoot().getChild( 0 );
  813. addExceptionMarker( 4, 7, firstParagraph );
  814. viewDoc.fire( 'clipboardInput', {
  815. dataTransfer: createDataTransfer( { 'text/html': '<p><s>XXX</s></p>', 'text/plain': 'XXX' } )
  816. } );
  817. assertEqualMarkup( getModelData( model ), '<paragraph>foo bXXX[]r baz</paragraph>' );
  818. assertMarkerRangePaths( [ 0, 4 ], [ 0, 9 ] );
  819. } );
  820. it( 'should filter out disallowed attributes from other text attributes when pasting inside exception marker', () => {
  821. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  822. const firstParagraph = model.document.getRoot().getChild( 0 );
  823. addExceptionMarker( 4, 7, firstParagraph );
  824. viewDoc.fire( 'clipboardInput', {
  825. dataTransfer: createDataTransfer( { 'text/html': '<p><b><s><i>XXX</i></s></b></p>', 'text/plain': 'XXX' } )
  826. } );
  827. assertEqualMarkup(
  828. getModelData( model ),
  829. '<paragraph>foo b<$text bold="true" italic="true">XXX[]</$text>r baz</paragraph>'
  830. );
  831. assertMarkerRangePaths( [ 0, 4 ], [ 0, 9 ] );
  832. } );
  833. } );
  834. } );
  835. } );
  836. describe( 'exception highlighting', () => {
  837. let view;
  838. beforeEach( async () => {
  839. editor = await VirtualTestEditor.create( {
  840. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  841. } );
  842. model = editor.model;
  843. view = editor.editing.view;
  844. } );
  845. afterEach( () => {
  846. return editor.destroy();
  847. } );
  848. it( 'should convert the highlight to a proper view classes', () => {
  849. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  850. const paragraph = model.document.getRoot().getChild( 0 );
  851. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  852. model.change( writer => {
  853. writer.addMarker( 'restrictedEditingException:1', {
  854. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  855. usingOperation: true,
  856. affectsData: true
  857. } );
  858. } );
  859. expect( getViewData( view ) ).to.equal(
  860. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{a}r</span> baz</p>'
  861. );
  862. } );
  863. it( 'should remove classes when selection is moved away from an exception', () => {
  864. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  865. const paragraph = model.document.getRoot().getChild( 0 );
  866. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  867. model.change( writer => {
  868. writer.addMarker( 'restrictedEditingException:1', {
  869. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  870. usingOperation: true,
  871. affectsData: true
  872. } );
  873. } );
  874. expect( getViewData( view ) ).to.equal(
  875. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{a}r</span> baz</p>'
  876. );
  877. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  878. expect( getViewData( view ) ).to.equal(
  879. '<p>{}foo <span class="restricted-editing-exception">bar</span> baz</p>'
  880. );
  881. } );
  882. it( 'should work correctly when selection is moved inside an exception', () => {
  883. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  884. const paragraph = model.document.getRoot().getChild( 0 );
  885. // <paragraph>[]foo <$marker>bar</$marker> baz</paragraph>
  886. model.change( writer => {
  887. writer.addMarker( 'restrictedEditingException:1', {
  888. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  889. usingOperation: true,
  890. affectsData: true
  891. } );
  892. } );
  893. expect( getViewData( view ) ).to.equal(
  894. '<p>{}foo <span class="restricted-editing-exception">bar</span> baz</p>'
  895. );
  896. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 6 ) );
  897. expect( getViewData( view ) ).to.equal(
  898. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">ba{}r</span> baz</p>'
  899. );
  900. } );
  901. describe( 'editing downcast conversion integration', () => {
  902. it( 'works for the #insert event', () => {
  903. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  904. const paragraph = model.document.getRoot().getChild( 0 );
  905. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  906. model.change( writer => {
  907. writer.addMarker( 'restrictedEditingException:1', {
  908. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  909. usingOperation: true,
  910. affectsData: true
  911. } );
  912. } );
  913. model.change( writer => {
  914. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  915. } );
  916. expect( getViewData( view ) ).to.equal(
  917. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">bFOO{a}r</span> baz</p>'
  918. );
  919. } );
  920. it( 'works for the #remove event', () => {
  921. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  922. const paragraph = model.document.getRoot().getChild( 0 );
  923. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  924. model.change( writer => {
  925. writer.addMarker( 'restrictedEditingException:1', {
  926. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  927. usingOperation: true,
  928. affectsData: true
  929. } );
  930. } );
  931. model.change( writer => {
  932. writer.remove( writer.createRange(
  933. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 ),
  934. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 6 )
  935. ) );
  936. } );
  937. expect( getViewData( view ) ).to.equal(
  938. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{}r</span> baz</p>'
  939. );
  940. } );
  941. it( 'works for the #attribute event', () => {
  942. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  943. const paragraph = model.document.getRoot().getChild( 0 );
  944. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  945. model.change( writer => {
  946. writer.addMarker( 'restrictedEditingException:1', {
  947. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  948. usingOperation: true,
  949. affectsData: true
  950. } );
  951. } );
  952. model.change( writer => {
  953. writer.setAttribute( 'bold', true, writer.createRange(
  954. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  955. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  956. );
  957. } );
  958. expect( getViewData( view ) ).to.equal(
  959. '<p>foo ' +
  960. '<span class="restricted-editing-exception restricted-editing-exception_selected">' +
  961. '<strong>b{a</strong>' +
  962. '}r</span>' +
  963. ' baz</p>'
  964. );
  965. } );
  966. it( 'works for the #selection event', () => {
  967. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  968. const paragraph = model.document.getRoot().getChild( 0 );
  969. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  970. model.change( writer => {
  971. writer.addMarker( 'restrictedEditingException:1', {
  972. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  973. usingOperation: true,
  974. affectsData: true
  975. } );
  976. } );
  977. model.change( writer => {
  978. writer.setSelection( writer.createRange(
  979. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  980. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  981. );
  982. } );
  983. expect( getViewData( view ) ).to.equal(
  984. '<p>foo {<span class="restricted-editing-exception restricted-editing-exception_selected">ba}r</span> baz</p>'
  985. );
  986. } );
  987. it( 'works for the addMarker and removeMarker events', () => {
  988. editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
  989. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  990. const paragraph = model.document.getRoot().getChild( 0 );
  991. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  992. model.change( writer => {
  993. writer.addMarker( 'restrictedEditingException:1', {
  994. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  995. usingOperation: true,
  996. affectsData: true
  997. } );
  998. } );
  999. model.change( writer => {
  1000. const range = writer.createRange(
  1001. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  1002. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  1003. );
  1004. writer.addMarker( 'fooMarker', { range, usingOperation: true } );
  1005. } );
  1006. expect( getViewData( view ) ).to.equal(
  1007. '<p>' +
  1008. '<span>foo </span>' +
  1009. '<span class="restricted-editing-exception restricted-editing-exception_selected">' +
  1010. '<span>b</span>{a}r' +
  1011. '</span>' +
  1012. ' baz</p>'
  1013. );
  1014. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  1015. expect( getViewData( view ) ).to.equal(
  1016. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{a}r</span> baz</p>'
  1017. );
  1018. } );
  1019. } );
  1020. } );
  1021. describe( 'exception cycling with the keyboard', () => {
  1022. let view, domEvtDataStub;
  1023. beforeEach( async () => {
  1024. editor = await VirtualTestEditor.create( {
  1025. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  1026. } );
  1027. model = editor.model;
  1028. view = editor.editing.view;
  1029. domEvtDataStub = {
  1030. keyCode: getCode( 'Tab' ),
  1031. preventDefault: sinon.spy(),
  1032. stopPropagation: sinon.spy()
  1033. };
  1034. sinon.spy( editor, 'execute' );
  1035. } );
  1036. afterEach( () => {
  1037. return editor.destroy();
  1038. } );
  1039. it( 'should move to the closest next exception on tab key', () => {
  1040. setModelData( model, '<paragraph>[]foo bar baz qux</paragraph>' );
  1041. const paragraph = model.document.getRoot().getChild( 0 );
  1042. // <paragraph>[]foo <marker>bar</marker> baz qux</paragraph>
  1043. model.change( writer => {
  1044. writer.addMarker( 'restrictedEditingException:1', {
  1045. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  1046. usingOperation: true,
  1047. affectsData: true
  1048. } );
  1049. } );
  1050. // <paragraph>[]foo <marker>bar</marker> <marker>baz</marker≥ qux</paragraph>
  1051. model.change( writer => {
  1052. writer.addMarker( 'restrictedEditingException:2', {
  1053. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  1054. usingOperation: true,
  1055. affectsData: true
  1056. } );
  1057. } );
  1058. view.document.fire( 'keydown', domEvtDataStub );
  1059. sinon.assert.calledOnce( editor.execute );
  1060. sinon.assert.calledWithExactly( editor.execute, 'goToNextRestrictedEditingException' );
  1061. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  1062. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  1063. } );
  1064. it( 'should let the focus go outside the editor on tab key when in the last exception', () => {
  1065. setModelData( model, '<paragraph>foo qux[]</paragraph>' );
  1066. const paragraph = model.document.getRoot().getChild( 0 );
  1067. // <paragraph><marker>foo</marker> qux[]</paragraph>
  1068. model.change( writer => {
  1069. writer.addMarker( 'restrictedEditingException:1', {
  1070. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 3 ) ),
  1071. usingOperation: true,
  1072. affectsData: true
  1073. } );
  1074. } );
  1075. view.document.fire( 'keydown', domEvtDataStub );
  1076. sinon.assert.notCalled( editor.execute );
  1077. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  1078. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  1079. } );
  1080. it( 'should move to the closest previous exception on shift+tab key', () => {
  1081. setModelData( model, '<paragraph>foo bar baz qux[]</paragraph>' );
  1082. const paragraph = model.document.getRoot().getChild( 0 );
  1083. // <paragraph>foo <marker>bar</marker> baz qux[]</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. // <paragraph>foo <marker>bar</marker> <marker>baz</marker≥ qux[]</paragraph>
  1092. model.change( writer => {
  1093. writer.addMarker( 'restrictedEditingException:2', {
  1094. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  1095. usingOperation: true,
  1096. affectsData: true
  1097. } );
  1098. } );
  1099. domEvtDataStub.keyCode += getCode( 'Shift' );
  1100. view.document.fire( 'keydown', domEvtDataStub );
  1101. sinon.assert.calledOnce( editor.execute );
  1102. sinon.assert.calledWithExactly( editor.execute, 'goToPreviousRestrictedEditingException' );
  1103. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  1104. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  1105. } );
  1106. it( 'should let the focus go outside the editor on shift+tab when in the first exception', () => {
  1107. setModelData( model, '<paragraph>[]foo qux</paragraph>' );
  1108. const paragraph = model.document.getRoot().getChild( 0 );
  1109. // <paragraph>[]foo <marker>qux</marker></paragraph>
  1110. model.change( writer => {
  1111. writer.addMarker( 'restrictedEditingException:1', {
  1112. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  1113. usingOperation: true,
  1114. affectsData: true
  1115. } );
  1116. } );
  1117. domEvtDataStub.keyCode += getCode( 'Shift' );
  1118. view.document.fire( 'keydown', domEvtDataStub );
  1119. sinon.assert.notCalled( editor.execute );
  1120. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  1121. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  1122. } );
  1123. } );
  1124. describe( 'custom keydown behaviour', () => {
  1125. let view, evtData;
  1126. beforeEach( async () => {
  1127. editor = await VirtualTestEditor.create( {
  1128. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  1129. } );
  1130. model = editor.model;
  1131. view = editor.editing.view;
  1132. } );
  1133. afterEach( () => {
  1134. return editor.destroy();
  1135. } );
  1136. describe( 'Ctrl+A handler', () => {
  1137. beforeEach( async () => {
  1138. evtData = {
  1139. keyCode: getCode( 'A' ),
  1140. ctrlKey: true,
  1141. preventDefault: sinon.spy(),
  1142. stopPropagation: sinon.spy()
  1143. };
  1144. } );
  1145. describe( 'collapsed selection', () => {
  1146. it( 'should select text only within an exception when selection is inside an exception', () => {
  1147. setModelData( model, '<paragraph>foo ba[]r baz</paragraph>' );
  1148. const paragraph = model.document.getRoot().getChild( 0 );
  1149. // <paragraph>foo <marker>ba[]r</marker> baz</paragraph>
  1150. addExceptionMarker( 4, 7, paragraph );
  1151. view.document.fire( 'keydown', evtData );
  1152. sinon.assert.calledOnce( evtData.preventDefault );
  1153. sinon.assert.calledOnce( evtData.stopPropagation );
  1154. expect( getModelData( model ) ).to.equal( '<paragraph>foo [bar] baz</paragraph>' );
  1155. } );
  1156. it( 'should select text only within an exception when selection is at the begining of an exception', () => {
  1157. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  1158. const paragraph = model.document.getRoot().getChild( 0 );
  1159. // <paragraph>foo <marker>[]bar</marker> baz</paragraph>
  1160. addExceptionMarker( 4, 7, paragraph );
  1161. view.document.fire( 'keydown', evtData );
  1162. sinon.assert.calledOnce( evtData.preventDefault );
  1163. sinon.assert.calledOnce( evtData.stopPropagation );
  1164. expect( getModelData( model ) ).to.equal( '<paragraph>foo [bar] baz</paragraph>' );
  1165. } );
  1166. it( 'should select text only within an exception when selection is at the end of an exception', () => {
  1167. setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
  1168. const paragraph = model.document.getRoot().getChild( 0 );
  1169. // <paragraph>foo <marker>bar[]</marker> baz</paragraph>
  1170. addExceptionMarker( 4, 7, paragraph );
  1171. view.document.fire( 'keydown', evtData );
  1172. sinon.assert.calledOnce( evtData.preventDefault );
  1173. sinon.assert.calledOnce( evtData.stopPropagation );
  1174. expect( getModelData( model ) ).to.equal( '<paragraph>foo [bar] baz</paragraph>' );
  1175. } );
  1176. it( 'should not change the selection if the caret is not inside an exception', () => {
  1177. setModelData( model, '<paragraph>foo ba[]r baz</paragraph>' );
  1178. // no markers
  1179. // <paragraph>foo ba[]r baz</paragraph>
  1180. view.document.fire( 'keydown', evtData );
  1181. sinon.assert.notCalled( evtData.preventDefault );
  1182. sinon.assert.notCalled( evtData.stopPropagation );
  1183. expect( getModelData( model ) ).to.equal( '<paragraph>foo ba[]r baz</paragraph>' );
  1184. } );
  1185. it( 'should not extend the selection outside an exception when press Ctrl+A second time', () => {
  1186. setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
  1187. const paragraph = model.document.getRoot().getChild( 0 );
  1188. // <paragraph>foo <marker>b[]ar</marker> baz</paragraph>
  1189. addExceptionMarker( 4, 7, paragraph );
  1190. view.document.fire( 'keydown', evtData );
  1191. view.document.fire( 'keydown', evtData );
  1192. sinon.assert.calledTwice( evtData.preventDefault );
  1193. sinon.assert.calledTwice( evtData.stopPropagation );
  1194. expect( getModelData( model ) ).to.equal( '<paragraph>foo [bar] baz</paragraph>' );
  1195. } );
  1196. } );
  1197. describe( 'non-collapsed selection', () => {
  1198. it( 'should select text within an exception when a whole selection range is inside an exception', () => {
  1199. setModelData( model, '<paragraph>fo[o ba]r baz</paragraph>' );
  1200. const paragraph = model.document.getRoot().getChild( 0 );
  1201. // <paragraph><marker>fo[o ba]r</marker> baz</paragraph>
  1202. addExceptionMarker( 0, 7, paragraph );
  1203. view.document.fire( 'keydown', evtData );
  1204. sinon.assert.calledOnce( evtData.preventDefault );
  1205. sinon.assert.calledOnce( evtData.stopPropagation );
  1206. expect( getModelData( model ) ).to.equal( '<paragraph>[foo bar] baz</paragraph>' );
  1207. } );
  1208. it( 'should select text within an exception when end of selection range is equal exception end', () => {
  1209. setModelData( model, '<paragraph>foo b[ar] baz</paragraph>' );
  1210. const paragraph = model.document.getRoot().getChild( 0 );
  1211. // <paragraph>foo <marker>b[ar]</marker> baz</paragraph>
  1212. addExceptionMarker( 4, 7, paragraph );
  1213. view.document.fire( 'keydown', evtData );
  1214. sinon.assert.calledOnce( evtData.preventDefault );
  1215. sinon.assert.calledOnce( evtData.stopPropagation );
  1216. expect( getModelData( model ) ).to.equal( '<paragraph>foo [bar] baz</paragraph>' );
  1217. } );
  1218. it( 'should select text within an exception when start of selection range is equal exception start', () => {
  1219. setModelData( model, '<paragraph>foo [ba]r baz</paragraph>' );
  1220. const paragraph = model.document.getRoot().getChild( 0 );
  1221. // <paragraph>foo <marker>[ba]r</marker> baz</paragraph>
  1222. addExceptionMarker( 4, 7, paragraph );
  1223. view.document.fire( 'keydown', evtData );
  1224. sinon.assert.calledOnce( evtData.preventDefault );
  1225. sinon.assert.calledOnce( evtData.stopPropagation );
  1226. expect( getModelData( model ) ).to.equal( '<paragraph>foo [bar] baz</paragraph>' );
  1227. } );
  1228. it( 'should not select text within an exception when a part of the selection range is outside an exception', () => {
  1229. setModelData( model, '<paragraph>fo[o ba]r baz</paragraph>' );
  1230. const paragraph = model.document.getRoot().getChild( 0 );
  1231. // <paragraph>fo[o <marker>ba]r</marker> baz</paragraph>
  1232. addExceptionMarker( 4, 7, paragraph );
  1233. view.document.fire( 'keydown', evtData );
  1234. sinon.assert.notCalled( evtData.preventDefault );
  1235. sinon.assert.notCalled( evtData.stopPropagation );
  1236. expect( getModelData( model ) ).to.equal( '<paragraph>fo[o ba]r baz</paragraph>' );
  1237. } );
  1238. it( 'should not extend the selection outside an exception when press Ctrl+A second time', () => {
  1239. setModelData( model, '<paragraph>foo [bar] baz</paragraph>' );
  1240. const paragraph = model.document.getRoot().getChild( 0 );
  1241. // <paragraph>foo <marker>[bar]</marker> baz</paragraph>
  1242. addExceptionMarker( 4, 7, paragraph );
  1243. view.document.fire( 'keydown', evtData );
  1244. view.document.fire( 'keydown', evtData );
  1245. sinon.assert.calledTwice( evtData.preventDefault );
  1246. sinon.assert.calledTwice( evtData.stopPropagation );
  1247. expect( getModelData( model ) ).to.equal( '<paragraph>foo [bar] baz</paragraph>' );
  1248. } );
  1249. } );
  1250. } );
  1251. } );
  1252. // Helper method that creates an exception marker inside given parent.
  1253. // Marker range is set to given position offsets (start, end).
  1254. function addExceptionMarker( startOffset, endOffset = startOffset, parent, id = 1 ) {
  1255. model.change( writer => {
  1256. writer.addMarker( `restrictedEditingException:${ id }`, {
  1257. range: writer.createRange(
  1258. writer.createPositionAt( parent, startOffset ),
  1259. writer.createPositionAt( parent, endOffset )
  1260. ),
  1261. usingOperation: true,
  1262. affectsData: true
  1263. } );
  1264. } );
  1265. }
  1266. function createDataTransfer( data ) {
  1267. return {
  1268. getData( type ) {
  1269. return data[ type ];
  1270. }
  1271. };
  1272. }
  1273. function assertMarkerRangePaths( startPath, endPath, markerId = 1 ) {
  1274. const marker = model.markers.get( `restrictedEditingException:${ markerId }` );
  1275. expect( marker.getStart().path ).to.deep.equal( startPath );
  1276. expect( marker.getEnd().path ).to.deep.equal( endPath );
  1277. }
  1278. } );