Permalink
Browse files

Reworked documentBaseURL detection logic

  • Loading branch information...
spocke committed Apr 7, 2016
1 parent a0ffaac commit 1483e4fd47ab58b5fc1015f82253c90caaeedc44
Showing with 36 additions and 1 deletion.
  1. +1 −1 js/tinymce/classes/EditorManager.js
  2. +21 −0 js/tinymce/classes/util/URI.js
  3. +14 −0 tests/tinymce/util/URI.js
@@ -162,7 +162,7 @@ define("tinymce/EditorManager", [
var self = this, baseURL, documentBaseURL, suffix = "", preInit, src;
// Get base URL for the current document
- documentBaseURL = document.location.href;
+ documentBaseURL = URI.getDocumentBaseUrl(document.location);
// Check if the URL is a document based format like: http://site/dir/file and file:///
// leave other formats like applewebdata://... intact
@@ -407,5 +407,26 @@ define("tinymce/util/URI", [
};
};
+ URI.getDocumentBaseUrl = function(loc) {
+ var baseUrl;
+
+ // Pass applewebdata:// and other non web protocols though
+ if (loc.protocol.indexOf('http') !== 0 && loc.protocol !== 'file:') {
+ baseUrl = loc.href;
+ } else {
+ baseUrl = loc.protocol + '//' + loc.host + loc.pathname;
+ }
+
+ if (/^[^:]+:\/\/\/?[^\/]+\//.test(baseUrl)) {
+ baseUrl = baseUrl.replace(/[\?#].*$/, '').replace(/[\/\\][^\/]+$/, '');
+
+ if (!/[\/\\]$/.test(baseUrl)) {
+ baseUrl += '/';
+ }
+ }
+
+ return baseUrl;
+ };
+
return URI;
});
View
@@ -102,3 +102,17 @@ test('isSameOrigin', function() {
ok(new tinymce.util.URI('https://www.site.com:8080').isSameOrigin(new tinymce.util.URI('https://www.site.com')) == false);
ok(new tinymce.util.URI('ftp://www.site.com:1021').isSameOrigin(new tinymce.util.URI('ftp://www.site.com')) == false);
});
+
+test('getDocumentBaseUrl', function() {
+ var getDocumentBaseUrl = tinymce.util.URI.getDocumentBaseUrl;
+
+ equal(getDocumentBaseUrl({protocol: 'file:', host: '', pathname: '/dir/path1/path2'}), 'file:///dir/path1/');
+ equal(getDocumentBaseUrl({protocol: 'http:', host: 'localhost', pathname: '/dir/path1/path2'}), 'http://localhost/dir/path1/');
+ equal(getDocumentBaseUrl({protocol: 'https:', host: 'localhost', pathname: '/dir/path1/path2'}), 'https://localhost/dir/path1/');
+ equal(getDocumentBaseUrl({protocol: 'https:', host: 'localhost', pathname: '/dir/path1/path2/'}), 'https://localhost/dir/path1/path2/');
+ equal(getDocumentBaseUrl({protocol: 'http:', host: 'localhost:8080', pathname: '/dir/path1/path2'}), 'http://localhost:8080/dir/path1/');
+ equal(getDocumentBaseUrl({protocol: 'http:', host: 'localhost', pathname: '/dir/path1/path2/file.html'}), 'http://localhost/dir/path1/path2/');
+ equal(getDocumentBaseUrl({protocol: 'http:', host: 'localhost', pathname: '/'}), 'http://localhost/');
+ equal(getDocumentBaseUrl({protocol: 'applewebdata:', href: 'applewebdata://something//dir/path1#hash'}), 'applewebdata://something//dir/');
+ equal(getDocumentBaseUrl({protocol: 'applewebdata:', href: 'applewebdata://something//dir/path1'}), 'applewebdata://something//dir/');
+});

0 comments on commit 1483e4f

Please sign in to comment.