basepath.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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( 'basePath', function() {
  14. it( 'should work with script tags', function( done ) {
  15. CKEDITOR.require( [ 'ckeditor' ], function( CKEDITOR ) {
  16. addScript( 'http://bar.com/ckeditor/ckeditor.js' );
  17. expect( CKEDITOR._getBasePath() ).to.equal( 'http://bar.com/ckeditor/' );
  18. done();
  19. } );
  20. } );
  21. it( 'should work with the CKEDITOR_BASEPATH global', function( done ) {
  22. CKEDITOR.require( [ 'ckeditor' ], function( CKEDITOR ) {
  23. window.CKEDITOR_BASEPATH = 'http://foo.com/ckeditor/';
  24. expect( CKEDITOR._getBasePath() ).to.equal( 'http://foo.com/ckeditor/' );
  25. done();
  26. } );
  27. } );
  28. } );
  29. describe( 'This browser', function() {
  30. it( 'should not keep script URLs absolute or relative', function( done ) {
  31. // Browsers should convert absolute and relative URLs to full URLs.
  32. // If this test fails in any browser, _getBasePath() must be reviewed to deal with such case (v4 does it).
  33. test( '/absolute/url/ckeditor.js' );
  34. test( '../relative/url/ckeditor.js' );
  35. done();
  36. function test( url ) {
  37. removeScripts();
  38. var script = addScript( url );
  39. // Test if the src now contains '://'.
  40. expect( /:\/\//.test( script.src ) ).to.be.true();
  41. }
  42. } );
  43. } );
  44. function addScript( url ) {
  45. var script = document.createElement( 'script' );
  46. script.src = url;
  47. document.head.appendChild( script );
  48. return script;
  49. }
  50. function removeScripts() {
  51. var scripts = [].slice.call( document.getElementsByTagName( 'script' ) );
  52. var script;
  53. while ( ( script = scripts.shift() ) ) {
  54. script.parentNode.removeChild( script );
  55. }
  56. }