8
0

restrictededitingmodeediting.js 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  6. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  8. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
  12. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  13. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  14. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  15. import RestrictedEditingModeEditing from './../src/restrictededitingmodeediting';
  16. import RestrictedEditingModeNavigationCommand from '../src/restrictededitingmodenavigationcommand';
  17. describe( 'RestrictedEditingModeEditing', () => {
  18. let editor, model;
  19. testUtils.createSinonSandbox();
  20. describe( 'plugin', () => {
  21. beforeEach( async () => {
  22. editor = await VirtualTestEditor.create( { plugins: [ RestrictedEditingModeEditing ] } );
  23. } );
  24. afterEach( async () => {
  25. await editor.destroy();
  26. } );
  27. it( 'should be named', () => {
  28. expect( RestrictedEditingModeEditing.pluginName ).to.equal( 'RestrictedEditingModeEditing' );
  29. } );
  30. it( 'should be loaded', () => {
  31. expect( editor.plugins.get( RestrictedEditingModeEditing ) ).to.be.instanceOf( RestrictedEditingModeEditing );
  32. } );
  33. it( 'root should have "ck-restricted-editing_mode_restricted" class', () => {
  34. for ( const root of editor.editing.view.document.roots ) {
  35. expect( root.hasClass( 'ck-restricted-editing_mode_restricted' ) ).to.be.true;
  36. }
  37. } );
  38. it( 'adds a "goToPreviousRestrictedEditingException" command', () => {
  39. expect( editor.commands.get( 'goToPreviousRestrictedEditingException' ) )
  40. .to.be.instanceOf( RestrictedEditingModeNavigationCommand );
  41. } );
  42. it( 'adds a "goToNextRestrictedEditingException" command', () => {
  43. expect(
  44. editor.commands.get( 'goToNextRestrictedEditingException' )
  45. ).to.be.instanceOf( RestrictedEditingModeNavigationCommand );
  46. } );
  47. } );
  48. describe( 'conversion', () => {
  49. beforeEach( async () => {
  50. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, RestrictedEditingModeEditing ] } );
  51. model = editor.model;
  52. } );
  53. afterEach( () => {
  54. return editor.destroy();
  55. } );
  56. describe( 'upcast', () => {
  57. it( 'should convert <span class="restricted-editing-exception"> to marker', () => {
  58. editor.setData( '<p>foo <span class="restricted-editing-exception">bar</span> baz</p>' );
  59. expect( model.markers.has( 'restrictedEditingException:1' ) ).to.be.true;
  60. const marker = model.markers.get( 'restrictedEditingException:1' );
  61. expect( marker.getStart().path ).to.deep.equal( [ 0, 4 ] );
  62. expect( marker.getEnd().path ).to.deep.equal( [ 0, 7 ] );
  63. } );
  64. it( 'should convert multiple <span class="restricted-editing-exception">', () => {
  65. editor.setData(
  66. '<p>foo <span class="restricted-editing-exception">bar</span> baz</p>' +
  67. '<p>ABCDEF<span class="restricted-editing-exception">GHIJK</span>LMNOPQRST</p>'
  68. );
  69. expect( model.markers.has( 'restrictedEditingException:1' ) ).to.be.true;
  70. expect( model.markers.has( 'restrictedEditingException:2' ) ).to.be.true;
  71. // Data for the first marker is the same as in previous tests so no need to test it again.
  72. const secondMarker = model.markers.get( 'restrictedEditingException:2' );
  73. expect( secondMarker.getStart().path ).to.deep.equal( [ 1, 6 ] );
  74. expect( secondMarker.getEnd().path ).to.deep.equal( [ 1, 11 ] );
  75. } );
  76. it( 'should not convert other <span> elements', () => {
  77. editor.setData( '<p>foo <span class="foo bar">bar</span> baz</p>' );
  78. expect( model.markers.has( 'restrictedEditingException:1' ) ).to.be.false;
  79. } );
  80. } );
  81. describe( 'downcast', () => {
  82. it( 'should convert model marker to <span>', () => {
  83. setModelData( model, '<paragraph>foo bar baz</paragraph>' );
  84. const paragraph = model.document.getRoot().getChild( 0 );
  85. model.change( writer => {
  86. writer.addMarker( 'restrictedEditingException:1', {
  87. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  88. usingOperation: true,
  89. affectsData: true
  90. } );
  91. } );
  92. const expectedView = '<p>foo <span class="restricted-editing-exception">bar</span> baz</p>';
  93. expect( editor.getData() ).to.equal( expectedView );
  94. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  95. } );
  96. it( 'should convert collapsed model marker to <span>', () => {
  97. setModelData( model, '<paragraph>foo bar baz</paragraph>' );
  98. const paragraph = model.document.getRoot().getChild( 0 );
  99. model.change( writer => {
  100. writer.addMarker( 'restrictedEditingException:1', {
  101. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 4 ) ),
  102. usingOperation: true,
  103. affectsData: true
  104. } );
  105. } );
  106. expect( editor.getData() ).to.equal( '<p>foo <span class="restricted-editing-exception"></span>bar baz</p>' );
  107. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  108. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_collapsed"></span>bar baz</p>'
  109. );
  110. } );
  111. it( 'converted <span> should be the outermost attribute element', () => {
  112. editor.conversion.for( 'downcast' ).attributeToElement( { model: 'bold', view: 'b' } );
  113. setModelData( model, '<paragraph><$text bold="true">foo bar baz</$text></paragraph>' );
  114. const paragraph = model.document.getRoot().getChild( 0 );
  115. model.change( writer => {
  116. writer.addMarker( 'restrictedEditingException:1', {
  117. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 'end' ) ),
  118. usingOperation: true,
  119. affectsData: true
  120. } );
  121. } );
  122. expect( editor.getData() ).to.equal(
  123. '<p><span class="restricted-editing-exception"><b>foo bar baz</b></span></p>'
  124. );
  125. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  126. '<p>' +
  127. '<span class="restricted-editing-exception restricted-editing-exception_selected"><b>foo bar baz</b></span>' +
  128. '</p>'
  129. );
  130. } );
  131. } );
  132. } );
  133. describe( 'editing behavior', () => {
  134. beforeEach( async () => {
  135. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingModeEditing ] } );
  136. model = editor.model;
  137. } );
  138. afterEach( () => {
  139. return editor.destroy();
  140. } );
  141. it( 'should keep markers in the view when editable region is edited', () => {
  142. setModelData( model,
  143. '<paragraph>foo bar baz</paragraph>' +
  144. '<paragraph>xxx y[]yy zzz</paragraph>'
  145. );
  146. const firstParagraph = model.document.getRoot().getChild( 0 );
  147. const secondParagraph = model.document.getRoot().getChild( 1 );
  148. model.change( writer => {
  149. writer.addMarker( 'restrictedEditingException:1', {
  150. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  151. usingOperation: true,
  152. affectsData: true
  153. } );
  154. writer.addMarker( 'restrictedEditingException:2', {
  155. range: writer.createRange(
  156. writer.createPositionAt( secondParagraph, 4 ),
  157. writer.createPositionAt( secondParagraph, 7 )
  158. ),
  159. usingOperation: true,
  160. affectsData: true
  161. } );
  162. } );
  163. model.change( writer => {
  164. model.insertContent( writer.createText( 'R', model.document.selection.getAttributes() ) );
  165. } );
  166. expect( editor.getData() ).to.equal(
  167. '<p>foo <span class="restricted-editing-exception">bar</span> baz</p>' +
  168. '<p>xxx <span class="restricted-editing-exception">yRyy</span> zzz</p>' );
  169. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  170. '<p>foo <span class="restricted-editing-exception">bar</span> baz</p>' +
  171. '<p>xxx <span class="restricted-editing-exception restricted-editing-exception_selected">yRyy</span> zzz</p>' );
  172. } );
  173. it( 'should block user typing outside exception markers', () => {
  174. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  175. editor.execute( 'input', { text: 'X' } );
  176. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  177. } );
  178. it( 'should not block user typing inside exception marker', () => {
  179. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  180. const firstParagraph = model.document.getRoot().getChild( 0 );
  181. model.change( writer => {
  182. writer.addMarker( 'restrictedEditingException:1', {
  183. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  184. usingOperation: true,
  185. affectsData: true
  186. } );
  187. } );
  188. model.change( writer => {
  189. writer.setSelection( firstParagraph, 5 );
  190. } );
  191. editor.execute( 'input', { text: 'X' } );
  192. assertEqualMarkup( getModelData( model ), '<paragraph>foo bX[]ar baz</paragraph>' );
  193. } );
  194. it( 'should extend maker when typing on the marker boundary (end)', () => {
  195. setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
  196. const firstParagraph = model.document.getRoot().getChild( 0 );
  197. model.change( writer => {
  198. writer.addMarker( 'restrictedEditingException:1', {
  199. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  200. usingOperation: true,
  201. affectsData: true
  202. } );
  203. } );
  204. editor.execute( 'input', { text: 'X' } );
  205. assertEqualMarkup( getModelData( model ), '<paragraph>foo barX[] baz</paragraph>' );
  206. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  207. const expectedRange = model.createRange(
  208. model.createPositionAt( firstParagraph, 4 ),
  209. model.createPositionAt( firstParagraph, 8 )
  210. );
  211. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  212. } );
  213. it( 'should extend marker when typing on the marker boundary (start)', () => {
  214. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  215. const firstParagraph = model.document.getRoot().getChild( 0 );
  216. model.change( writer => {
  217. writer.addMarker( 'restrictedEditingException:1', {
  218. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  219. usingOperation: true,
  220. affectsData: true
  221. } );
  222. } );
  223. editor.execute( 'input', { text: 'X' } );
  224. assertEqualMarkup( getModelData( model ), '<paragraph>foo X[]bar baz</paragraph>' );
  225. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  226. const expectedRange = model.createRange(
  227. model.createPositionAt( firstParagraph, 4 ),
  228. model.createPositionAt( firstParagraph, 8 )
  229. );
  230. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  231. } );
  232. it( 'should extend marker when typing on the marker boundary (collapsed marker)', () => {
  233. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  234. const firstParagraph = model.document.getRoot().getChild( 0 );
  235. model.change( writer => {
  236. writer.addMarker( 'restrictedEditingException:1', {
  237. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ) ),
  238. usingOperation: true,
  239. affectsData: true
  240. } );
  241. } );
  242. model.change( writer => {
  243. writer.setSelection( writer.createPositionAt( firstParagraph, 4 ) );
  244. } );
  245. editor.execute( 'input', { text: 'X' } );
  246. assertEqualMarkup( getModelData( model ), '<paragraph>foo X[]bar baz</paragraph>' );
  247. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  248. const expectedRange = model.createRange(
  249. model.createPositionAt( firstParagraph, 4 ),
  250. model.createPositionAt( firstParagraph, 5 )
  251. );
  252. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  253. } );
  254. it( 'should retain marker on non-typing change at the marker boundary (start)', () => {
  255. setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
  256. const firstParagraph = model.document.getRoot().getChild( 0 );
  257. addExceptionMarker( 4, 7, firstParagraph );
  258. model.change( writer => {
  259. editor.execute( 'delete', {
  260. selection: writer.createSelection( writer.createRange(
  261. writer.createPositionAt( firstParagraph, 4 ),
  262. writer.createPositionAt( firstParagraph, 6 )
  263. ) )
  264. } );
  265. editor.execute( 'input', {
  266. text: 'XX',
  267. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ) )
  268. } );
  269. } );
  270. assertEqualMarkup( getModelData( model ), '<paragraph>foo XX[]r baz</paragraph>' );
  271. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  272. const expectedRange = model.createRange(
  273. model.createPositionAt( firstParagraph, 4 ),
  274. model.createPositionAt( firstParagraph, 7 )
  275. );
  276. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  277. } );
  278. it( 'should retain marker on non-typing change at marker boundary (end)', () => {
  279. setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
  280. const firstParagraph = model.document.getRoot().getChild( 0 );
  281. addExceptionMarker( 4, 7, firstParagraph );
  282. model.change( writer => {
  283. editor.execute( 'delete', {
  284. selection: writer.createSelection( writer.createRange(
  285. writer.createPositionAt( firstParagraph, 5 ),
  286. writer.createPositionAt( firstParagraph, 7 )
  287. ) )
  288. } );
  289. editor.execute( 'input', {
  290. text: 'XX',
  291. range: writer.createRange( writer.createPositionAt( firstParagraph, 5 ) )
  292. } );
  293. } );
  294. assertEqualMarkup( getModelData( model ), '<paragraph>foo bXX[] baz</paragraph>' );
  295. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  296. const expectedRange = model.createRange(
  297. model.createPositionAt( firstParagraph, 4 ),
  298. model.createPositionAt( firstParagraph, 7 )
  299. );
  300. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  301. } );
  302. it( 'should not move collapsed marker to $graveyard', () => {
  303. setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
  304. const firstParagraph = model.document.getRoot().getChild( 0 );
  305. model.change( writer => {
  306. writer.addMarker( 'restrictedEditingException:1', {
  307. range: writer.createRange(
  308. writer.createPositionAt( firstParagraph, 4 ),
  309. writer.createPositionAt( firstParagraph, 5 )
  310. ),
  311. usingOperation: true,
  312. affectsData: true
  313. } );
  314. } );
  315. editor.execute( 'delete' );
  316. assertEqualMarkup( getModelData( model ), '<paragraph>foo []ar baz</paragraph>' );
  317. const markerRange = editor.model.markers.get( 'restrictedEditingException:1' ).getRange();
  318. const expectedRange = model.createRange(
  319. model.createPositionAt( firstParagraph, 4 ),
  320. model.createPositionAt( firstParagraph, 4 )
  321. );
  322. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  323. } );
  324. } );
  325. describe( 'enforcing restrictions on deleteContent', () => {
  326. beforeEach( async () => {
  327. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingModeEditing ] } );
  328. model = editor.model;
  329. } );
  330. afterEach( async () => {
  331. await editor.destroy();
  332. } );
  333. it( 'should not allow to delete content outside restricted area', () => {
  334. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  335. const firstParagraph = model.document.getRoot().getChild( 0 );
  336. addExceptionMarker( 3, 9, firstParagraph );
  337. model.change( writer => {
  338. writer.setSelection( firstParagraph, 2 );
  339. } );
  340. model.deleteContent( model.document.selection );
  341. assertEqualMarkup( getModelData( model ), '<paragraph>fo[]o bar baz</paragraph>' );
  342. } );
  343. it( 'should trim deleted content to a exception marker (focus in marker)', () => {
  344. setModelData( model, '<paragraph>[]foofoo bar baz</paragraph>' );
  345. const firstParagraph = model.document.getRoot().getChild( 0 );
  346. addExceptionMarker( 3, 9, firstParagraph );
  347. model.change( writer => {
  348. const selection = writer.createSelection( writer.createRange(
  349. writer.createPositionAt( firstParagraph, 0 ),
  350. writer.createPositionAt( firstParagraph, 6 )
  351. ) );
  352. model.deleteContent( selection );
  353. } );
  354. assertEqualMarkup( getModelData( model ), '<paragraph>[]foo bar baz</paragraph>' );
  355. } );
  356. it( 'should trim deleted content to a exception marker (anchor in marker)', () => {
  357. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  358. const firstParagraph = model.document.getRoot().getChild( 0 );
  359. addExceptionMarker( 4, 7, firstParagraph );
  360. model.change( writer => {
  361. const selection = writer.createSelection( writer.createRange(
  362. writer.createPositionAt( firstParagraph, 5 ),
  363. writer.createPositionAt( firstParagraph, 8 )
  364. ) );
  365. model.deleteContent( selection );
  366. } );
  367. assertEqualMarkup( getModelData( model ), '<paragraph>[]foo b baz</paragraph>' );
  368. } );
  369. it( 'should trim deleted content to a exception marker and alter the selection argument (delete command integration)', () => {
  370. setModelData( model, '<paragraph>[]foofoo bar baz</paragraph>' );
  371. const firstParagraph = model.document.getRoot().getChild( 0 );
  372. addExceptionMarker( 3, 9, firstParagraph );
  373. model.change( writer => {
  374. writer.setSelection( firstParagraph, 6 );
  375. } );
  376. editor.execute( 'delete', { unit: 'word' } );
  377. assertEqualMarkup( getModelData( model ), '<paragraph>foo[] bar baz</paragraph>' );
  378. } );
  379. it( 'should work with document selection', () => {
  380. setModelData( model, '<paragraph>f[oo bar] baz</paragraph>' );
  381. const firstParagraph = model.document.getRoot().getChild( 0 );
  382. addExceptionMarker( 2, 'end', firstParagraph );
  383. model.change( () => {
  384. model.deleteContent( model.document.selection );
  385. } );
  386. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), '<paragraph>fo baz</paragraph>' );
  387. } );
  388. } );
  389. describe( 'enforcing restrictions on input command', () => {
  390. let firstParagraph;
  391. beforeEach( async () => {
  392. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingModeEditing ] } );
  393. model = editor.model;
  394. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  395. firstParagraph = model.document.getRoot().getChild( 0 );
  396. } );
  397. afterEach( async () => {
  398. await editor.destroy();
  399. } );
  400. it( 'should prevent changing text before exception marker', () => {
  401. addExceptionMarker( 4, 7, firstParagraph );
  402. model.change( writer => {
  403. writer.setSelection( firstParagraph, 5 );
  404. } );
  405. // Simulate native spell-check action.
  406. editor.execute( 'input', {
  407. text: 'xxxxxxx',
  408. range: model.createRange(
  409. model.createPositionAt( firstParagraph, 0 ),
  410. model.createPositionAt( firstParagraph, 7 )
  411. )
  412. } );
  413. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  414. } );
  415. it( 'should prevent changing text before exception marker', () => {
  416. addExceptionMarker( 4, 7, firstParagraph );
  417. model.change( writer => {
  418. writer.setSelection( firstParagraph, 5 );
  419. } );
  420. // Simulate native spell-check action.
  421. editor.execute( 'input', {
  422. text: 'xxxxxxx',
  423. range: model.createRange(
  424. model.createPositionAt( firstParagraph, 4 ),
  425. model.createPositionAt( firstParagraph, 9 )
  426. )
  427. } );
  428. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  429. } );
  430. it( 'should prevent changing text before (change crossing different markers)', () => {
  431. addExceptionMarker( 0, 4, firstParagraph );
  432. addExceptionMarker( 7, 9, firstParagraph, 2 );
  433. model.change( writer => {
  434. writer.setSelection( firstParagraph, 2 );
  435. } );
  436. // Simulate native spell-check action.
  437. editor.execute( 'input', {
  438. text: 'xxxxxxx',
  439. range: model.createRange(
  440. model.createPositionAt( firstParagraph, 2 ),
  441. model.createPositionAt( firstParagraph, 8 )
  442. )
  443. } );
  444. assertEqualMarkup( getModelData( model ), '<paragraph>fo[]o bar baz</paragraph>' );
  445. } );
  446. it( 'should allow changing text inside single marker', () => {
  447. addExceptionMarker( 0, 9, firstParagraph );
  448. model.change( writer => {
  449. writer.setSelection( firstParagraph, 2 );
  450. } );
  451. // Simulate native spell-check action.
  452. editor.execute( 'input', {
  453. text: 'xxxxxxx',
  454. range: model.createRange(
  455. model.createPositionAt( firstParagraph, 2 ),
  456. model.createPositionAt( firstParagraph, 8 )
  457. )
  458. } );
  459. assertEqualMarkup( getModelData( model ), '<paragraph>foxxxxxxx[]baz</paragraph>' );
  460. } );
  461. } );
  462. describe( 'clipboard', () => {
  463. let viewDoc;
  464. beforeEach( async () => {
  465. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, Clipboard, RestrictedEditingModeEditing ] } );
  466. model = editor.model;
  467. viewDoc = editor.editing.view.document;
  468. } );
  469. afterEach( async () => {
  470. await editor.destroy();
  471. } );
  472. describe( 'cut', () => {
  473. it( 'should be blocked outside exception markers', () => {
  474. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  475. const spy = sinon.spy();
  476. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  477. viewDoc.fire( 'clipboardOutput', {
  478. content: {
  479. isEmpty: true
  480. },
  481. method: 'cut'
  482. } );
  483. sinon.assert.notCalled( spy );
  484. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  485. } );
  486. it( 'should cut selected content inside exception marker (selection inside marker)', () => {
  487. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  488. const firstParagraph = model.document.getRoot().getChild( 0 );
  489. addExceptionMarker( 4, 7, firstParagraph, 1 );
  490. viewDoc.fire( 'clipboardOutput', {
  491. content: {
  492. isEmpty: true
  493. },
  494. method: 'cut'
  495. } );
  496. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]r baz</paragraph>' );
  497. } );
  498. it( 'should cut selected content inside exception marker (selection touching marker start)', () => {
  499. setModelData( model, '<paragraph>foo [ba]r baz</paragraph>' );
  500. const firstParagraph = model.document.getRoot().getChild( 0 );
  501. addExceptionMarker( 4, 7, firstParagraph, 1 );
  502. viewDoc.fire( 'clipboardOutput', {
  503. content: {
  504. isEmpty: true
  505. },
  506. method: 'cut'
  507. } );
  508. assertEqualMarkup( getModelData( model ), '<paragraph>foo []r baz</paragraph>' );
  509. } );
  510. it( 'should cut selected content inside exception marker (selection touching marker end)', () => {
  511. setModelData( model, '<paragraph>foo b[ar] baz</paragraph>' );
  512. const firstParagraph = model.document.getRoot().getChild( 0 );
  513. addExceptionMarker( 4, 7, firstParagraph, 1 );
  514. viewDoc.fire( 'clipboardOutput', {
  515. content: {
  516. isEmpty: true
  517. },
  518. method: 'cut'
  519. } );
  520. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[] baz</paragraph>' );
  521. } );
  522. } );
  523. describe( 'copy', () => {
  524. it( 'should not be blocked outside exception markers', () => {
  525. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  526. const spy = sinon.spy();
  527. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  528. viewDoc.fire( 'clipboardOutput', {
  529. content: {
  530. isEmpty: true
  531. },
  532. method: 'copy'
  533. } );
  534. sinon.assert.calledOnce( spy );
  535. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  536. } );
  537. it( 'should not be blocked inside exception marker', () => {
  538. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  539. const firstParagraph = model.document.getRoot().getChild( 0 );
  540. const spy = sinon.spy();
  541. viewDoc.on( 'clipboardOutput', spy, { priority: 'high' } );
  542. model.change( writer => {
  543. writer.addMarker( 'restrictedEditingException:1', {
  544. range: writer.createRange(
  545. writer.createPositionAt( firstParagraph, 4 ),
  546. writer.createPositionAt( firstParagraph, 7 )
  547. ),
  548. usingOperation: true,
  549. affectsData: true
  550. } );
  551. } );
  552. model.change( writer => {
  553. writer.setSelection( firstParagraph, 5 );
  554. } );
  555. viewDoc.fire( 'clipboardOutput', {
  556. content: {
  557. isEmpty: true
  558. },
  559. method: 'copy'
  560. } );
  561. sinon.assert.calledOnce( spy );
  562. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  563. } );
  564. } );
  565. describe( 'paste', () => {
  566. it( 'should be blocked outside exception markers', () => {
  567. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  568. const spy = sinon.spy();
  569. viewDoc.on( 'clipboardInput', spy, { priority: 'high' } );
  570. viewDoc.fire( 'clipboardInput', {
  571. dataTransfer: {
  572. getData: sinon.spy()
  573. }
  574. } );
  575. sinon.assert.notCalled( spy );
  576. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  577. } );
  578. it( 'should be blocked inside exception marker', () => {
  579. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  580. const firstParagraph = model.document.getRoot().getChild( 0 );
  581. const spy = sinon.spy();
  582. viewDoc.on( 'clipboardInput', spy, { priority: 'high' } );
  583. model.change( writer => {
  584. writer.addMarker( 'restrictedEditingException:1', {
  585. range: writer.createRange(
  586. writer.createPositionAt( firstParagraph, 4 ),
  587. writer.createPositionAt( firstParagraph, 7 )
  588. ),
  589. usingOperation: true,
  590. affectsData: true
  591. } );
  592. } );
  593. model.change( writer => {
  594. writer.setSelection( firstParagraph, 5 );
  595. } );
  596. viewDoc.fire( 'clipboardInput', {
  597. dataTransfer: {
  598. getData: sinon.spy()
  599. }
  600. } );
  601. sinon.assert.notCalled( spy );
  602. assertEqualMarkup( getModelData( model ), '<paragraph>foo b[]ar baz</paragraph>' );
  603. } );
  604. } );
  605. } );
  606. describe( 'exception highlighting', () => {
  607. let view;
  608. beforeEach( async () => {
  609. editor = await VirtualTestEditor.create( {
  610. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  611. } );
  612. model = editor.model;
  613. view = editor.editing.view;
  614. } );
  615. afterEach( () => {
  616. return editor.destroy();
  617. } );
  618. it( 'should convert the highlight to a proper view classes', () => {
  619. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  620. const paragraph = model.document.getRoot().getChild( 0 );
  621. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  622. model.change( writer => {
  623. writer.addMarker( 'restrictedEditingException:1', {
  624. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  625. usingOperation: true,
  626. affectsData: true
  627. } );
  628. } );
  629. expect( getViewData( view ) ).to.equal(
  630. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{a}r</span> baz</p>'
  631. );
  632. } );
  633. it( 'should remove classes when selection is moved away from an exception', () => {
  634. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  635. const paragraph = model.document.getRoot().getChild( 0 );
  636. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  637. model.change( writer => {
  638. writer.addMarker( 'restrictedEditingException:1', {
  639. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  640. usingOperation: true,
  641. affectsData: true
  642. } );
  643. } );
  644. expect( getViewData( view ) ).to.equal(
  645. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{a}r</span> baz</p>'
  646. );
  647. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  648. expect( getViewData( view ) ).to.equal(
  649. '<p>{}foo <span class="restricted-editing-exception">bar</span> baz</p>'
  650. );
  651. } );
  652. it( 'should work correctly when selection is moved inside an exception', () => {
  653. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  654. const paragraph = model.document.getRoot().getChild( 0 );
  655. // <paragraph>[]foo <$marker>bar</$marker> baz</paragraph>
  656. model.change( writer => {
  657. writer.addMarker( 'restrictedEditingException:1', {
  658. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  659. usingOperation: true,
  660. affectsData: true
  661. } );
  662. } );
  663. expect( getViewData( view ) ).to.equal(
  664. '<p>{}foo <span class="restricted-editing-exception">bar</span> baz</p>'
  665. );
  666. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 6 ) );
  667. expect( getViewData( view ) ).to.equal(
  668. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">ba{}r</span> baz</p>'
  669. );
  670. } );
  671. describe( 'editing downcast conversion integration', () => {
  672. it( 'works for the #insert event', () => {
  673. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  674. const paragraph = model.document.getRoot().getChild( 0 );
  675. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  676. model.change( writer => {
  677. writer.addMarker( 'restrictedEditingException:1', {
  678. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  679. usingOperation: true,
  680. affectsData: true
  681. } );
  682. } );
  683. model.change( writer => {
  684. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  685. } );
  686. expect( getViewData( view ) ).to.equal(
  687. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">bFOO{a}r</span> baz</p>'
  688. );
  689. } );
  690. it( 'works for the #remove event', () => {
  691. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  692. const paragraph = model.document.getRoot().getChild( 0 );
  693. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  694. model.change( writer => {
  695. writer.addMarker( 'restrictedEditingException:1', {
  696. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  697. usingOperation: true,
  698. affectsData: true
  699. } );
  700. } );
  701. model.change( writer => {
  702. writer.remove( writer.createRange(
  703. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 ),
  704. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 6 )
  705. ) );
  706. } );
  707. expect( getViewData( view ) ).to.equal(
  708. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{}r</span> baz</p>'
  709. );
  710. } );
  711. it( 'works for the #attribute event', () => {
  712. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  713. const paragraph = model.document.getRoot().getChild( 0 );
  714. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  715. model.change( writer => {
  716. writer.addMarker( 'restrictedEditingException:1', {
  717. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  718. usingOperation: true,
  719. affectsData: true
  720. } );
  721. } );
  722. model.change( writer => {
  723. writer.setAttribute( 'bold', true, writer.createRange(
  724. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  725. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  726. );
  727. } );
  728. expect( getViewData( view ) ).to.equal(
  729. '<p>foo ' +
  730. '<span class="restricted-editing-exception restricted-editing-exception_selected">' +
  731. '<strong>b{a</strong>' +
  732. '}r</span>' +
  733. ' baz</p>'
  734. );
  735. } );
  736. it( 'works for the #selection event', () => {
  737. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  738. const paragraph = model.document.getRoot().getChild( 0 );
  739. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  740. model.change( writer => {
  741. writer.addMarker( 'restrictedEditingException:1', {
  742. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  743. usingOperation: true,
  744. affectsData: true
  745. } );
  746. } );
  747. model.change( writer => {
  748. writer.setSelection( writer.createRange(
  749. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  750. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  751. );
  752. } );
  753. expect( getViewData( view ) ).to.equal(
  754. '<p>foo {<span class="restricted-editing-exception restricted-editing-exception_selected">ba}r</span> baz</p>'
  755. );
  756. } );
  757. it( 'works for the addMarker and removeMarker events', () => {
  758. editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
  759. setModelData( model, '<paragraph>foo b[a]r baz</paragraph>' );
  760. const paragraph = model.document.getRoot().getChild( 0 );
  761. // <paragraph>foo <$marker>b[a]r</$marker> baz</paragraph>
  762. model.change( writer => {
  763. writer.addMarker( 'restrictedEditingException:1', {
  764. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  765. usingOperation: true,
  766. affectsData: true
  767. } );
  768. } );
  769. model.change( writer => {
  770. const range = writer.createRange(
  771. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  772. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  773. );
  774. writer.addMarker( 'fooMarker', { range, usingOperation: true } );
  775. } );
  776. expect( getViewData( view ) ).to.equal(
  777. '<p>' +
  778. '<span>foo </span>' +
  779. '<span class="restricted-editing-exception restricted-editing-exception_selected">' +
  780. '<span>b</span>{a}r' +
  781. '</span>' +
  782. ' baz</p>'
  783. );
  784. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  785. expect( getViewData( view ) ).to.equal(
  786. '<p>foo <span class="restricted-editing-exception restricted-editing-exception_selected">b{a}r</span> baz</p>'
  787. );
  788. } );
  789. } );
  790. } );
  791. describe( 'exception cycling with the keyboard', () => {
  792. let view, domEvtDataStub;
  793. beforeEach( async () => {
  794. editor = await VirtualTestEditor.create( {
  795. plugins: [ Paragraph, RestrictedEditingModeEditing, BoldEditing ]
  796. } );
  797. model = editor.model;
  798. view = editor.editing.view;
  799. domEvtDataStub = {
  800. keyCode: getCode( 'Tab' ),
  801. preventDefault: sinon.spy(),
  802. stopPropagation: sinon.spy()
  803. };
  804. sinon.spy( editor, 'execute' );
  805. } );
  806. afterEach( () => {
  807. return editor.destroy();
  808. } );
  809. it( 'should move to the closest next exception on tab key', () => {
  810. setModelData( model, '<paragraph>[]foo bar baz qux</paragraph>' );
  811. const paragraph = model.document.getRoot().getChild( 0 );
  812. // <paragraph>[]foo <marker>bar</marker> baz qux</paragraph>
  813. model.change( writer => {
  814. writer.addMarker( 'restrictedEditingException:1', {
  815. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  816. usingOperation: true,
  817. affectsData: true
  818. } );
  819. } );
  820. // <paragraph>[]foo <marker>bar</marker> <marker>baz</marker≥ qux</paragraph>
  821. model.change( writer => {
  822. writer.addMarker( 'restrictedEditingException:2', {
  823. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  824. usingOperation: true,
  825. affectsData: true
  826. } );
  827. } );
  828. view.document.fire( 'keydown', domEvtDataStub );
  829. sinon.assert.calledOnce( editor.execute );
  830. sinon.assert.calledWithExactly( editor.execute, 'goToNextRestrictedEditingException' );
  831. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  832. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  833. } );
  834. it( 'should not move to the closest next exception on tab key when there is none', () => {
  835. setModelData( model, '<paragraph>foo qux[]</paragraph>' );
  836. const paragraph = model.document.getRoot().getChild( 0 );
  837. // <paragraph><marker>foo</marker> qux[]</paragraph>
  838. model.change( writer => {
  839. writer.addMarker( 'restrictedEditingException:1', {
  840. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 3 ) ),
  841. usingOperation: true,
  842. affectsData: true
  843. } );
  844. } );
  845. view.document.fire( 'keydown', domEvtDataStub );
  846. sinon.assert.notCalled( editor.execute );
  847. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  848. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  849. } );
  850. it( 'should move to the closest previous exception on shift+tab key', () => {
  851. setModelData( model, '<paragraph>foo bar baz qux[]</paragraph>' );
  852. const paragraph = model.document.getRoot().getChild( 0 );
  853. // <paragraph>foo <marker>bar</marker> baz qux[]</paragraph>
  854. model.change( writer => {
  855. writer.addMarker( 'restrictedEditingException:1', {
  856. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  857. usingOperation: true,
  858. affectsData: true
  859. } );
  860. } );
  861. // <paragraph>foo <marker>bar</marker> <marker>baz</marker≥ qux[]</paragraph>
  862. model.change( writer => {
  863. writer.addMarker( 'restrictedEditingException:2', {
  864. range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
  865. usingOperation: true,
  866. affectsData: true
  867. } );
  868. } );
  869. domEvtDataStub.keyCode += getCode( 'Shift' );
  870. view.document.fire( 'keydown', domEvtDataStub );
  871. sinon.assert.calledOnce( editor.execute );
  872. sinon.assert.calledWithExactly( editor.execute, 'goToPreviousRestrictedEditingException' );
  873. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  874. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  875. } );
  876. it( 'should not move to the closest previous exception on shift+tab key when there is none', () => {
  877. setModelData( model, '<paragraph>[]foo qux</paragraph>' );
  878. const paragraph = model.document.getRoot().getChild( 0 );
  879. // <paragraph>[]foo <marker>qux</marker></paragraph>
  880. model.change( writer => {
  881. writer.addMarker( 'restrictedEditingException:1', {
  882. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  883. usingOperation: true,
  884. affectsData: true
  885. } );
  886. } );
  887. domEvtDataStub.keyCode += getCode( 'Shift' );
  888. view.document.fire( 'keydown', domEvtDataStub );
  889. sinon.assert.notCalled( editor.execute );
  890. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  891. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  892. } );
  893. } );
  894. // Helper method that creates an exception marker inside given parent.
  895. // Marker range is set to given position offsets (start, end).
  896. function addExceptionMarker( startOffset, endOffset = startOffset, parent, id = 1 ) {
  897. model.change( writer => {
  898. writer.addMarker( `restrictedEditingException:${ id }`, {
  899. range: writer.createRange(
  900. writer.createPositionAt( parent, startOffset ),
  901. writer.createPositionAt( parent, endOffset )
  902. ),
  903. usingOperation: true,
  904. affectsData: true
  905. } );
  906. } );
  907. }
  908. } );