toggleattributecommand.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Editor from '/ckeditor5/core/editor/editor.js';
  6. import Document from '/ckeditor5/engine/model/document.js';
  7. import ToggleAttributeCommand from '/ckeditor5/core/command/toggleattributecommand.js';
  8. import Range from '/ckeditor5/engine/model/range.js';
  9. import Position from '/ckeditor5/engine/model/position.js';
  10. import { setData, getData } from '/tests/engine/_utils/model.js';
  11. describe( 'ToggleAttributeCommand', () => {
  12. const attrKey = 'bold';
  13. let editor, command, modelDoc, root;
  14. beforeEach( () => {
  15. editor = new Editor();
  16. editor.document = new Document();
  17. modelDoc = editor.document;
  18. root = modelDoc.createRoot();
  19. command = new ToggleAttributeCommand( editor, attrKey );
  20. modelDoc.schema.registerItem( 'p', '$block' );
  21. modelDoc.schema.registerItem( 'h1', '$block' );
  22. modelDoc.schema.registerItem( 'img', '$inline' );
  23. // Allow block in "root" (DIV)
  24. modelDoc.schema.allow( { name: '$block', inside: '$root' } );
  25. // Bold text is allowed only in P.
  26. modelDoc.schema.allow( { name: '$text', attributes: 'bold', inside: 'p' } );
  27. modelDoc.schema.allow( { name: 'p', attributes: 'bold', inside: '$root' } );
  28. // Disallow bold on image.
  29. modelDoc.schema.disallow( { name: 'img', attributes: 'bold', inside: '$root' } );
  30. } );
  31. afterEach( () => {
  32. command.destroy();
  33. } );
  34. describe( 'value', () => {
  35. it( 'should be set to true or false basing on selection attribute', () => {
  36. modelDoc.selection.setAttribute( attrKey, true );
  37. expect( command.value ).to.be.true;
  38. modelDoc.selection.removeAttribute( attrKey );
  39. expect( command.value ).to.be.false;
  40. } );
  41. } );
  42. describe( '_doExecute', () => {
  43. it( 'should add attribute on selected nodes if the command value was false', () => {
  44. setData( modelDoc, '<p>a[bc<$text bold="true">fo]obar</$text>xyz</p>' );
  45. expect( command.value ).to.be.false;
  46. command._doExecute();
  47. expect( command.value ).to.be.true;
  48. expect( getData( modelDoc ) ).to.equal( '<p>a[<$text bold="true">bcfo]obar</$text>xyz</p>' );
  49. } );
  50. it( 'should remove attribute from selected nodes if the command value was true', () => {
  51. setData( modelDoc, '<p>abc[<$text bold="true">foo]bar</$text>xyz</p>' );
  52. expect( command.value ).to.be.true;
  53. command._doExecute();
  54. expect( getData( modelDoc ) ).to.equal( '<p>abc[foo]<$text bold="true">bar</$text>xyz</p>' );
  55. expect( command.value ).to.be.false;
  56. } );
  57. it( 'should add attribute on selected nodes if execute parameter was set to true', () => {
  58. setData( modelDoc, '<p>abc<$text bold="true">foob[ar</$text>x]yz</p>' );
  59. expect( command.value ).to.be.true;
  60. command._doExecute( true );
  61. expect( command.value ).to.be.true;
  62. expect( getData( modelDoc ) ).to.equal( '<p>abc<$text bold="true">foob[arx</$text>]yz</p>' );
  63. } );
  64. it( 'should remove attribute on selected nodes if execute parameter was set to false', () => {
  65. setData( modelDoc, '<p>a[bc<$text bold="true">fo]obar</$text>xyz</p>' );
  66. command._doExecute( false );
  67. expect( command.value ).to.be.false;
  68. expect( getData( modelDoc ) ).to.equal( '<p>a[bcfo]<$text bold="true">obar</$text>xyz</p>' );
  69. } );
  70. it( 'should change selection attribute if selection is collapsed in non-empty parent', () => {
  71. setData( modelDoc, '<p>a[]bc<$text bold="true">foobar</$text>xyz</p><p></p>' );
  72. expect( command.value ).to.be.false;
  73. command._doExecute();
  74. expect( command.value ).to.be.true;
  75. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.true;
  76. command._doExecute();
  77. expect( command.value ).to.be.false;
  78. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.false;
  79. } );
  80. it( 'should not store attribute change on selection if selection is collapsed in non-empty parent', () => {
  81. setData( modelDoc, '<p>a[]bc<$text bold="true">foobar</$text>xyz</p>' );
  82. command._doExecute();
  83. // It should not save that bold was executed at position ( root, [ 0, 1 ] ).
  84. // Simulate clicking right arrow key by changing selection ranges.
  85. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 2 ] ) ) ] );
  86. // Get back to previous selection.
  87. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 1 ] ) ) ] );
  88. expect( command.value ).to.be.false;
  89. } );
  90. it( 'should change selection attribute and store it if selection is collapsed in empty parent', () => {
  91. setData( modelDoc, '<p>abc<$text bold="true">foobar</$text>xyz</p><p>[]</p>' );
  92. expect( command.value ).to.be.false;
  93. command._doExecute();
  94. expect( command.value ).to.be.true;
  95. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.true;
  96. // Attribute should be stored.
  97. // Simulate clicking somewhere else in the editor.
  98. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 2 ] ) ) ] );
  99. expect( command.value ).to.be.false;
  100. // Go back to where attribute was stored.
  101. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) ) ] );
  102. // Attribute should be restored.
  103. expect( command.value ).to.be.true;
  104. command._doExecute();
  105. expect( command.value ).to.be.false;
  106. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.false;
  107. } );
  108. it( 'should not apply attribute change where it would invalid schema', () => {
  109. modelDoc.schema.registerItem( 'image', '$block' );
  110. setData( modelDoc, '<p>ab[c<image></image><$text bold="true">foobar</$text>xy<image></image>]z</p>' );
  111. expect( command.isEnabled ).to.be.true;
  112. command._doExecute();
  113. expect( getData( modelDoc ) )
  114. .to.equal( '<p>ab[<$text bold="true">c</$text><image></image><$text bold="true">foobarxy</$text><image></image>]z</p>' );
  115. } );
  116. } );
  117. describe( '_checkEnabled', () => {
  118. describe( '_checkEnabled', () => {
  119. // This test doesn't tests every possible case.
  120. // Method `_checkEnabled` uses `isAttributeAllowedInSelection` helper which is fully tested in his own test.
  121. beforeEach( () => {
  122. modelDoc.schema.registerItem( 'x', '$block' );
  123. modelDoc.schema.disallow( { name: '$text', inside: 'x', attributes: 'link' } );
  124. } );
  125. describe( 'when selection is collapsed', () => {
  126. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  127. setData( modelDoc, '<p>f[]oo</p>' );
  128. expect( command._checkEnabled() ).to.be.true;
  129. } );
  130. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  131. setData( modelDoc, '<x>fo[]o</x>' );
  132. expect( command._checkEnabled() ).to.be.false;
  133. } );
  134. } );
  135. describe( 'when selection is not collapsed', () => {
  136. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  137. setData( modelDoc, '<p>[foo]</p>' );
  138. expect( command._checkEnabled() ).to.be.true;
  139. } );
  140. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  141. setData( modelDoc, '<x>[foo]</x>' );
  142. expect( command._checkEnabled() ).to.be.false;
  143. } );
  144. } );
  145. } );
  146. } );
  147. } );