Files
gods-eye-view/src/render/creditMarkup.js
T
Sameh Khamis 979e2213b9 Give the Cesium renderer the contract's credits port
The shared Cesium ports gain credits, on both the conformance renderer and
the application's. list() reports what Cesium owes as plain data: its logo,
its static credits and the last drawn frame's imagery, terrain and tile
credits, read from the credit display through internals.js, with the
credits the app adds listed as given where the display shows them. addStatic
shows a credit through Cesium's display, as markup built from the plain
data with every value escaped and only web links kept, so attribution stays
on screen. change fires when the app adds or removes a credit, and after a
drawn frame whose Cesium credits differ from the last one's.

src/render/creditMarkup.js reads markup credits as plain data. The credits
listener check moves out of renderer.listenerRules into its own check, so a
renderer without every port can run it; it and the credits check now run on
Cesium.
2026-11-06 16:32:17 -08:00

103 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @module render/creditMarkup
* @description A credit written as HTML markup, as engines and older layer
* code carry it, read as the contract's plain credit data
* (`src/render/contract/credits.d.ts`): its text, the first link and the
* first image. Markup with more than one link keeps only the first.
*
* A plain reading, no DOM: it runs in Node and in workers. A link or logo
* of a scheme that could run script is dropped.
*/
/** The schemes a credit may link to, and load its logo from. */
export const LINK_SCHEMES = Object.freeze(['https:', 'http:', 'mailto:']);
export const LOGO_SCHEMES = Object.freeze(['https:', 'http:', 'data:']);
/**
* A URL of one of `schemes`, resolved against the page, or null.
* @param {string} value
* @param {readonly string[]} schemes
* @returns {string|null}
*/
export function safeUrl(value, schemes) {
try {
const base = globalThis.location?.href ?? 'https://invalid.invalid/';
const url = new URL(value, base);
if (!schemes.includes(url.protocol)) return null;
if (url.protocol === 'data:' && !/^data:image\//i.test(url.href))
return null;
return url.href;
} catch {
return null;
}
}
/** A tag's inside, quoted attribute values allowed to hold `>`. */
const TAG_BODY = `(?:[^>"']|"[^"]*"|'[^']*')*`;
const ANCHOR = new RegExp(`<a\\b${TAG_BODY}>`, 'i');
const IMAGE = new RegExp(`<img\\b${TAG_BODY}>`, 'i');
const ANY_TAG = new RegExp(`<${TAG_BODY}>`, 'g');
const ENTITIES = {
amp: '&',
lt: '<',
gt: '>',
quot: '"',
apos: "'",
nbsp: ' ',
copy: '©',
reg: '®',
trade: '™',
middot: '·',
ndash: '–',
mdash: '—',
};
/** Character references decoded; unknown named ones are left as written. */
function decode(text) {
return text.replace(/&(#x[0-9a-f]+|#[0-9]+|[a-z]+);/gi, (whole, ref) => {
if (ref[0] !== '#') return ENTITIES[ref.toLowerCase()] ?? whole;
const code =
ref[1] === 'x' || ref[1] === 'X'
? parseInt(ref.slice(2), 16)
: parseInt(ref.slice(1), 10);
return code > 0 && code <= 0x10ffff ? String.fromCodePoint(code) : whole;
});
}
/** An attribute's value in one start tag, decoded. */
function attribute(tag, name) {
const match = new RegExp(
`\\s${name}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s"'>]+))`,
'i',
).exec(tag);
return match ? decode(match[1] ?? match[2] ?? match[3]) : undefined;
}
/**
* @param {string} html
* @returns {{text: string, href?: string, logoUrl?: string}} A logo with no
* text around it is named by its title or alt text.
*/
export function creditFromMarkup(html) {
const markup = String(html ?? '');
const anchor = ANCHOR.exec(markup)?.[0];
const image = IMAGE.exec(markup)?.[0];
let text = decode(
markup
.replace(/<!--[\s\S]*?-->/g, '')
.replace(/<(script|style)\b[\s\S]*?<\/\1\s*>/gi, '')
.replace(ANY_TAG, ''),
)
.replace(/\s+/g, ' ')
.trim();
if (!text && image)
text = attribute(image, 'title') ?? attribute(image, 'alt') ?? '';
const credit = { text };
const href = anchor && attribute(anchor, 'href');
if (href && safeUrl(href, LINK_SCHEMES)) credit.href = href;
const logoUrl = image && attribute(image, 'src');
if (logoUrl && safeUrl(logoUrl, LOGO_SCHEMES)) credit.logoUrl = logoUrl;
return credit;
}