basepath.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const modules = bender.amd.require( 'ckeditor' );
  7. beforeEach( () => {
  8. // Ensure that no CKEDITOR_BASEPATH global is available.
  9. delete window.CKEDITOR_BASEPATH;
  10. // Remove all script elements from the document.
  11. removeScripts();
  12. } );
  13. describe( 'basePath', () => {
  14. testGetBasePathFromTag( 'http://bar.com/ckeditor/ckeditor.js', 'http://bar.com/ckeditor/' );
  15. testGetBasePathFromTag( '/ckeditor/ckeditor.js', /\/ckeditor\/$/ );
  16. testGetBasePathFromTag( '/ckeditor/ckeditor.js?foo=1#bar', /\/ckeditor\/$/ );
  17. testGetBasePathFromTag( '/ckeditor/ckeditor.js;id=foo-bar', /\/ckeditor\/$/ );
  18. testGetBasePathFromTag( '/ckeditor/CKEDITOR.JS', /\/ckeditor\/$/ );
  19. testGetBasePathFromTag( '../ckeditor/foo/ckeditor.JS', /\/ckeditor\/foo\/$/ );
  20. it( 'should work with the CKEDITOR_BASEPATH global', () => {
  21. const CKEDITOR = modules.ckeditor;
  22. window.CKEDITOR_BASEPATH = 'http://foo.com/ckeditor/';
  23. expect( CKEDITOR._getBasePath() ).to.equal( 'http://foo.com/ckeditor/' );
  24. } );
  25. function testGetBasePathFromTag( url, expectedBasePath ) {
  26. it( 'should work with script tags - ' + url, () => {
  27. const CKEDITOR = modules.ckeditor;
  28. addScript( url );
  29. if ( typeof expectedBasePath == 'string' ) {
  30. expect( CKEDITOR._getBasePath() ).to.equal( expectedBasePath );
  31. } else {
  32. expect( CKEDITOR._getBasePath() ).to.match( expectedBasePath );
  33. }
  34. } );
  35. }
  36. } );
  37. describe( 'This browser', () => {
  38. testUrlIsFull( '/absolute/url/ckeditor.js' );
  39. testUrlIsFull( '../relative/url/ckeditor.js' );
  40. // Browsers should convert absolute and relative URLs to full URLs.
  41. // If this test fails in any browser, _getBasePath() must be reviewed to deal with such case (v4 does it).
  42. function testUrlIsFull( url ) {
  43. it( 'should not keep script URLs absolute or relative - ' + url, () => {
  44. removeScripts();
  45. const script = addScript( url );
  46. // Test if the src now contains '://'.
  47. expect( script.src ).to.match( /:\/\// );
  48. } );
  49. }
  50. } );
  51. function addScript( url ) {
  52. const script = document.createElement( 'script' );
  53. script.src = url;
  54. document.head.appendChild( script );
  55. return script;
  56. }
  57. function removeScripts() {
  58. const scripts = [].slice.call( document.getElementsByTagName( 'script' ) );
  59. let script;
  60. while ( ( script = scripts.shift() ) ) {
  61. script.parentNode.removeChild( script );
  62. }
  63. }