basepath.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals beforeEach, describe, it, expect, CKEDITOR, window, document */
  6. 'use strict';
  7. beforeEach( function() {
  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( 'ckeditor.basePath', function() {
  14. it( 'Full URL', function( done ) {
  15. CKEDITOR.require( [ 'ckeditor' ], function( CKEDITOR ) {
  16. addScript( 'http://bar.com/ckeditor/ckeditor.js' );
  17. expect( CKEDITOR._getBasePath() ).equals( 'http://bar.com/ckeditor/' );
  18. done();
  19. } );
  20. } );
  21. it( 'CKEDITOR_BASEPATH', function( done ) {
  22. CKEDITOR.require( [ 'ckeditor' ], function( CKEDITOR ) {
  23. window.CKEDITOR_BASEPATH = 'http://foo.com/ckeditor/';
  24. expect( CKEDITOR._getBasePath() ).equals( 'http://foo.com/ckeditor/' );
  25. done();
  26. } );
  27. } );
  28. it( 'Ensure that no browser keep script URLs absolute or relative', function( done ) {
  29. // Browsers should convert absolute and relative URLs to full URLs.
  30. // If this test fails in any browser, _getBasePath() must be reviewed to deal with such case (v4 does it).
  31. test( '/absolute/url/ckeditor.js' );
  32. test( '../relative/url/ckeditor.js' );
  33. done();
  34. function test( url ) {
  35. removeScripts();
  36. var script = addScript( url );
  37. // Test if the src now contains '://'.
  38. expect( /:\/\//.test( script.src ) ).to.be.true();
  39. }
  40. } );
  41. } );
  42. function addScript( url ) {
  43. var script = document.createElement( 'script' );
  44. script.src = url;
  45. document.head.appendChild( script );
  46. return script;
  47. }
  48. function removeScripts() {
  49. var scripts = [].slice.call( document.getElementsByTagName( 'script' ) );
  50. var script;
  51. while ( ( script = scripts.shift() ) ) {
  52. script.parentNode.removeChild( script );
  53. }
  54. }