basepath.js 2.6 KB

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