From da52d7ca36d780476c0c0715f4fee1cf2a1bad78 Mon Sep 17 00:00:00 2001 From: Ed Mackey Date: Sat, 18 Nov 2017 10:55:37 -0500 Subject: [PATCH 1/3] Add support for the file scheme to joinUrls. --- Source/Core/joinUrls.js | 4 +++ Specs/Core/joinUrlsSpec.js | 73 +++++++++++++++++++++++++++++++++----- 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/Source/Core/joinUrls.js b/Source/Core/joinUrls.js index a6a71faee95c..5d214e97121c 100644 --- a/Source/Core/joinUrls.js +++ b/Source/Core/joinUrls.js @@ -78,6 +78,10 @@ define([ if (baseUri.path !== '' && baseUri.path !== '/') { url = url.replace(/\/?$/, '/'); baseUri.path = baseUri.path.replace(/^\/?/g, ''); + + if (baseUri.authority === '') { + url += '/'; + } } } diff --git a/Specs/Core/joinUrlsSpec.js b/Specs/Core/joinUrlsSpec.js index 99a05a52793a..e0e4c4c48749 100644 --- a/Specs/Core/joinUrlsSpec.js +++ b/Specs/Core/joinUrlsSpec.js @@ -11,7 +11,14 @@ defineSuite([ var qualifiedUrl = "http://www.url.com"; var qualifiedUrlWithQueryString = "http://www.url.com" + "?" + queryString; - var qualifiedUrlWithPath = "http://www.url.com/some/path"; + var qualifiedUrlWithPath = "http://www.url.com/some/qualified/path"; + + // Local files require three slashes on the front, to leave the hostname blank. + // Windows is leanient so long as a drive letter is present. Other systems are not. + var localFileUrlWithPath = "file:///mnt/local/path"; + var driveUrlWithPath = "file:///c:/some/drive/path"; + // Files can be read from network shares with only two slashes on the front. + var networkUrlWithPath = "file://networkShareHostname/some/remote/path"; var absolutePath = "/some/path"; var absolutePathWithQueryString = absolutePath + "?" + queryString; @@ -56,7 +63,7 @@ defineSuite([ }); it('appends relative path correctly to qualified url', function() { - var result = joinUrls(qualifiedUrl, absolutePath); + var result = joinUrls(qualifiedUrl, relativePath); expect(result).toEqual(expectedQualifiedUrl); }); @@ -95,14 +102,9 @@ defineSuite([ expect(result).toEqual(qualifiedUrl); }); - it('appends qualfied url correctly to qualified url with path', function() { - var result = joinUrls(qualifiedUrl, qualifiedUrlWithPath); - expect(result).toEqual(expectedQualifiedUrl); - }); - it('appends qualfied url with path correctly to qualified url', function() { var result = joinUrls(qualifiedUrl, qualifiedUrlWithPath); - expect(result).toEqual(expectedQualifiedUrl); + expect(result).toEqual(qualifiedUrlWithPath); }); it('appends qualfied url with path correctly to qualified url with path', function() { @@ -168,4 +170,59 @@ defineSuite([ result = joinUrls(absolutePath, dataUri); expect(result).toEqual(dataUri); }); + + // File scheme tests + // + // NOTE: Following example set by 'appends absolute path correctly to qualified url with path', + // the so-called 'absolutePath' is expected to simply append to the existing path, not reset it. + + it('appends absolute path correctly to local file with path', function() { + var result = joinUrls(localFileUrlWithPath, absolutePath); + expect(result).toEqual(localFileUrlWithPath + absolutePath); + }); + + it('appends relative path correctly to local file with path', function() { + var result = joinUrls(localFileUrlWithPath, relativePath); + expect(result).toEqual(localFileUrlWithPath + absolutePath); + }); + + it('appends absolute path correctly to drive letter with path', function() { + var result = joinUrls(driveUrlWithPath, absolutePath); + expect(result).toEqual(driveUrlWithPath + absolutePath); + }); + + it('appends relative path correctly to drive letter with path', function() { + var result = joinUrls(driveUrlWithPath, relativePath); + expect(result).toEqual(driveUrlWithPath + absolutePath); + }); + + it('appends absolute path correctly to network share with path', function() { + var result = joinUrls(networkUrlWithPath, absolutePath); + expect(result).toEqual(networkUrlWithPath + absolutePath); + }); + + it('appends relative path correctly to network share with path', function() { + var result = joinUrls(networkUrlWithPath, relativePath); + expect(result).toEqual(networkUrlWithPath + absolutePath); + }); + + it('works when the file scheme appears in the second path', function() { + var result = joinUrls(driveUrlWithPath, localFileUrlWithPath); + expect(result).toEqual(localFileUrlWithPath); + }); + + it('works when a drive letter appears in the second path', function() { + var result = joinUrls(localFileUrlWithPath, driveUrlWithPath); + expect(result).toEqual(driveUrlWithPath); + }); + + it('works when the first is a network file and the second is a local file', function() { + var result = joinUrls(networkUrlWithPath, localFileUrlWithPath); + expect(result).toEqual(localFileUrlWithPath); + }); + + it('works when the first is a local file and the second is a network file', function() { + var result = joinUrls(localFileUrlWithPath, networkUrlWithPath); + expect(result).toEqual(networkUrlWithPath); + }); }); From d4e52c49f5ddea990bb65c486baf836e9b2718be Mon Sep 17 00:00:00 2001 From: Ed Mackey Date: Sat, 18 Nov 2017 14:57:18 -0500 Subject: [PATCH 2/3] CHANGES.md --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 1b2944da55b8..b7bc09ef475c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,7 @@ Change Log ### 1.40 - 2017-12-01 * Added ability to support touch event in Imagery Layers Split demo application. [#5948](https://github.com/AnalyticalGraphicsInc/cesium/pull/5948) +* Added `file:` scheme compatibility to `joinUrls`. [#5989](https://github.com/AnalyticalGraphicsInc/cesium/pull/5989) * Fixed `Invalid asm.js: Invalid member of stdlib` console error by recompiling crunch.js with latest emscripten toolchain. [#5847](https://github.com/AnalyticalGraphicsInc/cesium/issues/5847) ### 1.39 - 2017-11-01 From d073e6555bb997b59cc9465781aeabc2e44ff18e Mon Sep 17 00:00:00 2001 From: Ed Mackey Date: Tue, 28 Nov 2017 14:44:29 -0500 Subject: [PATCH 3/3] Add comments --- Source/Core/joinUrls.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Source/Core/joinUrls.js b/Source/Core/joinUrls.js index 5d214e97121c..c9a619840729 100644 --- a/Source/Core/joinUrls.js +++ b/Source/Core/joinUrls.js @@ -76,9 +76,12 @@ define([ url += '//' + baseUri.authority; if (baseUri.path !== '' && baseUri.path !== '/') { + // The next line ensures that url (including a non-blank authority) ends with a slash. url = url.replace(/\/?$/, '/'); baseUri.path = baseUri.path.replace(/^\/?/g, ''); + // If authority is empty, add a third slash. This is primarily for the file scheme, + // where a blank authority indicates a file on localhost (as opposed to a network share). if (baseUri.authority === '') { url += '/'; }