Skip to content

Commit 0abb246

Browse files
author
Hannah
authored
Merge pull request #5352 from AnalyticalGraphicsInc/kml-paths
Fix KML descriptions that link to relative paths
2 parents f712e16 + 317cf45 commit 0abb246

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Change Log
1313
* Fixed translucency bug for certain material types [#5335](https://github.com/AnalyticalGraphicsInc/cesium/pull/5335)
1414
* Fix picking polylines that use a depth fail appearance. [#5337](https://github.com/AnalyticalGraphicsInc/cesium/pull/5337)
1515
* Fixed a crash when morphing from Columbus view to 3D. [#5311](https://github.com/AnalyticalGraphicsInc/cesium/issues/5311)
16+
* Fixed a bug which prevented KML descriptions with relative paths from loading. [#5352](https://github.com/AnalyticalGraphicsInc/cesium/pull/5352)
1617
* Updated documentation for Quaternion.fromHeadingPitchRoll [#5264](https://github.com/AnalyticalGraphicsInc/cesium/issues/5264)
1718

1819
### 1.33 - 2017-05-01

Source/DataSources/KmlDataSource.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ define([
253253
});
254254
}
255255

256-
function replaceAttributes(div, elementType, attributeName, uriResolver) {
256+
function embedDataUris(div, elementType, attributeName, uriResolver) {
257257
var keys = uriResolver.keys;
258258
var baseUri = new Uri('.');
259259
var elements = div.querySelectorAll(elementType);
@@ -272,6 +272,16 @@ define([
272272
}
273273
}
274274

275+
function applyBasePath(div, elementType, attributeName, proxy, sourceUri) {
276+
var elements = div.querySelectorAll(elementType);
277+
for (var i = 0; i < elements.length; i++) {
278+
var element = elements[i];
279+
var value = element.getAttribute(attributeName);
280+
var uri = resolveHref(value, proxy, sourceUri);
281+
element.setAttribute(attributeName, uri);
282+
}
283+
}
284+
275285
function proxyUrl(url, proxy) {
276286
if (defined(proxy)) {
277287
if (new Uri(url).isAbsolute()) {
@@ -1431,7 +1441,7 @@ define([
14311441

14321442
var scratchDiv = document.createElement('div');
14331443

1434-
function processDescription(node, entity, styleEntity, uriResolver) {
1444+
function processDescription(node, entity, styleEntity, uriResolver, proxy, sourceUri) {
14351445
var i;
14361446
var key;
14371447
var keys;
@@ -1516,10 +1526,14 @@ define([
15161526

15171527
//Rewrite any KMZ embedded urls
15181528
if (defined(uriResolver) && uriResolver.keys.length > 1) {
1519-
replaceAttributes(scratchDiv, 'a', 'href', uriResolver);
1520-
replaceAttributes(scratchDiv, 'img', 'src', uriResolver);
1529+
embedDataUris(scratchDiv, 'a', 'href', uriResolver);
1530+
embedDataUris(scratchDiv, 'img', 'src', uriResolver);
15211531
}
15221532

1533+
//Make relative urls absolute using the sourceUri
1534+
applyBasePath(scratchDiv, 'a', 'href', proxy, sourceUri);
1535+
applyBasePath(scratchDiv, 'img', 'src', proxy, sourceUri);
1536+
15231537
var tmp = '<div class="cesium-infoBox-description-lighter" style="';
15241538
tmp += 'overflow:auto;';
15251539
tmp += 'word-wrap:break-word;';
@@ -1582,7 +1596,7 @@ define([
15821596
kmlData.snippet = queryStringValue(featureNode, 'Snippet', namespaces.kml);
15831597

15841598
processExtendedData(featureNode, entity);
1585-
processDescription(featureNode, entity, styleEntity, uriResolver);
1599+
processDescription(featureNode, entity, styleEntity, uriResolver, dataSource._proxy, sourceUri);
15861600

15871601
if (defined(queryFirstNode(featureNode, 'Camera', namespaces.kml))) {
15881602
console.log('KML - Unsupported view: Camera');

Specs/DataSources/KmlDataSourceSpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -1832,6 +1832,28 @@ defineSuite([
18321832
});
18331833
});
18341834

1835+
it('BalloonStyle: relative description paths absolute to sourceUri', function() {
1836+
var kml = '<?xml version="1.0" encoding="UTF-8"?>\
1837+
<Placemark>\
1838+
<description><![CDATA[<img src="foo/bar.png"/>]]></description>\
1839+
</Placemark>';
1840+
1841+
return KmlDataSource.load(parser.parseFromString(kml, "text/xml"), {
1842+
camera : options.camera,
1843+
canvas : options.canvas,
1844+
sourceUri : 'http://test.invalid'
1845+
}).then(function(dataSource) {
1846+
var entity = dataSource.entities.values[0];
1847+
1848+
var element = document.createElement('div');
1849+
element.innerHTML = entity.description.getValue();
1850+
1851+
var a = element.firstChild.firstChild;
1852+
expect(a.localName).toEqual('img');
1853+
expect(a.getAttribute('src')).toEqual('http://test.invalid/foo/bar.png');
1854+
});
1855+
});
1856+
18351857
it('BalloonStyle: description retargets existing links to _blank', function() {
18361858
var kml = '<?xml version="1.0" encoding="UTF-8"?>\
18371859
<Placemark>\

0 commit comments

Comments
 (0)