8
0

path.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const path = {
  7. /**
  8. * TODO: review whether this property is needed https://github.com/ckeditor/ckeditor5/issues/61.
  9. *
  10. * The full URL for the CKEditor installation directory.
  11. *
  12. * It is possible to manually provide the base path by setting a global variable named `CKEDITOR_BASEPATH`. This
  13. * global variable must be set **before** the editor script loading.
  14. *
  15. * console.log( CKEDITOR.basePath ); // e.g. 'http://www.example.com/ckeditor/'
  16. *
  17. * @readonly
  18. * @property {String}
  19. */
  20. basePath: getBasePath(),
  21. /**
  22. * Computes the value of the `basePath` property.
  23. *
  24. * @private
  25. * @method
  26. * @returns {String} A full URL.
  27. */
  28. _getBasePath: getBasePath
  29. };
  30. function getBasePath() {
  31. if ( window.CKEDITOR_BASEPATH ) {
  32. return window.CKEDITOR_BASEPATH;
  33. }
  34. const scripts = document.getElementsByTagName( 'script' );
  35. const basePathSrcPattern = /(^|.*[\\\/])ckeditor\.js(?:\?.*|;.*)?$/i;
  36. let path;
  37. // Find the first script that src matches ckeditor.js.
  38. Array.from( scripts ).some( ( script ) => {
  39. const match = script.src.match( basePathSrcPattern );
  40. if ( match ) {
  41. path = match[ 1 ];
  42. return true;
  43. }
  44. } );
  45. return path;
  46. }
  47. export default path;