basepath.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. it( 'should not keep script URLs absolute or relative', function() {
  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. test( '/absolute/url/ckeditor.js' );
  44. test( '../relative/url/ckeditor.js' );
  45. function test( url ) {
  46. removeScripts();
  47. var script = addScript( url );
  48. // Test if the src now contains '://'.
  49. expect( /:\/\//.test( script.src ) ).to.be.true();
  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. }