list.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import List from '../src/list';
  7. import ListEngine from '../src/listengine';
  8. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
  11. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  12. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  13. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. describe( 'List', () => {
  15. let editor, model, bulletedListButton, numberedListButton;
  16. beforeEach( () => {
  17. const editorElement = document.createElement( 'div' );
  18. document.body.appendChild( editorElement );
  19. return ClassicTestEditor.create( editorElement, { plugins: [ Paragraph, BlockQuote, List ] } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. model = editor.model;
  23. bulletedListButton = editor.ui.componentFactory.create( 'bulletedList' );
  24. numberedListButton = editor.ui.componentFactory.create( 'numberedList' );
  25. } );
  26. } );
  27. afterEach( () => {
  28. return editor.destroy();
  29. } );
  30. it( 'should be loaded', () => {
  31. expect( editor.plugins.get( List ) ).to.be.instanceOf( List );
  32. } );
  33. it( 'should load ListEngine', () => {
  34. expect( editor.plugins.get( ListEngine ) ).to.be.instanceOf( ListEngine );
  35. } );
  36. it( 'should set up keys for bulleted list and numbered list', () => {
  37. expect( bulletedListButton ).to.be.instanceOf( ButtonView );
  38. expect( numberedListButton ).to.be.instanceOf( ButtonView );
  39. } );
  40. it( 'should execute proper commands when buttons are used', () => {
  41. sinon.spy( editor, 'execute' );
  42. bulletedListButton.fire( 'execute' );
  43. expect( editor.execute.calledWithExactly( 'bulletedList' ) );
  44. numberedListButton.fire( 'execute' );
  45. expect( editor.execute.calledWithExactly( 'numberedList' ) );
  46. } );
  47. it( 'should bind bulleted list button model to bulledList command', () => {
  48. setData( model, '<listItem type="bulleted" indent="0">[]foo</listItem>' );
  49. const command = editor.commands.get( 'bulletedList' );
  50. expect( bulletedListButton.isOn ).to.be.true;
  51. expect( bulletedListButton.isEnabled ).to.be.true;
  52. command.value = false;
  53. expect( bulletedListButton.isOn ).to.be.false;
  54. command.isEnabled = false;
  55. expect( bulletedListButton.isEnabled ).to.be.false;
  56. } );
  57. it( 'should bind numbered list button model to numberedList command', () => {
  58. setData( model, '<listItem type="bulleted" indent="0">[]foo</listItem>' );
  59. const command = editor.commands.get( 'numberedList' );
  60. // We are in UL, so numbered list is off.
  61. expect( numberedListButton.isOn ).to.be.false;
  62. expect( numberedListButton.isEnabled ).to.be.true;
  63. command.value = true;
  64. expect( numberedListButton.isOn ).to.be.true;
  65. command.isEnabled = false;
  66. expect( numberedListButton.isEnabled ).to.be.false;
  67. } );
  68. describe( 'enter key handling callback', () => {
  69. it( 'should execute outdentList command on enter key in empty list', () => {
  70. const domEvtDataStub = { preventDefault() {} };
  71. sinon.spy( editor, 'execute' );
  72. setData( model, '<listItem type="bulleted" indent="0">[]</listItem>' );
  73. editor.editing.view.fire( 'enter', domEvtDataStub );
  74. expect( editor.execute.calledOnce ).to.be.true;
  75. expect( editor.execute.calledWithExactly( 'outdentList' ) );
  76. } );
  77. it( 'should not execute outdentList command on enter key in non-empty list', () => {
  78. const domEvtDataStub = { preventDefault() {} };
  79. sinon.spy( editor, 'execute' );
  80. setData( model, '<listItem type="bulleted" indent="0">foo[]</listItem>' );
  81. editor.editing.view.fire( 'enter', domEvtDataStub );
  82. expect( editor.execute.called ).to.be.false;
  83. } );
  84. } );
  85. describe( 'delete key handling callback', () => {
  86. it( 'should execute outdentList command on backspace key in first item of list', () => {
  87. const domEvtDataStub = { preventDefault() {}, direction: 'backward' };
  88. sinon.spy( editor, 'execute' );
  89. setData( model, '<listItem type="bulleted" indent="0">[]foo</listItem>' );
  90. editor.editing.view.fire( 'delete', domEvtDataStub );
  91. expect( editor.execute.calledWithExactly( 'outdentList' ) );
  92. } );
  93. it( 'should execute outdentList command on backspace key in first item of list', () => {
  94. const domEvtDataStub = { preventDefault() {}, direction: 'backward' };
  95. sinon.spy( editor, 'execute' );
  96. setData( model, '<paragraph>foo</paragraph><listItem type="bulleted" indent="0">[]foo</listItem>' );
  97. editor.editing.view.fire( 'delete', domEvtDataStub );
  98. expect( editor.execute.calledWithExactly( 'outdentList' ) );
  99. } );
  100. it( 'should not execute outdentList command on delete key in first item of list', () => {
  101. const domEvtDataStub = { preventDefault() {}, direction: 'forward' };
  102. sinon.spy( editor, 'execute' );
  103. setData( model, '<listItem type="bulleted" indent="0">[]foo</listItem>' );
  104. editor.editing.view.fire( 'delete', domEvtDataStub );
  105. sinon.assert.notCalled( editor.execute );
  106. } );
  107. it( 'should not execute outdentList command when selection is not collapsed', () => {
  108. const domEvtDataStub = { preventDefault() {}, direction: 'backward' };
  109. sinon.spy( editor, 'execute' );
  110. setData( model, '<listItem type="bulleted" indent="0">[fo]o</listItem>' );
  111. editor.editing.view.fire( 'delete', domEvtDataStub );
  112. sinon.assert.notCalled( editor.execute );
  113. } );
  114. it( 'should not execute outdentList command if not in list item', () => {
  115. const domEvtDataStub = { preventDefault() {}, direction: 'backward' };
  116. sinon.spy( editor, 'execute' );
  117. setData( model, '<paragraph>[]foo</paragraph>' );
  118. editor.editing.view.fire( 'delete', domEvtDataStub );
  119. sinon.assert.notCalled( editor.execute );
  120. } );
  121. it( 'should not execute outdentList command if not in first list item', () => {
  122. const domEvtDataStub = { preventDefault() {}, direction: 'backward' };
  123. sinon.spy( editor, 'execute' );
  124. setData( model, '<listItem type="bulleted" indent="0">foo</listItem><listItem type="bulleted" indent="0">[]foo</listItem>' );
  125. editor.editing.view.fire( 'delete', domEvtDataStub );
  126. sinon.assert.notCalled( editor.execute );
  127. } );
  128. it( 'should not execute outdentList command when selection is not on first position', () => {
  129. const domEvtDataStub = { preventDefault() {}, direction: 'backward' };
  130. sinon.spy( editor, 'execute' );
  131. setData( model, '<listItem type="bulleted" indent="0">fo[]o</listItem>' );
  132. editor.editing.view.fire( 'delete', domEvtDataStub );
  133. sinon.assert.notCalled( editor.execute );
  134. } );
  135. it( 'should not execute outdentList command when selection is not on first position', () => {
  136. const domEvtDataStub = { preventDefault() {}, direction: 'backward' };
  137. sinon.spy( editor, 'execute' );
  138. setData( model, '<listItem type="bulleted" indent="0">fo[]o</listItem>' );
  139. editor.editing.view.fire( 'delete', domEvtDataStub );
  140. sinon.assert.notCalled( editor.execute );
  141. } );
  142. it( 'should outdent list when previous element is nested in block quote', () => {
  143. const domEvtDataStub = { preventDefault() {}, direction: 'backward' };
  144. sinon.spy( editor, 'execute' );
  145. setData( model, '<blockQuote><paragraph>x</paragraph></blockQuote><listItem type="bulleted" indent="0">[]foo</listItem>' );
  146. editor.editing.view.fire( 'delete', domEvtDataStub );
  147. expect( editor.execute.calledWithExactly( 'outdentList' ) );
  148. } );
  149. it( 'should outdent list when list is nested in block quote', () => {
  150. const domEvtDataStub = { preventDefault() {}, direction: 'backward' };
  151. sinon.spy( editor, 'execute' );
  152. setData( model, '<paragraph>x</paragraph><blockQuote><listItem type="bulleted" indent="0">[]foo</listItem></blockQuote>' );
  153. editor.editing.view.fire( 'delete', domEvtDataStub );
  154. expect( editor.execute.calledWithExactly( 'outdentList' ) );
  155. } );
  156. } );
  157. describe( 'tab key handling callback', () => {
  158. let domEvtDataStub;
  159. beforeEach( () => {
  160. domEvtDataStub = {
  161. keyCode: getCode( 'Tab' ),
  162. preventDefault: sinon.spy(),
  163. stopPropagation: sinon.spy()
  164. };
  165. sinon.spy( editor, 'execute' );
  166. } );
  167. afterEach( () => {
  168. editor.execute.restore();
  169. } );
  170. it( 'should execute indentList command on tab key', () => {
  171. setData(
  172. model,
  173. '<listItem type="bulleted" indent="0">foo</listItem>' +
  174. '<listItem type="bulleted" indent="0">[]bar</listItem>'
  175. );
  176. editor.editing.view.fire( 'keydown', domEvtDataStub );
  177. expect( editor.execute.calledOnce ).to.be.true;
  178. expect( editor.execute.calledWithExactly( 'indentList' ) ).to.be.true;
  179. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  180. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  181. } );
  182. it( 'should execute outdentList command on Shift+Tab keystroke', () => {
  183. domEvtDataStub.keyCode += getCode( 'Shift' );
  184. setData(
  185. model,
  186. '<listItem type="bulleted" indent="0">foo</listItem>' +
  187. '<listItem type="bulleted" indent="1">[]bar</listItem>'
  188. );
  189. editor.editing.view.fire( 'keydown', domEvtDataStub );
  190. expect( editor.execute.calledOnce ).to.be.true;
  191. expect( editor.execute.calledWithExactly( 'outdentList' ) ).to.be.true;
  192. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  193. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  194. } );
  195. it( 'should not indent if command is disabled', () => {
  196. setData( model, '<listItem type="bulleted" indent="0">[]foo</listItem>' );
  197. editor.editing.view.fire( 'keydown', domEvtDataStub );
  198. expect( editor.execute.called ).to.be.false;
  199. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  200. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  201. } );
  202. it( 'should not indent or outdent if alt+tab is pressed', () => {
  203. domEvtDataStub.keyCode += getCode( 'alt' );
  204. setData(
  205. model,
  206. '<listItem type="bulleted" indent="0">foo</listItem>' +
  207. '<listItem type="bulleted" indent="0">[]bar</listItem>'
  208. );
  209. editor.editing.view.fire( 'keydown', domEvtDataStub );
  210. expect( editor.execute.called ).to.be.false;
  211. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  212. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  213. } );
  214. } );
  215. } );