blockquote.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import BlockQuote from '../src/blockquote';
  7. import BlockQuoteEngine from '../src/blockquoteengine';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import List from '@ckeditor/ckeditor5-list/src/list';
  10. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  11. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  12. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. describe( 'BlockQuote', () => {
  14. let editor, doc, command, element;
  15. beforeEach( () => {
  16. element = document.createElement( 'div' );
  17. document.body.appendChild( element );
  18. return ClassicTestEditor.create( element, {
  19. plugins: [ BlockQuote, Paragraph, List, Enter ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. doc = editor.document;
  24. command = editor.commands.get( 'blockQuote' );
  25. } );
  26. } );
  27. afterEach( () => {
  28. element.remove();
  29. return editor.destroy();
  30. } );
  31. it( 'requires BlockQuoteEngine', () => {
  32. expect( BlockQuote.requires ).to.deep.equal( [ BlockQuoteEngine ] );
  33. } );
  34. describe( 'blockQuote button', () => {
  35. it( 'has the base properties', () => {
  36. const button = editor.ui.componentFactory.create( 'blockQuote' );
  37. expect( button ).to.have.property( 'label', 'Block quote' );
  38. expect( button ).to.have.property( 'icon' );
  39. expect( button ).to.have.property( 'tooltip', true );
  40. } );
  41. it( 'has isOn bound to command\'s value', () => {
  42. const button = editor.ui.componentFactory.create( 'blockQuote' );
  43. command.value = false;
  44. expect( button ).to.have.property( 'isOn', false );
  45. command.value = true;
  46. expect( button ).to.have.property( 'isOn', true );
  47. } );
  48. it( 'has isEnabled bound to command\'s isEnabled', () => {
  49. const button = editor.ui.componentFactory.create( 'blockQuote' );
  50. command.isEnabled = true;
  51. expect( button ).to.have.property( 'isEnabled', true );
  52. command.isEnabled = false;
  53. expect( button ).to.have.property( 'isEnabled', false );
  54. } );
  55. it( 'executes command when it\'s executed', () => {
  56. const button = editor.ui.componentFactory.create( 'blockQuote' );
  57. const spy = sinon.stub( editor, 'execute' );
  58. button.fire( 'execute' );
  59. expect( spy.calledOnce ).to.be.true;
  60. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'blockQuote' );
  61. } );
  62. } );
  63. describe( 'enter key support', () => {
  64. function fakeEventData() {
  65. return {
  66. preventDefault: sinon.spy()
  67. };
  68. }
  69. it( 'does nothing if selection is in an empty block but not in a block quote', () => {
  70. const data = fakeEventData();
  71. const execSpy = sinon.spy( editor, 'execute' );
  72. setModelData( doc, '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>x</paragraph>' );
  73. editor.editing.view.fire( 'enter', data );
  74. // Only enter command should be executed.
  75. expect( data.preventDefault.called ).to.be.true;
  76. expect( execSpy.calledOnce ).to.be.true;
  77. expect( execSpy.args[ 0 ][ 0 ] ).to.equal( 'enter' );
  78. } );
  79. it( 'does nothing if selection is in a non-empty block (at the end) in a block quote', () => {
  80. const data = fakeEventData();
  81. const execSpy = sinon.spy( editor, 'execute' );
  82. setModelData( doc, '<blockQuote><paragraph>xx[]</paragraph></blockQuote>' );
  83. editor.editing.view.fire( 'enter', data );
  84. // Only enter command should be executed.
  85. expect( data.preventDefault.called ).to.be.true;
  86. expect( execSpy.calledOnce ).to.be.true;
  87. expect( execSpy.args[ 0 ][ 0 ] ).to.equal( 'enter' );
  88. } );
  89. it( 'does nothing if selection is in a non-empty block (at the beginning) in a block quote', () => {
  90. const data = fakeEventData();
  91. const execSpy = sinon.spy( editor, 'execute' );
  92. setModelData( doc, '<blockQuote><paragraph>[]xx</paragraph></blockQuote>' );
  93. editor.editing.view.fire( 'enter', data );
  94. // Only enter command should be executed.
  95. expect( data.preventDefault.called ).to.be.true;
  96. expect( execSpy.calledOnce ).to.be.true;
  97. expect( execSpy.args[ 0 ][ 0 ] ).to.equal( 'enter' );
  98. } );
  99. it( 'does nothing if selection is not collapsed', () => {
  100. const data = fakeEventData();
  101. const execSpy = sinon.spy( editor, 'execute' );
  102. setModelData( doc, '<blockQuote><paragraph>[</paragraph><paragraph>]</paragraph></blockQuote>' );
  103. editor.editing.view.fire( 'enter', data );
  104. // Only enter command should be executed.
  105. expect( data.preventDefault.called ).to.be.true;
  106. expect( execSpy.calledOnce ).to.be.true;
  107. expect( execSpy.args[ 0 ][ 0 ] ).to.equal( 'enter' );
  108. } );
  109. it( 'does not interfere with a similar handler in the list feature', () => {
  110. const data = fakeEventData();
  111. setModelData( doc,
  112. '<paragraph>x</paragraph>' +
  113. '<blockQuote>' +
  114. '<listItem indent="0" type="bulleted">a</listItem>' +
  115. '<listItem indent="0" type="bulleted">[]</listItem>' +
  116. '</blockQuote>' +
  117. '<paragraph>x</paragraph>'
  118. );
  119. editor.editing.view.fire( 'enter', data );
  120. expect( data.preventDefault.called ).to.be.true;
  121. expect( getModelData( doc ) ).to.equal(
  122. '<paragraph>x</paragraph>' +
  123. '<blockQuote>' +
  124. '<listItem indent="0" type="bulleted">a</listItem>' +
  125. '<paragraph>[]</paragraph>' +
  126. '</blockQuote>' +
  127. '<paragraph>x</paragraph>'
  128. );
  129. } );
  130. it( 'escapes block quote if selection is in an empty block in an empty block quote', () => {
  131. const data = fakeEventData();
  132. const execSpy = sinon.spy( editor, 'execute' );
  133. setModelData( doc, '<paragraph>x</paragraph><blockQuote><paragraph>[]</paragraph></blockQuote><paragraph>x</paragraph>' );
  134. editor.editing.view.fire( 'enter', data );
  135. expect( data.preventDefault.called ).to.be.true;
  136. expect( execSpy.calledOnce ).to.be.true;
  137. expect( execSpy.args[ 0 ][ 0 ] ).to.equal( 'blockQuote' );
  138. expect( getModelData( doc ) ).to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>x</paragraph>' );
  139. } );
  140. it( 'escapes block quote if selection is in an empty block in the middle of a block quote', () => {
  141. const data = fakeEventData();
  142. const execSpy = sinon.spy( editor, 'execute' );
  143. setModelData( doc,
  144. '<paragraph>x</paragraph>' +
  145. '<blockQuote><paragraph>a</paragraph><paragraph>[]</paragraph><paragraph>b</paragraph></blockQuote>' +
  146. '<paragraph>x</paragraph>'
  147. );
  148. editor.editing.view.fire( 'enter', data );
  149. expect( data.preventDefault.called ).to.be.true;
  150. expect( execSpy.calledOnce ).to.be.true;
  151. expect( execSpy.args[ 0 ][ 0 ] ).to.equal( 'blockQuote' );
  152. expect( getModelData( doc ) ).to.equal(
  153. '<paragraph>x</paragraph>' +
  154. '<blockQuote><paragraph>a</paragraph></blockQuote>' +
  155. '<paragraph>[]</paragraph>' +
  156. '<blockQuote><paragraph>b</paragraph></blockQuote>' +
  157. '<paragraph>x</paragraph>'
  158. );
  159. } );
  160. it( 'escapes block quote if selection is in an empty block at the end of a block quote', () => {
  161. const data = fakeEventData();
  162. const execSpy = sinon.spy( editor, 'execute' );
  163. setModelData( doc,
  164. '<paragraph>x</paragraph>' +
  165. '<blockQuote><paragraph>a</paragraph><paragraph>[]</paragraph></blockQuote>' +
  166. '<paragraph>x</paragraph>'
  167. );
  168. editor.editing.view.fire( 'enter', data );
  169. expect( data.preventDefault.called ).to.be.true;
  170. expect( execSpy.calledOnce ).to.be.true;
  171. expect( execSpy.args[ 0 ][ 0 ] ).to.equal( 'blockQuote' );
  172. expect( getModelData( doc ) ).to.equal(
  173. '<paragraph>x</paragraph>' +
  174. '<blockQuote><paragraph>a</paragraph></blockQuote>' +
  175. '<paragraph>[]</paragraph>' +
  176. '<paragraph>x</paragraph>'
  177. );
  178. } );
  179. } );
  180. } );