8
0

path.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import path from '/ckeditor5/path.js';
  7. beforeEach( () => {
  8. // Ensure that no CKEDITOR_BASEPATH global is available.
  9. delete window.CKEDITOR_BASEPATH;
  10. // Remove all script elements from the document.
  11. removeScripts();
  12. } );
  13. describe( 'basePath', () => {
  14. testGetBasePathFromTag( 'http://bar.com/ckeditor/ckeditor.js', 'http://bar.com/ckeditor/' );
  15. testGetBasePathFromTag( '/ckeditor/ckeditor.js', /\/ckeditor\/$/ );
  16. testGetBasePathFromTag( '/ckeditor/ckeditor.js?foo=1#bar', /\/ckeditor\/$/ );
  17. testGetBasePathFromTag( '/ckeditor/ckeditor.js;id=foo-bar', /\/ckeditor\/$/ );
  18. testGetBasePathFromTag( '/ckeditor/CKEDITOR.JS', /\/ckeditor\/$/ );
  19. testGetBasePathFromTag( '../ckeditor/foo/ckeditor.JS', /\/ckeditor\/foo\/$/ );
  20. it( 'should work with the CKEDITOR_BASEPATH global', () => {
  21. window.CKEDITOR_BASEPATH = 'http://foo.com/ckeditor/';
  22. expect( path._getBasePath() ).to.equal( 'http://foo.com/ckeditor/' );
  23. } );
  24. function testGetBasePathFromTag( url, expectedBasePath ) {
  25. it( 'should work with script tags - ' + url, () => {
  26. addScript( url );
  27. if ( typeof expectedBasePath == 'string' ) {
  28. expect( path._getBasePath() ).to.equal( expectedBasePath );
  29. } else {
  30. expect( path._getBasePath() ).to.match( expectedBasePath );
  31. }
  32. } );
  33. }
  34. } );
  35. describe( 'This browser', () => {
  36. testUrlIsFull( '/absolute/url/ckeditor.js' );
  37. testUrlIsFull( '../relative/url/ckeditor.js' );
  38. // Browsers should convert absolute and relative URLs to full URLs.
  39. // If this test fails in any browser, _getBasePath() must be reviewed to deal with such case (v4 does it).
  40. function testUrlIsFull( url ) {
  41. it( 'should not keep script URLs absolute or relative - ' + url, () => {
  42. removeScripts();
  43. const script = addScript( url );
  44. // Test if the src now contains '://'.
  45. expect( script.src ).to.match( /:\/\// );
  46. } );
  47. }
  48. } );
  49. function addScript( url ) {
  50. const script = document.createElement( 'script' );
  51. script.src = url;
  52. document.head.appendChild( script );
  53. return script;
  54. }
  55. function removeScripts() {
  56. const scripts = [].slice.call( document.getElementsByTagName( 'script' ) );
  57. let script;
  58. while ( ( script = scripts.shift() ) ) {
  59. script.parentNode.removeChild( script );
  60. }
  61. }