restrictededitingmodeediting.js 45 KB

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