utils.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document, console */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  8. import Image from '../../src/image';
  9. import ImageUploadUI from '../../src/imageupload/imageuploadui';
  10. import ImageUploadEditing from '../../src/imageupload/imageuploadediting';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import Link from '@ckeditor/ckeditor5-link/src/link';
  13. import CKFinder from '@ckeditor/ckeditor5-ckfinder/src/ckfinder';
  14. import { createImageTypeRegExp, prepareIntegrations, createLabeledInputView } from '../../src/imageupload/utils';
  15. describe( 'Upload utils', () => {
  16. describe( 'createImageTypeRegExp()', () => {
  17. it( 'should return RegExp for testing regular mime type', () => {
  18. expect( createImageTypeRegExp( [ 'png' ] ).test( 'image/png' ) ).to.be.true;
  19. expect( createImageTypeRegExp( [ 'png' ] ).test( 'foo/png' ) ).to.be.false;
  20. expect( createImageTypeRegExp( [ 'png' ] ).test( 'png' ) ).to.be.false;
  21. } );
  22. it( 'should return RegExp for testing mime type with dot', () => {
  23. expect( createImageTypeRegExp( [ 'vnd.microsoft.icon' ] ).test( 'image/vnd.microsoft.icon' ) ).to.be.true;
  24. expect( createImageTypeRegExp( [ 'png' ] ).test( 'foo/vnd.microsoft.icon' ) ).to.be.false;
  25. expect( createImageTypeRegExp( [ 'png' ] ).test( 'vnd.microsoft.icon' ) ).to.be.false;
  26. } );
  27. it( 'should return RegExp for testing mime type with dash', () => {
  28. expect( createImageTypeRegExp( [ 'x-xbitmap' ] ).test( 'image/x-xbitmap' ) ).to.be.true;
  29. expect( createImageTypeRegExp( [ 'png' ] ).test( 'foo/x-xbitmap' ) ).to.be.false;
  30. expect( createImageTypeRegExp( [ 'png' ] ).test( 'x-xbitmap' ) ).to.be.false;
  31. } );
  32. it( 'should return RegExp for testing mime type with plus', () => {
  33. expect( createImageTypeRegExp( [ 'svg+xml' ] ).test( 'image/svg+xml' ) ).to.be.true;
  34. expect( createImageTypeRegExp( [ 'png' ] ).test( 'foo/svg+xml' ) ).to.be.false;
  35. expect( createImageTypeRegExp( [ 'png' ] ).test( 'svg+xml' ) ).to.be.false;
  36. } );
  37. } );
  38. describe( 'prepareIntegrations()', () => {
  39. it( 'should return "insetImageViaUrl" and "openCKFinder" integrations', async () => {
  40. const editorElement = document.createElement( 'div' );
  41. document.body.appendChild( editorElement );
  42. const editor = await ClassicEditor
  43. .create( editorElement, {
  44. plugins: [
  45. CKFinder,
  46. Paragraph,
  47. Image,
  48. ImageUploadEditing,
  49. ImageUploadUI
  50. ],
  51. image: {
  52. upload: {
  53. panel: {
  54. items: [
  55. 'insertImageViaUrl',
  56. 'openCKFinder'
  57. ]
  58. }
  59. }
  60. }
  61. } );
  62. const openCKFinderExtendedView = Object.values( prepareIntegrations( editor ) )[ 1 ];
  63. expect( openCKFinderExtendedView.class ).contains( 'ck-image-upload__ck-finder-button' );
  64. expect( openCKFinderExtendedView.label ).to.equal( 'Insert image or file' );
  65. expect( openCKFinderExtendedView.withText ).to.be.true;
  66. editor.destroy();
  67. editorElement.remove();
  68. } );
  69. it( 'should return only "insertImageViaUrl" integration and throw warning about missing integration', async () => {
  70. const editorElement = document.createElement( 'div' );
  71. document.body.appendChild( editorElement );
  72. const consoleWarn = sinon.stub( console, 'warn' );
  73. const editor = await ClassicEditor
  74. .create( editorElement, {
  75. plugins: [
  76. Paragraph,
  77. Image,
  78. ImageUploadEditing,
  79. ImageUploadUI
  80. ],
  81. image: {
  82. upload: {
  83. panel: {
  84. items: [
  85. 'insertImageViaUrl',
  86. 'openCKFinder'
  87. ]
  88. }
  89. }
  90. }
  91. } );
  92. expect( Object.values( prepareIntegrations( editor ) ).length ).to.equal( 1 );
  93. sinon.assert.calledOnce( consoleWarn );
  94. expect( /Integration "openCKFinder" doesn't exist/gmi.test( consoleWarn.args[ 0 ][ 0 ] ) ).to.be.true;
  95. editor.destroy();
  96. editorElement.remove();
  97. } );
  98. it( 'should return only "link" integration', async () => {
  99. const editorElement = document.createElement( 'div' );
  100. document.body.appendChild( editorElement );
  101. const editor = await ClassicEditor
  102. .create( editorElement, {
  103. plugins: [
  104. Paragraph,
  105. Link,
  106. Image,
  107. ImageUploadEditing,
  108. ImageUploadUI
  109. ],
  110. image: {
  111. upload: {
  112. panel: {
  113. items: [
  114. 'link'
  115. ]
  116. }
  117. }
  118. }
  119. } );
  120. expect( Object.values( prepareIntegrations( editor ) ).length ).to.equal( 1 );
  121. expect( Object.values( prepareIntegrations( editor ) )[ 0 ].label ).to.equal( 'Link' );
  122. expect( Object.values( prepareIntegrations( editor ) )[ 0 ] ).to.be.instanceOf( ButtonView );
  123. editor.destroy();
  124. editorElement.remove();
  125. } );
  126. it( 'should return "insertImageViaUrl" integration, when no integrations were configured', async () => {
  127. const editorElement = document.createElement( 'div' );
  128. document.body.appendChild( editorElement );
  129. const editor = await ClassicEditor
  130. .create( editorElement, {
  131. plugins: [
  132. Paragraph,
  133. Image,
  134. ImageUploadEditing,
  135. ImageUploadUI
  136. ]
  137. } );
  138. expect( Object.keys( prepareIntegrations( editor ) ).length ).to.equal( 1 );
  139. editor.destroy();
  140. editorElement.remove();
  141. } );
  142. } );
  143. describe( 'createLabeledInputView()', () => {
  144. describe( 'image URL input view', () => {
  145. it( 'should have placeholder', () => {
  146. const view = createLabeledInputView( { t: val => val } );
  147. expect( view.fieldView.placeholder ).to.equal( 'https://example.com/src/image.png' );
  148. } );
  149. it( 'should have info text', () => {
  150. const view = createLabeledInputView( { t: val => val } );
  151. expect( view.infoText ).to.match( /^Paste the image source URL/ );
  152. } );
  153. } );
  154. } );
  155. } );