basepath.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const modules = bender.amd.require( 'ckeditor5/path' );
  7. let path;
  8. before( () => {
  9. path = modules[ 'ckeditor5/path' ];
  10. } );
  11. beforeEach( () => {
  12. // Ensure that no CKEDITOR_BASEPATH global is available.
  13. delete window.CKEDITOR_BASEPATH;
  14. // Remove all script elements from the document.
  15. removeScripts();
  16. } );
  17. describe( 'basePath', () => {
  18. testGetBasePathFromTag( 'http://bar.com/ckeditor/ckeditor.js', 'http://bar.com/ckeditor/' );
  19. testGetBasePathFromTag( '/ckeditor/ckeditor.js', /\/ckeditor\/$/ );
  20. testGetBasePathFromTag( '/ckeditor/ckeditor.js?foo=1#bar', /\/ckeditor\/$/ );
  21. testGetBasePathFromTag( '/ckeditor/ckeditor.js;id=foo-bar', /\/ckeditor\/$/ );
  22. testGetBasePathFromTag( '/ckeditor/CKEDITOR.JS', /\/ckeditor\/$/ );
  23. testGetBasePathFromTag( '../ckeditor/foo/ckeditor.JS', /\/ckeditor\/foo\/$/ );
  24. it( 'should work with the CKEDITOR_BASEPATH global', () => {
  25. window.CKEDITOR_BASEPATH = 'http://foo.com/ckeditor/';
  26. expect( path._getBasePath() ).to.equal( 'http://foo.com/ckeditor/' );
  27. } );
  28. function testGetBasePathFromTag( url, expectedBasePath ) {
  29. it( 'should work with script tags - ' + url, () => {
  30. addScript( url );
  31. if ( typeof expectedBasePath == 'string' ) {
  32. expect( path._getBasePath() ).to.equal( expectedBasePath );
  33. } else {
  34. expect( path._getBasePath() ).to.match( expectedBasePath );
  35. }
  36. } );
  37. }
  38. } );
  39. describe( 'This browser', () => {
  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, () => {
  46. removeScripts();
  47. const script = addScript( url );
  48. // Test if the src now contains '://'.
  49. expect( script.src ).to.match( /:\/\// );
  50. } );
  51. }
  52. } );
  53. function addScript( url ) {
  54. const script = document.createElement( 'script' );
  55. script.src = url;
  56. document.head.appendChild( script );
  57. return script;
  58. }
  59. function removeScripts() {
  60. const scripts = [].slice.call( document.getElementsByTagName( 'script' ) );
  61. let script;
  62. while ( ( script = scripts.shift() ) ) {
  63. script.parentNode.removeChild( script );
  64. }
  65. }