From 577740244a90e8d8ce50562417e4019acd17ba3a Mon Sep 17 00:00:00 2001 From: Matthew Amato Date: Tue, 23 May 2017 15:38:10 -0400 Subject: [PATCH 1/2] Fix KML descriptions that link to relative paths If a KML description included a relative path, the browser request would end up using whatever server the application was hosted on as the base path, this is because the KML HTML would be injected into the InfoBox as-is. The end result is that relative images and links inside of KML files would not load properly. To fix this, we now expand all relative paths into absolute paths that use the KML's source Uri as the bas. --- Source/DataSources/KmlDataSource.js | 24 +++++++++++++++++++----- Specs/DataSources/KmlDataSourceSpec.js | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/Source/DataSources/KmlDataSource.js b/Source/DataSources/KmlDataSource.js index 0807ee87b7bd..87f00c7f7a35 100644 --- a/Source/DataSources/KmlDataSource.js +++ b/Source/DataSources/KmlDataSource.js @@ -253,7 +253,7 @@ define([ }); } - function replaceAttributes(div, elementType, attributeName, uriResolver) { + function embedDataUris(div, elementType, attributeName, uriResolver) { var keys = uriResolver.keys; var baseUri = new Uri('.'); var elements = div.querySelectorAll(elementType); @@ -272,6 +272,16 @@ define([ } } + function applyBasePath(div, elementType, attributeName, proxy, sourceUri) { + var elements = div.querySelectorAll(elementType); + for (var i = 0; i < elements.length; i++) { + var element = elements[i]; + var value = element.getAttribute(attributeName); + var uri = resolveHref(value, proxy, sourceUri); + element.setAttribute(attributeName, uri); + } + } + function proxyUrl(url, proxy) { if (defined(proxy)) { if (new Uri(url).isAbsolute()) { @@ -1431,7 +1441,7 @@ define([ var scratchDiv = document.createElement('div'); - function processDescription(node, entity, styleEntity, uriResolver) { + function processDescription(node, entity, styleEntity, uriResolver, proxy, sourceUri) { var i; var key; var keys; @@ -1516,10 +1526,14 @@ define([ //Rewrite any KMZ embedded urls if (defined(uriResolver) && uriResolver.keys.length > 1) { - replaceAttributes(scratchDiv, 'a', 'href', uriResolver); - replaceAttributes(scratchDiv, 'img', 'src', uriResolver); + embedDataUris(scratchDiv, 'a', 'href', uriResolver); + embedDataUris(scratchDiv, 'img', 'src', uriResolver); } + //Make relative urls absolute using the sourceUri + applyBasePath(scratchDiv, 'a', 'href', proxy, sourceUri); + applyBasePath(scratchDiv, 'img', 'src', proxy, sourceUri); + var tmp = '
\ + \ + ]]>\ + '; + + return KmlDataSource.load(parser.parseFromString(kml, "text/xml"), { + camera : options.camera, + canvas : options.canvas, + sourceUri : 'http://test.invalid' + }).then(function(dataSource) { + var entity = dataSource.entities.values[0]; + + var element = document.createElement('div'); + element.innerHTML = entity.description.getValue(); + + var a = element.firstChild.firstChild; + expect(a.localName).toEqual('img'); + expect(a.getAttribute('src')).toEqual('http://test.invalid/foo/bar.png'); + }); + }); + it('BalloonStyle: description retargets existing links to _blank', function() { var kml = '\ \ From 317cf45c0f4a2d6c40ec79b3f0d3077393effb67 Mon Sep 17 00:00:00 2001 From: Matthew Amato Date: Tue, 23 May 2017 15:40:59 -0400 Subject: [PATCH 2/2] Update CHANGES.md --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 7848cdd3fb3e..e1bde72eb300 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,6 +13,7 @@ Change Log * Fixed translucency bug for certain material types [#5335](https://github.com/AnalyticalGraphicsInc/cesium/pull/5335) * Fix picking polylines that use a depth fail appearance. [#5337](https://github.com/AnalyticalGraphicsInc/cesium/pull/5337) * Fixed a crash when morphing from Columbus view to 3D. [#5311](https://github.com/AnalyticalGraphicsInc/cesium/issues/5311) +* Fixed a bug which prevented KML descriptions with relative paths from loading. [#5352](https://github.com/AnalyticalGraphicsInc/cesium/pull/5352) * Updated documentation for Quaternion.fromHeadingPitchRoll [#5264](https://github.com/AnalyticalGraphicsInc/cesium/issues/5264) ### 1.33 - 2017-05-01