list.js 6.8 KB

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