basepath.js 2.5 KB

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