list.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import List from '../src/list';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import ListEngine from '../src/listengine';
  10. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  11. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  12. describe( 'List', () => {
  13. let editor, bulletedListButton, numberedListButton, schema;
  14. beforeEach( () => {
  15. const editorElement = document.createElement( 'div' );
  16. document.body.appendChild( editorElement );
  17. return ClassicTestEditor.create( editorElement, {
  18. plugins: [ Paragraph, List ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. schema = editor.document.schema;
  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. editor.setData( '<ul><li>foo</li></ul>' );
  49. // Collapsing selection in model, which has just flat listItems.
  50. editor.document.selection.collapse( editor.document.getRoot().getChild( 0 ) );
  51. const command = editor.commands.get( 'bulletedList' );
  52. expect( bulletedListButton.isOn ).to.be.true;
  53. expect( bulletedListButton.isEnabled ).to.be.true;
  54. command.value = false;
  55. expect( bulletedListButton.isOn ).to.be.false;
  56. command.isEnabled = false;
  57. expect( bulletedListButton.isEnabled ).to.be.false;
  58. } );
  59. it( 'should bind numbered list button model to numberedList command', () => {
  60. editor.setData( '<ul><li>foo</li></ul>' );
  61. // Collapsing selection in model, which has just flat listItems.
  62. editor.document.selection.collapse( editor.document.getRoot().getChild( 0 ) );
  63. const command = editor.commands.get( 'numberedList' );
  64. // We are in UL, so numbered list is off.
  65. expect( numberedListButton.isOn ).to.be.false;
  66. expect( numberedListButton.isEnabled ).to.be.true;
  67. command.value = true;
  68. expect( numberedListButton.isOn ).to.be.true;
  69. command.isEnabled = false;
  70. expect( numberedListButton.isEnabled ).to.be.false;
  71. } );
  72. describe( 'enter key handling callback', () => {
  73. it( 'should execute outdentList command on enter key in empty list', () => {
  74. const domEvtDataStub = { preventDefault() {} };
  75. sinon.spy( editor, 'execute' );
  76. editor.setData( '<ul><li></li></ul>' );
  77. // Collapsing selection in model, which has just flat listItems.
  78. editor.document.selection.collapse( editor.document.getRoot().getChild( 0 ) );
  79. editor.editing.view.fire( 'enter', domEvtDataStub );
  80. expect( editor.execute.calledOnce ).to.be.true;
  81. expect( editor.execute.calledWithExactly( 'outdentList' ) );
  82. } );
  83. it( 'should not execute outdentList command on enter key in non-empty list', () => {
  84. const domEvtDataStub = { preventDefault() {} };
  85. sinon.spy( editor, 'execute' );
  86. editor.setData( '<ul><li>foobar</li></ul>' );
  87. // Collapsing selection in model, which has just flat listItems.
  88. editor.document.selection.collapse( editor.document.getRoot().getChild( 0 ) );
  89. editor.editing.view.fire( 'enter', domEvtDataStub );
  90. expect( editor.execute.called ).to.be.false;
  91. } );
  92. } );
  93. describe( 'tab key handling callback', () => {
  94. let domEvtDataStub;
  95. beforeEach( () => {
  96. domEvtDataStub = {
  97. keystroke: getCode( 'tab' ),
  98. preventDefault() {}
  99. };
  100. sinon.spy( editor, 'execute' );
  101. } );
  102. afterEach( () => {
  103. editor.execute.restore();
  104. } );
  105. it( 'should execute indentList command on tab key', () => {
  106. editor.setData( '<ul><li>foo</li><li>bar</li></ul>' );
  107. // Collapsing selection in model, which has just flat listItems.
  108. editor.document.selection.collapse( editor.document.getRoot().getChild( 1 ) );
  109. editor.editing.view.fire( 'keydown', domEvtDataStub );
  110. expect( editor.execute.calledOnce ).to.be.true;
  111. expect( editor.execute.calledWithExactly( 'indentList' ) ).to.be.true;
  112. } );
  113. it( 'should execute indentList command on tab key for non-collapsed selection and indent only first item', () => {
  114. editor.setData( '<ul><li>foo</li><li>bar</li><li>xyz</li></ul>' );
  115. editor.document.selection.collapse( editor.document.getRoot().getChild( 1 ) );
  116. editor.document.selection.setFocus( editor.document.getRoot().getChild( 2 ) );
  117. editor.editing.view.fire( 'keydown', domEvtDataStub );
  118. expect( editor.execute.calledOnce ).to.be.true;
  119. expect( editor.execute.calledWithExactly( 'indentList' ) ).to.be.true;
  120. expect( editor.getData() ).to.equal( '<ul><li>foo<ul><li>bar</li></ul></li><li>xyz</li></ul>' );
  121. } );
  122. it( 'should execute outdentList command on shift+tab keystroke', () => {
  123. domEvtDataStub.keystroke += getCode( 'shift' );
  124. editor.setData( '<ul><li>foo<ul><li>bar</li></ul></li></ul>' );
  125. // Collapsing selection in model, which has just flat listItems.
  126. editor.document.selection.collapse( editor.document.getRoot().getChild( 1 ) );
  127. editor.editing.view.fire( 'keydown', domEvtDataStub );
  128. expect( editor.execute.calledOnce ).to.be.true;
  129. expect( editor.execute.calledWithExactly( 'outdentList' ) ).to.be.true;
  130. } );
  131. it( 'should not indent if command is disabled', () => {
  132. editor.setData( '<ul><li>foo</li></ul>' );
  133. // Collapsing selection in model, which has just flat listItems.
  134. editor.document.selection.collapse( editor.document.getRoot().getChild( 0 ) );
  135. editor.editing.view.fire( 'keydown', domEvtDataStub );
  136. expect( editor.execute.called ).to.be.false;
  137. } );
  138. it( 'should not indent or outdent if alt+tab is pressed', () => {
  139. domEvtDataStub.keystroke += getCode( 'alt' );
  140. editor.setData( '<ul><li>foo</li></ul>' );
  141. // Collapsing selection in model, which has just flat listItems.
  142. editor.document.selection.collapse( editor.document.getRoot().getChild( 0 ) );
  143. editor.editing.view.fire( 'keydown', domEvtDataStub );
  144. expect( editor.execute.called ).to.be.false;
  145. } );
  146. } );
  147. } );