8
0

imagealternatetext.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import Image from '../../src/image';
  7. import ImageToolbar from '../../src/imagetoolbar';
  8. import ImageAlternateText from '../../src/imagealternatetext/imagealternatetext';
  9. import ImageAlternateTextEngine from '../../src/imagealternatetext/imagealternatetextengine';
  10. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  11. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  12. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  14. /* global Event */
  15. describe( 'ImageAlternateText', () => {
  16. let editor, plugin, command, balloonPanel, form;
  17. beforeEach( () => {
  18. const editorElement = global.document.createElement( 'div' );
  19. global.document.body.appendChild( editorElement );
  20. return ClassicTestEditor.create( editorElement, {
  21. plugins: [ ImageAlternateText, Image ]
  22. } )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. newEditor.editing.view.attachDomRoot( editorElement );
  26. plugin = editor.plugins.get( ImageAlternateText );
  27. command = editor.commands.get( 'imageAlternateText' );
  28. balloonPanel = plugin.balloonPanel;
  29. form = plugin.form;
  30. } );
  31. } );
  32. afterEach( () => {
  33. return editor.destroy();
  34. } );
  35. it( 'should be loaded', () => {
  36. expect( plugin ).to.be.instanceOf( ImageAlternateText );
  37. } );
  38. it( 'should load ImageAlternateTextEngine plugin', () => {
  39. expect( editor.plugins.get( ImageAlternateTextEngine ) ).to.be.instanceOf( ImageAlternateTextEngine );
  40. } );
  41. describe( 'toolbar button', () => {
  42. let button;
  43. beforeEach( () => {
  44. button = editor.ui.componentFactory.create( 'imageAlternateText' );
  45. } );
  46. it( 'should be registered in component factory', () => {
  47. expect( button ).to.be.instanceOf( ButtonView );
  48. } );
  49. it( 'should have isEnabled property bind to command\'s isEnabled property', () => {
  50. command.isEnabled = true;
  51. expect( button.isEnabled ).to.be.true;
  52. command.isEnabled = false;
  53. expect( button.isEnabled ).to.be.false;
  54. } );
  55. it( 'should show balloon panel on execute', () => {
  56. const spy = sinon.spy( balloonPanel, 'attach' );
  57. setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
  58. button.fire( 'execute' );
  59. sinon.assert.calledOnce( spy );
  60. } );
  61. it( 'should set alt attribute value to textarea and select it', () => {
  62. const spy = sinon.spy( form.lebeledInput, 'select' );
  63. setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
  64. button.fire( 'execute' );
  65. sinon.assert.calledOnce( spy );
  66. expect( plugin.form.lebeledInput.value ).equals( 'foo bar' );
  67. } );
  68. it( 'should set empty text to textarea and select it when there is no alt attribute', () => {
  69. const spy = sinon.spy( form.lebeledInput, 'select' );
  70. setData( editor.document, '[<image src=""></image>]' );
  71. button.fire( 'execute' );
  72. sinon.assert.calledOnce( spy );
  73. expect( plugin.form.lebeledInput.value ).equals( '' );
  74. } );
  75. it( 'should not add button to default image toolbar if image toolbar is not present', () => {
  76. expect( editor.config.get( 'image.defaultToolbar' ) ).to.be.undefined;
  77. } );
  78. it( 'should add button to default image toolbar if toolbar is present', () => {
  79. const editorElement = global.document.createElement( 'div' );
  80. global.document.body.appendChild( editorElement );
  81. return ClassicTestEditor.create( editorElement, {
  82. plugins: [ ImageAlternateText, ImageToolbar ]
  83. } )
  84. .then( newEditor => {
  85. expect( newEditor.config.get( 'image.defaultToolbar' ) ).to.eql( [ 'imageAlternateText' ] );
  86. newEditor.destroy();
  87. } );
  88. } );
  89. } );
  90. describe( 'balloon panel form', () => {
  91. it( 'should execute command on submit', () => {
  92. const spy = sinon.spy( editor, 'execute' );
  93. form.fire( 'submit' );
  94. sinon.assert.calledOnce( spy );
  95. sinon.assert.calledWithExactly( spy, 'imageAlternateText', { newValue: form.lebeledInput.inputView.element.value } );
  96. } );
  97. it( 'should detach panel on cancel', () => {
  98. const spy = sinon.spy( balloonPanel, 'detach' );
  99. form.fire( 'cancel' );
  100. sinon.assert.called( spy );
  101. } );
  102. it( 'should show ImageToolbar on cancel if present', () => {
  103. const editorElement = global.document.createElement( 'div' );
  104. global.document.body.appendChild( editorElement );
  105. return ClassicTestEditor.create( editorElement, {
  106. plugins: [ ImageAlternateText, Image, ImageToolbar ],
  107. image: {
  108. toolbar: [ 'imageAlternateText' ]
  109. }
  110. } )
  111. .then( newEditor => {
  112. newEditor.editing.view.attachDomRoot( editorElement );
  113. const plugin = newEditor.plugins.get( ImageAlternateText );
  114. const toolbarPlugin = newEditor.plugins.get( ImageToolbar );
  115. const form = plugin.form;
  116. const spy = sinon.spy( toolbarPlugin, 'show' );
  117. form.fire( 'cancel' );
  118. sinon.assert.called( spy );
  119. } );
  120. } );
  121. describe( 'close listeners', () => {
  122. let hidePanelSpy;
  123. beforeEach( () => {
  124. hidePanelSpy = sinon.spy( balloonPanel, 'detach' );
  125. } );
  126. describe( 'keyboard', () => {
  127. it( 'should close after `ESC` press', () => {
  128. balloonPanel.isVisible = true;
  129. const keyCode = keyCodes.esc;
  130. const event = global.document.createEvent( 'Events' );
  131. event.initEvent( 'keydown', true, true );
  132. event.which = keyCode;
  133. event.keyCode = keyCode;
  134. global.document.dispatchEvent( event );
  135. sinon.assert.called( hidePanelSpy );
  136. } );
  137. } );
  138. describe( 'mouse', () => {
  139. it( 'should close and not focus editable on click outside the panel', () => {
  140. balloonPanel.isVisible = true;
  141. global.document.body.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  142. sinon.assert.called( hidePanelSpy );
  143. } );
  144. it( 'should not close on click inside the panel', () => {
  145. balloonPanel.isVisible = true;
  146. balloonPanel.element.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  147. sinon.assert.notCalled( hidePanelSpy );
  148. } );
  149. } );
  150. } );
  151. } );
  152. describe( 'working with ImageToolbar', () => {
  153. let editor, button, imageToolbarPlugin, plugin;
  154. beforeEach( () => {
  155. const editorElement = global.document.createElement( 'div' );
  156. global.document.body.appendChild( editorElement );
  157. return ClassicTestEditor.create( editorElement, {
  158. plugins: [ ImageAlternateText, Image, ImageToolbar ],
  159. image: {
  160. toolbar: [ 'imageAlternateText' ]
  161. }
  162. } )
  163. .then( newEditor => {
  164. editor = newEditor;
  165. editor.editing.view.attachDomRoot( editorElement );
  166. button = newEditor.ui.componentFactory.create( 'imageAlternateText' );
  167. imageToolbarPlugin = newEditor.plugins.get( ImageToolbar );
  168. plugin = editor.plugins.get( ImageAlternateText );
  169. } );
  170. } );
  171. afterEach( () => editor.destroy() );
  172. it( 'should hide ImageToolbar when visible', () => {
  173. setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
  174. const spy = sinon.spy( imageToolbarPlugin, 'hide' );
  175. button.fire( 'execute' );
  176. sinon.assert.calledOnce( spy );
  177. } );
  178. it( 'ImageToolbar should not show when alternate text panel is visible', () => {
  179. setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
  180. button.fire( 'execute' );
  181. const spy = sinon.spy( imageToolbarPlugin, 'show' );
  182. editor.editing.view.render();
  183. sinon.assert.notCalled( spy );
  184. } );
  185. it( 'ImageToolbar should show when alternate text panel is not visible', () => {
  186. setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
  187. button.fire( 'execute' );
  188. const spy = sinon.spy( imageToolbarPlugin, 'show' );
  189. plugin.balloonPanel.isVisible = false;
  190. sinon.assert.called( spy );
  191. } );
  192. } );
  193. } );