8
0

path.js 2.3 KB

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