Add layer and map credits through the renderer's credits port

The street-level provider credits, the map source credit, the shared
inline OpenStreetMap and OpenMapTiles credits and the ALPR provider credit
go through the renderer's credits port instead of Cesium's credit display.
Their markup has one link each and is read as the port's plain data; the
ALPR credit is built as plain data, which the port escapes. src/maps/credits.js
and src/layers/streetLevel/credits.js no longer reach Cesium and leave the
allow-list.

findCredits finds the port as findCamera finds the camera: a Cesium viewer
of a consumer's own, with no renderer bound, gets a credits port over its
own credit display, so the public entries keep their attribution. A port
built that way is released, not disposed, when a renderer binds later, and
a credit let go after its viewer is destroyed leaves the display alone.

The per-layer data credits and the transit and event credits stay on
Cesium's display: their markup carries several links each, which the
contract's credit cannot hold.
This commit is contained in:
Sameh Khamis committed 2026-11-06 16:32:23 -08:00
1 parent 9c9ba376e4
commit cf915e8c16
26 files changed
+418 -142

No files matched your search

+2
View File
@@ -10,6 +10,8 @@ import { listenerList, toCartesian, uncaught } from './common.js';
import { cameraFlightActive } from './internals.js';
import { createCesiumFollow } from './follow.js';
import { cameraOf, findCamera, provideUnboundCamera } from '../rendererOf.js';
// Loads the factory an unbound viewer gets its credits port from.
import './credits.js';
/**
* `cameraOf` and `findCamera`, for code that already reaches Cesium:
+41 -6
View File
@@ -24,7 +24,8 @@ import {
creditFromMarkup,
safeUrl,
} from '../creditMarkup.js';
import { listenerList } from './common.js';
import { provideUnboundCredits } from '../rendererOf.js';
import { listenerList, uncaught } from './common.js';
import { creditDisplayContents } from './internals.js';
const CREDIT_KEYS = new Set(['text', 'href', 'logoUrl', 'inline']);
@@ -86,6 +87,9 @@ function copyCredit(credit) {
*/
export function createCesiumCredits({ display, scene, report, requestRender }) {
const listeners = listenerList(report);
// A consumer may destroy its viewer before the code holding credits lets
// them go; a destroyed display throws on every call.
const displayLive = () => Boolean(display) && !display.isDestroyed?.();
/** Core's credits, in the order added: each `{credit, markup}`. */
const added = [];
/** Core's credits as Cesium shows them, by markup: `{credit, count}`. */
@@ -120,7 +124,9 @@ export function createCesiumCredits({ display, scene, report, requestRender }) {
* leaves it out.
*/
function shownCredits() {
const { statics, screen, lightbox, logo } = creditDisplayContents(display);
const { statics, screen, lightbox, logo } = creditDisplayContents(
displayLive() ? display : undefined,
);
const out = [];
const seen = new Set();
const take = (credit) => {
@@ -204,7 +210,7 @@ export function createCesiumCredits({ display, scene, report, requestRender }) {
entry = { credit: new Cesium.Credit(markup, inline), count: 0 };
shown.set(key, entry);
coreCredits.add(entry.credit);
display?.addStaticCredit(entry.credit);
if (displayLive()) display.addStaticCredit(entry.credit);
}
entry.count += 1;
return key;
@@ -217,7 +223,7 @@ export function createCesiumCredits({ display, scene, report, requestRender }) {
if (entry.count > 0) return;
shown.delete(key);
coreCredits.delete(entry.credit);
display?.removeStaticCredit(entry.credit);
if (displayLive()) display.removeStaticCredit(entry.credit);
}
let disposed = false;
@@ -251,15 +257,44 @@ export function createCesiumCredits({ display, scene, report, requestRender }) {
return {
port,
/** Detach from the scene and take Core's credits off the display. */
dispose() {
disposed = true;
offFrame();
listeners.clear();
if (!display?.isDestroyed?.())
if (displayLive())
for (const { credit } of shown.values())
display?.removeStaticCredit(credit);
display.removeStaticCredit(credit);
shown.clear();
coreCredits.clear();
},
/**
* Detach from the scene only: the credits added stay shown until their
* own Off, for a port another one supersedes.
*/
release() {
offFrame();
listeners.clear();
},
};
}
// A Cesium viewer of a consumer's own, with no renderer bound to it, gets a
// credits port over its credit display when code first asks `findCredits`
// for one. `camera.js` loads this module, so the entries that load the
// unbound camera load this too.
provideUnboundCredits((viewer) =>
// By shape, not `instanceof`: a consumer's viewer may come from another
// copy of Cesium.
typeof viewer.creditDisplay?.addStaticCredit === 'function' &&
typeof viewer.scene?.postRender?.addEventListener === 'function'
? createCesiumCredits({
display: viewer.creditDisplay,
scene: viewer.scene,
report: uncaught,
requestRender: () => {
if (!viewer.scene.isDestroyed?.()) viewer.scene.requestRender?.();
},
})
: null,
);
+51
View File
@@ -2,6 +2,7 @@ import assert from 'node:assert/strict';
import test from 'node:test';
import * as Cesium from 'cesium';
import { createCesiumCredits, creditMarkup } from './credits.js';
import { findCredits } from '../rendererOf.js';
/** A credit display as Cesium keeps it: static credits and a frame's. */
function fakeDisplay() {
@@ -271,6 +272,31 @@ test('dispose detaches from the frame and takes Core’s credits off', () => {
assert.equal(t.renders(), 1, 'no render asked of a disposed scene');
});
test('a credit let go after its display is destroyed leaves the display alone', () => {
const t = setup();
const off = t.port.addStatic({ text: 'x' });
t.display.isDestroyed = () => true;
t.display.removeStaticCredit = () => {
throw new Error('destroyed');
};
off();
assert.deepEqual(t.port.list(), []);
t.part.dispose();
});
test('a released port stops following frames and keeps its credits until their Off', () => {
const t = setup();
const seen = [];
t.port.on('change', () => seen.push(1));
const off = t.port.addStatic({ text: 'x' });
t.part.release();
assert.equal(t.listeners.size, 0);
assert.equal(t.display._staticCredits.length, 1, 'still shown');
off();
assert.equal(t.display._staticCredits.length, 0);
assert.deepEqual(seen, [1], 'its listeners are gone');
});
test('credit markup escapes every value and links only web URLs', () => {
assert.equal(
creditMarkup({ text: 'a "b" & <c>' }),
@@ -289,3 +315,28 @@ test('credit markup escapes every value and links only web URLs', () => {
'<a href="https://example.com/?a=1&amp;b=%222%22" target="_blank" rel="noopener">x</a>',
);
});
test('a Cesium viewer with no renderer gets a credits port over its display', () => {
const display = fakeDisplay();
const listeners = new Set();
let renders = 0;
const viewer = {
creditDisplay: display,
scene: {
postRender: {
addEventListener(cb) {
listeners.add(cb);
return () => listeners.delete(cb);
},
},
requestRender: () => (renders += 1),
},
};
const port = findCredits(viewer);
assert.equal(findCredits(viewer), port);
port.addStatic({ text: 'Unbound' });
assert.equal(display._staticCredits.length, 1);
assert.equal(renders, 1);
assert.equal(listeners.size, 1);
assert.equal(findCredits({ creditDisplay: display }), null, 'no scene');
});