restrictededitingmodeediting.js 51 KB

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