restrictededitingexceptioncommand.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import RestrictedEditingExceptionCommand from '../src/restrictededitingexceptioncommand';
  8. describe( 'RestrictedEditingExceptionCommand', () => {
  9. let editor, command, model;
  10. beforeEach( () => {
  11. return ModelTestEditor
  12. .create()
  13. .then( newEditor => {
  14. editor = newEditor;
  15. model = editor.model;
  16. command = new RestrictedEditingExceptionCommand( editor );
  17. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  18. model.schema.register( 'h1', { inheritAllFrom: '$block' } );
  19. model.schema.register( 'img', {
  20. allowWhere: [ '$block', '$text' ],
  21. isObject: true
  22. } );
  23. editor.model.schema.extend( '$text', { allowAttributes: [ 'restrictedEditingException' ] } );
  24. } );
  25. } );
  26. afterEach( () => {
  27. command.destroy();
  28. return editor.destroy();
  29. } );
  30. describe( 'value', () => {
  31. it( 'is true when collapsed selection has the attribute', () => {
  32. model.change( writer => {
  33. writer.setSelectionAttribute( 'restrictedEditingException', true );
  34. } );
  35. expect( command.value ).to.be.true;
  36. } );
  37. it( 'is false when collapsed selection does not have the attribute', () => {
  38. model.change( writer => {
  39. writer.setSelectionAttribute( 'restrictedEditingException', true );
  40. } );
  41. model.change( writer => {
  42. writer.removeSelectionAttribute( 'restrictedEditingException' );
  43. } );
  44. expect( command.value ).to.be.false;
  45. } );
  46. it( 'is true when the selection is inside a text with the attribute', () => {
  47. setData( model, '<p><$text restrictedEditingException="true">fo[]o</$text></p><h1>bar</h1>' );
  48. expect( command.value ).to.be.true;
  49. } );
  50. it( 'is true when the selection is on a text with the attribute', () => {
  51. setData( model, '<p>foo[<$text restrictedEditingException="true">bar</$text>]baz</p>' );
  52. expect( command.value ).to.be.true;
  53. } );
  54. } );
  55. describe( 'isEnabled', () => {
  56. beforeEach( () => {
  57. model.schema.register( 'x', { inheritAllFrom: '$block' } );
  58. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  59. if ( ctx.endsWith( 'x $text' ) && attributeName == 'restrictedEditingException' ) {
  60. return false;
  61. }
  62. } );
  63. } );
  64. describe( 'when the selection is collapsed', () => {
  65. it( 'should return true if the attribute is allowed at the caret position', () => {
  66. setData( model, '<p>f[]oo</p>' );
  67. expect( command.isEnabled ).to.be.true;
  68. } );
  69. it( 'should return true if the attribute is not allowed at the caret position', () => {
  70. setData( model, '<x>fo[]o</x>' );
  71. expect( command.isEnabled ).to.be.false;
  72. } );
  73. } );
  74. describe( 'when the selection is not collapsed', () => {
  75. it( 'should return true if there is at least one node in the selection that can have the attribute', () => {
  76. setData( model, '<p>[foo]</p>' );
  77. expect( command.isEnabled ).to.be.true;
  78. } );
  79. it( 'should return false if there are no nodes in the selection that can have the attribute', () => {
  80. setData( model, '<x>[foo]</x>' );
  81. expect( command.isEnabled ).to.be.false;
  82. } );
  83. } );
  84. } );
  85. describe( 'execute()', () => {
  86. describe( 'collapsed selection', () => {
  87. it( 'should set the selection attribute if there is a text without the attribute', () => {
  88. setData( model, '<p>abcfoo[]barbaz</p>' );
  89. command.execute();
  90. expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.true;
  91. } );
  92. it( 'should remove the selection attribute if a text has the non-restricted attribute', () => {
  93. setData( model, '<p>abc<$text restrictedEditingException="true">foo[]bar</$text>baz</p>' );
  94. command.execute();
  95. expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.false;
  96. } );
  97. } );
  98. describe( 'non-collapsed selection', () => {
  99. it( 'should do nothing if the command is disabled', () => {
  100. setData( model, '<p>fo[ob]ar</p>' );
  101. command.isEnabled = false;
  102. command.execute();
  103. expect( getData( model ) ).to.equal( '<p>fo[ob]ar</p>' );
  104. } );
  105. it( 'should add the attribute on a text without the attribute', () => {
  106. setData( model, '<p>foo[bar]baz</p>' );
  107. command.execute();
  108. expect( getData( model ) ).to.equal( '<p>foo[<$text restrictedEditingException="true">bar</$text>]baz</p>' );
  109. } );
  110. it( 'should add the attribute on a selected text if a selected part already has the attribute', () => {
  111. setData( model, '<p>[foo<$text restrictedEditingException="true">bar</$text>]baz</p>' );
  112. command.execute();
  113. expect( getData( model ) ).to.equal( '<p>[<$text restrictedEditingException="true">foobar</$text>]baz</p>' );
  114. } );
  115. it( 'should remove the attribute only from the selected part of a non-restricted text', () => {
  116. setData( model, '<p><$text restrictedEditingException="true">foo[bar]baz</$text></p>' );
  117. command.execute();
  118. expect( getData( model ) ).to.equal(
  119. '<p><$text restrictedEditingException="true">foo</$text>[bar]<$text restrictedEditingException="true">baz</$text></p>'
  120. );
  121. } );
  122. it( 'should remove the attribute from the selected text if all text contains the attribute', () => {
  123. setData( model, '<p>abc[<$text restrictedEditingException="true">foo]bar</$text>baz</p>' );
  124. command.execute();
  125. expect( getData( model ) ).to.equal( '<p>abc[foo]<$text restrictedEditingException="true">bar</$text>baz</p>' );
  126. } );
  127. it( 'should add the attribute on a selected text if the "forceValue" parameter was true', () => {
  128. setData( model, '<p>abc<$text restrictedEditingException="true">foob[ar</$text>x]yz</p>' );
  129. expect( command.value ).to.be.true;
  130. command.execute( { forceValue: true } );
  131. expect( command.value ).to.be.true;
  132. expect( getData( model ) ).to.equal( '<p>abc<$text restrictedEditingException="true">foob[arx</$text>]yz</p>' );
  133. } );
  134. it( 'should remove the attribute on selected nodes if the "forceValue" parameter was set false', () => {
  135. setData( model, '<p>a[bc<$text restrictedEditingException="true">fo]obar</$text>xyz</p>' );
  136. command.execute( { forceValue: false } );
  137. expect( command.value ).to.be.false;
  138. expect( getData( model ) ).to.equal( '<p>a[bcfo]<$text restrictedEditingException="true">obar</$text>xyz</p>' );
  139. } );
  140. } );
  141. } );
  142. } );