8
0

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