Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix KML descriptions that link to relative paths #5352

Merged
merged 2 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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