8
0

restrictededitingmodeediting.js 42 KB

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