Skip to content

Commit

Permalink
Merge pull request #5352 from AnalyticalGraphicsInc/kml-paths
Browse files Browse the repository at this point in the history
Fix KML descriptions that link to relative paths
  • Loading branch information
Hannah authored May 23, 2017
2 parents f712e16 + 317cf45 commit 0abb246
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 19 additions & 5 deletions Source/DataSources/KmlDataSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 = '<div class="cesium-infoBox-description-lighter" style="';
tmp += 'overflow:auto;';
tmp += 'word-wrap:break-word;';
Expand Down Expand Up @@ -1582,7 +1596,7 @@ define([
kmlData.snippet = queryStringValue(featureNode, 'Snippet', namespaces.kml);

processExtendedData(featureNode, entity);
processDescription(featureNode, entity, styleEntity, uriResolver);
processDescription(featureNode, entity, styleEntity, uriResolver, dataSource._proxy, sourceUri);

if (defined(queryFirstNode(featureNode, 'Camera', namespaces.kml))) {
console.log('KML - Unsupported view: Camera');
Expand Down
22 changes: 22 additions & 0 deletions Specs/DataSources/KmlDataSourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,28 @@ defineSuite([
});
});

it('BalloonStyle: relative description paths absolute to sourceUri', function() {
var kml = '<?xml version="1.0" encoding="UTF-8"?>\
<Placemark>\
<description><![CDATA[<img src="foo/bar.png"/>]]></description>\
</Placemark>';

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 = '<?xml version="1.0" encoding="UTF-8"?>\
<Placemark>\
Expand Down

0 comments on commit 0abb246

Please sign in to comment.