imagetextalternative.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global Event, document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Image from '../src/image';
  8. import ImageTextAlternative from '../src/imagetextalternative';
  9. import ImageTextAlternativeEngine from '../src/imagetextalternative/imagetextalternativeengine';
  10. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  11. import View from '@ckeditor/ckeditor5-ui/src/view';
  12. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  13. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  15. describe( 'ImageTextAlternative', () => {
  16. let editor, editingView, doc, plugin, command, form, balloon, editorElement, button;
  17. beforeEach( () => {
  18. editorElement = global.document.createElement( 'div' );
  19. global.document.body.appendChild( editorElement );
  20. return ClassicTestEditor
  21. .create( editorElement, {
  22. plugins: [ ImageTextAlternative, Image ]
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. editingView = editor.editing.view;
  27. doc = editor.document;
  28. newEditor.editing.view.attachDomRoot( editorElement );
  29. plugin = editor.plugins.get( ImageTextAlternative );
  30. command = editor.commands.get( 'imageTextAlternative' );
  31. form = plugin._form;
  32. balloon = editor.plugins.get( 'ContextualBalloon' );
  33. button = editor.ui.componentFactory.create( 'imageTextAlternative' );
  34. } );
  35. } );
  36. afterEach( () => {
  37. editorElement.remove();
  38. return editor.destroy();
  39. } );
  40. it( 'should be loaded', () => {
  41. expect( plugin ).to.be.instanceOf( ImageTextAlternative );
  42. } );
  43. it( 'should load ImageTextAlternativeEngine plugin', () => {
  44. expect( editor.plugins.get( ImageTextAlternativeEngine ) ).to.be.instanceOf( ImageTextAlternativeEngine );
  45. } );
  46. describe( 'toolbar button', () => {
  47. it( 'should be registered in component factory', () => {
  48. expect( button ).to.be.instanceOf( ButtonView );
  49. } );
  50. it( 'should have isEnabled property bind to command\'s isEnabled property', () => {
  51. command.isEnabled = true;
  52. expect( button.isEnabled ).to.be.true;
  53. command.isEnabled = false;
  54. expect( button.isEnabled ).to.be.false;
  55. } );
  56. it( 'should show balloon panel on execute', () => {
  57. expect( balloon.visibleView ).to.be.null;
  58. setData( doc, '[<image src="" alt="foo bar"></image>]' );
  59. button.fire( 'execute' );
  60. expect( balloon.visibleView ).to.equal( form );
  61. // Make sure successive execute does not throw, e.g. attempting
  62. // to display the form twice.
  63. button.fire( 'execute' );
  64. expect( balloon.visibleView ).to.equal( form );
  65. } );
  66. it( 'should set alt attribute value to textarea and select it', () => {
  67. const spy = sinon.spy( form.labeledInput, 'select' );
  68. setData( doc, '[<image src="" alt="foo bar"></image>]' );
  69. button.fire( 'execute' );
  70. sinon.assert.calledOnce( spy );
  71. expect( form.labeledInput.value ).equals( 'foo bar' );
  72. } );
  73. it( 'should set empty text to textarea and select it when there is no alt attribute', () => {
  74. const spy = sinon.spy( form.labeledInput, 'select' );
  75. setData( doc, '[<image src=""></image>]' );
  76. button.fire( 'execute' );
  77. sinon.assert.calledOnce( spy );
  78. expect( form.labeledInput.value ).equals( '' );
  79. } );
  80. } );
  81. describe( 'balloon panel form', () => {
  82. // https://github.com/ckeditor/ckeditor5-image/issues/114
  83. it( 'should make sure the input always stays in sync with the value of the command', () => {
  84. const button = editor.ui.componentFactory.create( 'imageTextAlternative' );
  85. // Mock the value of the input after some past editing.
  86. form.labeledInput.value = 'foo';
  87. // Mock the user using the form, changing the value but clicking "Cancel".
  88. // so the command's value is not updated.
  89. form.labeledInput.inputView.element.value = 'This value was canceled.';
  90. // Mock the user editing the same image once again.
  91. setData( doc, '[<image src="" alt="foo"></image>]' );
  92. button.fire( 'execute' );
  93. expect( form.labeledInput.inputView.element.value ).to.equal( 'foo' );
  94. } );
  95. it( 'should execute command on submit', () => {
  96. const spy = sinon.spy( editor, 'execute' );
  97. form.fire( 'submit' );
  98. sinon.assert.calledOnce( spy );
  99. sinon.assert.calledWithExactly( spy, 'imageTextAlternative', {
  100. newValue: form.labeledInput.inputView.element.value
  101. } );
  102. } );
  103. it( 'should hide the panel on cancel and focus the editing view', () => {
  104. const spy = sinon.spy( editor.editing.view, 'focus' );
  105. setData( doc, '[<image src="" alt="foo bar"></image>]' );
  106. editor.ui.componentFactory.create( 'imageTextAlternative' ).fire( 'execute' );
  107. expect( balloon.visibleView ).to.equal( form );
  108. form.fire( 'cancel' );
  109. expect( balloon.visibleView ).to.be.null;
  110. sinon.assert.calledOnce( spy );
  111. } );
  112. it( 'should not engage when the form is in the balloon yet invisible', () => {
  113. setData( doc, '[<image src=""></image>]' );
  114. button.fire( 'execute' );
  115. expect( balloon.visibleView ).to.equal( form );
  116. const lastView = new View();
  117. lastView.element = document.createElement( 'div' );
  118. balloon.add( { view: lastView } );
  119. expect( balloon.visibleView ).to.equal( lastView );
  120. button.fire( 'execute' );
  121. expect( balloon.visibleView ).to.equal( lastView );
  122. } );
  123. describe( 'integration with the editor selection (#render event)', () => {
  124. it( 'should re-position the form', () => {
  125. setData( doc, '[<image src=""></image>]' );
  126. button.fire( 'execute' );
  127. const spy = sinon.spy( balloon, 'updatePosition' );
  128. editingView.fire( 'render' );
  129. sinon.assert.calledOnce( spy );
  130. } );
  131. it( 'should hide the form when image widget has been removed by external change', () => {
  132. setData( doc, '[<image src=""></image>]' );
  133. button.fire( 'execute' );
  134. const spy = sinon.spy( balloon, 'remove' );
  135. // EnqueueChanges automatically calls #render event.
  136. doc.enqueueChanges( () => {
  137. doc.batch( 'transparent' ).remove( doc.selection.getFirstRange() );
  138. } );
  139. sinon.assert.calledWithExactly( spy, form );
  140. } );
  141. } );
  142. describe( 'integration with the editor focus', () => {
  143. it( 'should hide the toolbar when the editor loses focus and the image is selected', () => {
  144. editor.ui.focusTracker.isFocused = true;
  145. setData( doc, '[<image src=""></image>]' );
  146. button.fire( 'execute' );
  147. const spy = sinon.spy( balloon, 'remove' );
  148. editor.ui.focusTracker.isFocused = false;
  149. sinon.assert.calledWithExactly( spy, form );
  150. } );
  151. } );
  152. describe( 'close listeners', () => {
  153. describe( 'keyboard', () => {
  154. it( 'should close upon Esc key press and focus the editing view', () => {
  155. const hideSpy = sinon.spy( plugin, '_hideForm' );
  156. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  157. setData( doc, '[<image src=""></image>]' );
  158. button.fire( 'execute' );
  159. const keyEvtData = {
  160. keyCode: keyCodes.esc,
  161. preventDefault: sinon.spy(),
  162. stopPropagation: sinon.spy()
  163. };
  164. form.keystrokes.press( keyEvtData );
  165. sinon.assert.calledOnce( hideSpy );
  166. sinon.assert.calledOnce( keyEvtData.preventDefault );
  167. sinon.assert.calledOnce( focusSpy );
  168. } );
  169. } );
  170. describe( 'mouse', () => {
  171. it( 'should close and not focus editable on click outside the panel', () => {
  172. const hideSpy = sinon.spy( plugin, '_hideForm' );
  173. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  174. setData( doc, '[<image src=""></image>]' );
  175. button.fire( 'execute' );
  176. global.document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  177. sinon.assert.called( hideSpy );
  178. sinon.assert.notCalled( focusSpy );
  179. } );
  180. it( 'should not close on click inside the panel', () => {
  181. const spy = sinon.spy( plugin, '_hideForm' );
  182. setData( doc, '[<image src=""></image>]' );
  183. button.fire( 'execute' );
  184. form.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  185. sinon.assert.notCalled( spy );
  186. } );
  187. } );
  188. } );
  189. } );
  190. } );