utils.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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' +
  70. 'for "image-upload-integrations-invalid-view" error', async () => {
  71. const editorElement = document.createElement( 'div' );
  72. document.body.appendChild( editorElement );
  73. const consoleWarn = sinon.stub( console, 'warn' );
  74. const editor = await ClassicEditor
  75. .create( editorElement, {
  76. plugins: [
  77. Paragraph,
  78. Image,
  79. ImageUploadEditing,
  80. ImageUploadUI
  81. ],
  82. image: {
  83. upload: {
  84. panel: {
  85. items: [
  86. 'insertImageViaUrl',
  87. 'openCKFinder'
  88. ]
  89. }
  90. }
  91. }
  92. } );
  93. expect( Object.values( prepareIntegrations( editor ) ).length ).to.equal( 1 );
  94. sinon.assert.calledOnce( consoleWarn );
  95. expect( /image-upload-integrations-invalid-view/gmi.test( consoleWarn.args[ 0 ][ 0 ] ) ).to.be.true;
  96. editor.destroy();
  97. editorElement.remove();
  98. } );
  99. it( 'should return only "link" integration', async () => {
  100. const editorElement = document.createElement( 'div' );
  101. document.body.appendChild( editorElement );
  102. const editor = await ClassicEditor
  103. .create( editorElement, {
  104. plugins: [
  105. Paragraph,
  106. Link,
  107. Image,
  108. ImageUploadEditing,
  109. ImageUploadUI
  110. ],
  111. image: {
  112. upload: {
  113. panel: {
  114. items: [
  115. 'link'
  116. ]
  117. }
  118. }
  119. }
  120. } );
  121. expect( Object.values( prepareIntegrations( editor ) ).length ).to.equal( 1 );
  122. expect( Object.values( prepareIntegrations( editor ) )[ 0 ].label ).to.equal( 'Link' );
  123. expect( Object.values( prepareIntegrations( editor ) )[ 0 ] ).to.be.instanceOf( ButtonView );
  124. editor.destroy();
  125. editorElement.remove();
  126. } );
  127. it( 'should return "insertImageViaUrl" integration, when no integrations were configured', async () => {
  128. const editorElement = document.createElement( 'div' );
  129. document.body.appendChild( editorElement );
  130. const editor = await ClassicEditor
  131. .create( editorElement, {
  132. plugins: [
  133. Paragraph,
  134. Image,
  135. ImageUploadEditing,
  136. ImageUploadUI
  137. ]
  138. } );
  139. expect( Object.keys( prepareIntegrations( editor ) ).length ).to.equal( 1 );
  140. editor.destroy();
  141. editorElement.remove();
  142. } );
  143. } );
  144. describe( 'createLabeledInputView()', () => {
  145. describe( 'image URL input view', () => {
  146. it( 'should have placeholder', () => {
  147. const view = createLabeledInputView( { t: val => val } );
  148. expect( view.fieldView.placeholder ).to.equal( 'https://example.com/src/image.png' );
  149. } );
  150. it( 'should have info text', () => {
  151. const view = createLabeledInputView( { t: val => val } );
  152. expect( view.infoText ).to.match( /^Paste the image source URL/ );
  153. } );
  154. } );
  155. } );
  156. } );