mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
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 caller'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:
1 parent
979e2213b9
commit
765a0db094
26 files changed
+418
-142
No files matched your search
@@ -12,7 +12,7 @@ import {
|
||||
boxContains,
|
||||
alprRetryDelayMs,
|
||||
validateAlprSnapshot,
|
||||
alprCreditMarkup,
|
||||
alprCredit,
|
||||
} from './model.js';
|
||||
import { createAlprPresentation } from './presentation.js';
|
||||
import { t, tn, msg } from '../../i18n/index.js';
|
||||
@@ -44,6 +44,7 @@ export function createAlprCamerasLayer({ source, services } = {}) {
|
||||
credit: null,
|
||||
creditTimer: null,
|
||||
creditPresented: false,
|
||||
creditOff: null,
|
||||
enabled: false,
|
||||
records: [],
|
||||
recordById: new Map(),
|
||||
@@ -297,10 +298,7 @@ export function createAlprCamerasLayer({ source, services } = {}) {
|
||||
if (state.viewer) throw new Error('ALPR layer is already initialized');
|
||||
state.viewer = viewer;
|
||||
state.dataSource = new Cesium.CustomDataSource('alpr-cameras');
|
||||
const creditMarkup = alprCreditMarkup(source.attribution);
|
||||
state.credit = creditMarkup
|
||||
? new Cesium.Credit(creditMarkup, true)
|
||||
: null;
|
||||
state.credit = alprCredit(source.attribution);
|
||||
viewer.dataSources.add(state.dataSource);
|
||||
state.moveEndRemove =
|
||||
viewer.camera.moveEnd.addEventListener(scheduleLoad);
|
||||
|
||||
@@ -31,24 +31,16 @@ export function destinationPointDeg(latDeg, lonDeg, bearingDeg, distanceM) {
|
||||
};
|
||||
}
|
||||
|
||||
/** Build a linked attribution from plain text and an HTTPS URL only. */
|
||||
export function alprCreditMarkup(attribution) {
|
||||
/**
|
||||
* The on-map credit for the renderer's credits port, from plain text and an
|
||||
* HTTPS URL only; the port escapes it as it shows it.
|
||||
* @returns {{text: string, href: string, inline: true}|null}
|
||||
*/
|
||||
export function alprCredit(attribution) {
|
||||
if (!attribution?.text || !attribution?.href) return null;
|
||||
const href = new URL(attribution.href);
|
||||
if (href.protocol !== 'https:' || href.username || href.password) {
|
||||
throw new TypeError('Camera attribution requires a public HTTPS link');
|
||||
}
|
||||
const escape = (value) =>
|
||||
String(value).replace(
|
||||
/[&<>"']/g,
|
||||
(char) =>
|
||||
({
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
"'": ''',
|
||||
})[char],
|
||||
);
|
||||
return `<span class="gev-alpr-credit">ALPR: <a href="${escape(href.href)}" target="_blank" rel="noopener">${escape(attribution.text)}</a></span>`;
|
||||
return { text: `ALPR: ${attribution.text}`, href: href.href, inline: true };
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import { entityRecord, readEntityRecord } from '../../data/entityRecords.js';
|
||||
import { alprMarkers } from './markerState.js';
|
||||
import { heightsFor } from '../../queries/heights.js';
|
||||
import { cameraOf } from '../../render/cesium/camera.js';
|
||||
import { findCredits } from '../../render/rendererOf.js';
|
||||
|
||||
export function createAlprPresentation({ state, services, source }) {
|
||||
const { governorRequestRender } = services.render;
|
||||
@@ -326,8 +327,8 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
services.credits?.hideOsmCredit?.(state.viewer, LAYER_ID);
|
||||
clearTimeout(state.creditTimer);
|
||||
state.creditTimer = null;
|
||||
if (state.credit)
|
||||
state.viewer?.creditDisplay?.removeStaticCredit(state.credit);
|
||||
state.creditOff?.();
|
||||
state.creditOff = null;
|
||||
governorRequestRender('alpr-credit');
|
||||
}
|
||||
|
||||
@@ -342,7 +343,8 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
}
|
||||
if (!state.enabled || state.creditPresented || !state.credit) return;
|
||||
state.creditPresented = true;
|
||||
state.viewer.creditDisplay?.addStaticCredit(state.credit);
|
||||
state.creditOff =
|
||||
findCredits(state.viewer)?.addStatic(state.credit) ?? null;
|
||||
state.creditTimer = setTimeout(hideOnMapCredit, CREDIT_DISPLAY_MS);
|
||||
governorRequestRender('alpr-credit');
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
createOverpassAlprSource,
|
||||
buildOverpassQuery,
|
||||
} from './source.js';
|
||||
import { validateAlprSnapshot, alprCreditMarkup } from './model.js';
|
||||
import { validateAlprSnapshot, alprCredit } from './model.js';
|
||||
const box = { south: 30, west: -98, north: 30.1, east: -97.9 };
|
||||
test('the source rejects invalid and unbounded queries before fetching', async () => {
|
||||
let calls = 0;
|
||||
@@ -130,19 +130,28 @@ test('source snapshots reject malformed coordinates and duplicate identities', (
|
||||
);
|
||||
});
|
||||
|
||||
test('provider attribution escapes markup and rejects executable links', () => {
|
||||
assert.equal(alprCreditMarkup(null), null);
|
||||
test('provider attribution is plain text with a public HTTPS link only', () => {
|
||||
assert.equal(alprCredit(null), null);
|
||||
assert.throws(
|
||||
() => alprCreditMarkup({ text: 'test', href: 'javascript:alert(1)' }),
|
||||
() => alprCredit({ text: 'test', href: 'javascript:alert(1)' }),
|
||||
/HTTPS/,
|
||||
);
|
||||
const html = alprCreditMarkup({
|
||||
text: '<img onerror=alert(1)>',
|
||||
href: 'https://example.org/?a=1&b=2',
|
||||
});
|
||||
assert.ok(html.includes('<img onerror=alert(1)>'));
|
||||
assert.ok(html.includes('?a=1&b=2'));
|
||||
assert.ok(!html.includes('<img'));
|
||||
assert.throws(
|
||||
() => alprCredit({ text: 'test', href: 'https://user:[email protected]/' }),
|
||||
/HTTPS/,
|
||||
);
|
||||
assert.deepEqual(
|
||||
alprCredit({
|
||||
text: '<img onerror=alert(1)>',
|
||||
href: 'https://example.org/?a=1&b=2',
|
||||
}),
|
||||
{
|
||||
text: 'ALPR: <img onerror=alert(1)>',
|
||||
href: 'https://example.org/?a=1&b=2',
|
||||
inline: true,
|
||||
},
|
||||
'left as text: the credits port escapes it',
|
||||
);
|
||||
});
|
||||
|
||||
test('wide detail view returns zoom guidance before any fetch and can retry a smaller view', async () => {
|
||||
|
||||
@@ -3,6 +3,7 @@ import assert from 'node:assert/strict';
|
||||
import * as credits from '../../data/dataCredits.js';
|
||||
import { createLifecycle } from './lifecycle.js';
|
||||
import { LAYER_ID } from './policy.js';
|
||||
import { bindFakeCesiumCredits } from '../../testSupport/fakeCreditsPort.mjs';
|
||||
|
||||
for (const source of [
|
||||
{ name: 'OpenStreetMap', id: 'tile:base' },
|
||||
@@ -13,13 +14,13 @@ for (const source of [
|
||||
test(`enable restores cached installation credits before a refresh: ${source?.id || 'empty'}`, () => {
|
||||
const visibleCredits = new Set();
|
||||
const noop = () => {};
|
||||
const viewer = {
|
||||
const viewer = bindFakeCesiumCredits({
|
||||
camera: {},
|
||||
creditDisplay: {
|
||||
addStaticCredit: (credit) => visibleCredits.add(credit),
|
||||
removeStaticCredit: (credit) => visibleCredits.delete(credit),
|
||||
},
|
||||
};
|
||||
});
|
||||
const state = {
|
||||
viewer,
|
||||
records: source ? [{ id: 'base', sources: [source] }] : [],
|
||||
|
||||
@@ -1,33 +1,31 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import { creditFromMarkup } from '../../render/creditMarkup.js';
|
||||
import { findCredits } from '../../render/rendererOf.js';
|
||||
|
||||
/**
|
||||
* One static Cesium credit per active provider: CC BY-SA imagery needs
|
||||
* visible attribution that goes away with the imagery.
|
||||
* One on-map credit per active provider, through the renderer's credits
|
||||
* port: CC BY-SA imagery needs visible attribution that goes away with the
|
||||
* imagery. A provider's credit is markup with one link, read as the
|
||||
* port's plain data.
|
||||
*/
|
||||
export function createCredits() {
|
||||
/** @type {Map<string, object>} provider id → Cesium.Credit */
|
||||
/** @type {Map<string, () => void>} provider id → the credit's Off */
|
||||
const shown = new Map();
|
||||
|
||||
function show(viewer, def) {
|
||||
if (shown.has(def.id) || !viewer?.creditDisplay) return;
|
||||
try {
|
||||
const credit = new Cesium.Credit(def.credit.html, true);
|
||||
viewer.creditDisplay.addStaticCredit(credit);
|
||||
shown.set(def.id, credit);
|
||||
} catch {
|
||||
/* credit display unavailable */
|
||||
}
|
||||
if (shown.has(def.id)) return;
|
||||
const credits = findCredits(viewer);
|
||||
if (!credits) return;
|
||||
shown.set(
|
||||
def.id,
|
||||
credits.addStatic({ ...creditFromMarkup(def.credit.html), inline: true }),
|
||||
);
|
||||
}
|
||||
|
||||
function hide(viewer, def) {
|
||||
const credit = shown.get(def.id);
|
||||
if (!credit) return;
|
||||
try {
|
||||
viewer?.creditDisplay?.removeStaticCredit?.(credit);
|
||||
} catch {
|
||||
/* credit display already torn down */
|
||||
}
|
||||
function hide(_viewer, def) {
|
||||
const off = shown.get(def.id);
|
||||
if (!off) return;
|
||||
shown.delete(def.id);
|
||||
off();
|
||||
}
|
||||
|
||||
function hideAll(viewer) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import * as Cesium from 'cesium';
|
||||
import { bindFakeCesiumCamera } from '../../testSupport/fakeCameraPort.mjs';
|
||||
import { createStreetLevelLayer } from './index.js';
|
||||
import { MAPILLARY_CREDIT_HTML } from './providers/mapillary/policy.js';
|
||||
import { creditFromMarkup } from '../../render/creditMarkup.js';
|
||||
import { createMapillaryProvider } from './providers/mapillary/index.js';
|
||||
import { createKartaViewProvider } from './providers/kartaview/index.js';
|
||||
import {
|
||||
@@ -67,13 +68,16 @@ const posingViewer = () =>
|
||||
|
||||
test('the CC BY-SA credit shows while Mapillary is on and goes with it', async (t) => {
|
||||
const { layer, viewer } = await startLayer(t);
|
||||
const shown = () => viewer.credits.map((credit) => credit.html);
|
||||
assert.deepEqual(shown(), [MAPILLARY_CREDIT_HTML], 'shown on enable');
|
||||
const shown = () =>
|
||||
viewer.credits.map((credit) => creditFromMarkup(credit.html).text);
|
||||
const mapillary = creditFromMarkup(MAPILLARY_CREDIT_HTML).text;
|
||||
assert.deepEqual(shown(), [mapillary], 'shown on enable');
|
||||
assert.ok(viewer.credits[0].showOnScreen, 'on the map');
|
||||
assert.match(shown()[0], /CC BY-SA 4\.0/);
|
||||
layer.setParams({ mapillary: false });
|
||||
assert.deepEqual(shown(), [], 'hidden with the switch');
|
||||
layer.setParams({ mapillary: true });
|
||||
assert.deepEqual(shown(), [MAPILLARY_CREDIT_HTML]);
|
||||
assert.deepEqual(shown(), [mapillary]);
|
||||
layer.disable();
|
||||
assert.deepEqual(shown(), [], 'the credit goes with the layer');
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user