restrictededitingexceptioncommand.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 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 not set the selection attribute if there is a text without the attribute (forceValue="false")', () => {
  93. setData( model, '<p>abcfoo[]barbaz</p>' );
  94. command.execute( { forceValue: false } );
  95. expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.false;
  96. } );
  97. it( 'should not change the selection attribute if selection has the attribute already (forceValue="true")', () => {
  98. setData( model, '<p>abcfoo[]barbaz</p>' );
  99. model.change( writer => {
  100. writer.setSelectionAttribute( 'restrictedEditingException', true );
  101. } );
  102. command.execute( { forceValue: true } );
  103. expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.true;
  104. } );
  105. it( 'should remove an attribute from text node if a text has the non-restricted attribute', () => {
  106. setData( model, '<p>abc<$text restrictedEditingException="true">foo[]bar</$text>baz</p>' );
  107. command.execute();
  108. expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.false;
  109. expect( getData( model ) ).to.equal( '<p>abcfoo[]barbaz</p>' );
  110. } );
  111. it( 'should remove attribute from text node if a text has the non-restricted attribute (forceValue="false")', () => {
  112. setData( model, '<p>abc<$text restrictedEditingException="true">foo[]bar</$text>baz</p>' );
  113. command.execute( { forceValue: false } );
  114. expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.false;
  115. expect( getData( model ) ).to.equal( '<p>abcfoo[]barbaz</p>' );
  116. } );
  117. it( 'should not remove attribute from text node if a text has the non-restricted attribute (forceValue="true")', () => {
  118. setData( model, '<p>abc<$text restrictedEditingException="true">foo[]bar</$text>baz</p>' );
  119. command.execute( { forceValue: true } );
  120. expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.true;
  121. expect( getData( model ) ).to.equal( '<p>abc<$text restrictedEditingException="true">foo[]bar</$text>baz</p>' );
  122. } );
  123. it( 'should not remove exception when selection is at the beginning of restricted text', () => {
  124. setData( model, '<p>abc<$text restrictedEditingException="true">[]foobar</$text>baz</p>' );
  125. command.execute();
  126. expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.true;
  127. expect( getData( model ) ).to.equal( '<p>abc<$text restrictedEditingException="true">[]foobar</$text>baz</p>' );
  128. } );
  129. it( 'should remove attribute from text nodes when other attributes are present', () => {
  130. setData( model,
  131. '<p>' +
  132. '<$text bold="true">abc</$text>' +
  133. '<$text bold="true" restrictedEditingException="true">foo[]</$text>' +
  134. '<$text restrictedEditingException="true">bar</$text>' +
  135. 'baz' +
  136. '</p>'
  137. );
  138. command.execute();
  139. expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.false;
  140. expect( getData( model ) ).to.equal( '<p><$text bold="true">abcfoo[]</$text>barbaz</p>' );
  141. } );
  142. it( 'should remove selection attribute if selection does not have it (selection at the beginning)', () => {
  143. setData( model, '<p>abc<$text restrictedEditingException="true">[]foobar</$text>baz</p>' );
  144. model.change( writer => {
  145. writer.setSelectionAttribute( 'restrictedEditingException', 'true' );
  146. } );
  147. command.execute();
  148. expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.false;
  149. expect( getData( model ) ).to.equal( '<p>abc[]<$text restrictedEditingException="true">foobar</$text>baz</p>' );
  150. } );
  151. it( 'should not remove exception when selection is at the end of restricted text', () => {
  152. setData( model, '<p>abc<$text restrictedEditingException="true">foobar[]</$text>baz</p>' );
  153. command.execute();
  154. expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.false;
  155. expect( getData( model ) ).to.equal( '<p>abc<$text restrictedEditingException="true">foobar</$text>[]baz</p>' );
  156. } );
  157. it( 'should set selection attribute if selection does not have it (selection at the end)', () => {
  158. setData( model, '<p>abc<$text restrictedEditingException="true">foobar[]</$text>baz</p>' );
  159. model.change( writer => {
  160. writer.removeSelectionAttribute( 'restrictedEditingException' );
  161. } );
  162. command.execute();
  163. expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.true;
  164. expect( getData( model ) ).to.equal( '<p>abc<$text restrictedEditingException="true">foobar[]</$text>baz</p>' );
  165. } );
  166. } );
  167. describe( 'non-collapsed selection', () => {
  168. it( 'should do nothing if the command is disabled', () => {
  169. setData( model, '<p>fo[ob]ar</p>' );
  170. command.isEnabled = false;
  171. command.execute();
  172. expect( getData( model ) ).to.equal( '<p>fo[ob]ar</p>' );
  173. } );
  174. it( 'should add the attribute on a text without the attribute', () => {
  175. setData( model, '<p>foo[bar]baz</p>' );
  176. command.execute();
  177. expect( getData( model ) ).to.equal( '<p>foo[<$text restrictedEditingException="true">bar</$text>]baz</p>' );
  178. } );
  179. it( 'should add the attribute on a selected text if a selected part already has the attribute', () => {
  180. setData( model, '<p>[foo<$text restrictedEditingException="true">bar</$text>]baz</p>' );
  181. command.execute();
  182. expect( getData( model ) ).to.equal( '<p>[<$text restrictedEditingException="true">foobar</$text>]baz</p>' );
  183. } );
  184. it( 'should remove the attribute only from the selected part of a non-restricted text', () => {
  185. setData( model, '<p><$text restrictedEditingException="true">foo[bar]baz</$text></p>' );
  186. command.execute();
  187. expect( getData( model ) ).to.equal(
  188. '<p><$text restrictedEditingException="true">foo</$text>[bar]<$text restrictedEditingException="true">baz</$text></p>'
  189. );
  190. } );
  191. it( 'should remove the attribute from the selected text if all text contains the attribute', () => {
  192. setData( model, '<p>abc[<$text restrictedEditingException="true">foo]bar</$text>baz</p>' );
  193. command.execute();
  194. expect( getData( model ) ).to.equal( '<p>abc[foo]<$text restrictedEditingException="true">bar</$text>baz</p>' );
  195. } );
  196. it( 'should add the attribute on a selected text if the "forceValue" parameter was true', () => {
  197. setData( model, '<p>abc<$text restrictedEditingException="true">foob[ar</$text>x]yz</p>' );
  198. expect( command.value ).to.be.true;
  199. command.execute( { forceValue: true } );
  200. expect( command.value ).to.be.true;
  201. expect( getData( model ) ).to.equal( '<p>abc<$text restrictedEditingException="true">foob[arx</$text>]yz</p>' );
  202. } );
  203. it( 'should remove the attribute on selected nodes if the "forceValue" parameter was set false', () => {
  204. setData( model, '<p>a[bc<$text restrictedEditingException="true">fo]obar</$text>xyz</p>' );
  205. command.execute( { forceValue: false } );
  206. expect( command.value ).to.be.false;
  207. expect( getData( model ) ).to.equal( '<p>a[bcfo]<$text restrictedEditingException="true">obar</$text>xyz</p>' );
  208. } );
  209. } );
  210. } );
  211. } );