Credits keep their linked words: only the words a source linked are the link

A credit read from markup keeps the first link's own words as linkText, and the Cesium credit display and the credit line link just those words, as the source wrote them (Street Level's "© Mapillary contributors" links Mapillary only). The Street Level gate reads a drawn item's pick id from its draw-set id.
This commit is contained in:
Sameh Khamis committed 2026-11-15 20:12:14 -08:00
1 parent 74673f3c85
commit a7fe3eebc6
9 files changed
+123 -8

No files matched your search

+3 -1
View File
@@ -378,7 +378,9 @@ const findPick = (page, prefix, { box = null, step = 3 } = {}) =>
};
for (let py = y; py < y + h; py += stride)
for (let px = x; px < x + w; px += stride) {
const id = scene.pick({ x: px, y: py })?.id;
// A drawn item's pick id names its draw set and its own id.
const picked = scene.pick({ x: px, y: py })?.id;
const id = typeof picked === 'string' ? picked : picked?.id;
if (typeof id === 'string' && id.startsWith(want))
return {
x: rect.left + px,
+3 -2
View File
@@ -292,11 +292,12 @@ test('data credits go through the renderer to the attribution list', async () =>
assert.ok(shown.every((credit) => !credit.showOnScreen));
const odbl = shown.find((credit) => credit.html.includes('opendatacommons'));
assert.match(odbl.html, /href="https:\/\/opendatacommons.org/);
assert.match(odbl.html, />\(ODbL\)</);
// As the source wrote it: only the licence's name is the link.
assert.match(odbl.html, /\(<a [^>]*>ODbL<\/a>\)/);
assert.equal(registerDynamicCredit(viewer, TOMTOM_CREDIT), true);
assert.equal(registerDynamicCredit(viewer, TOMTOM_CREDIT), true);
assert.equal(shown.length, 4, 'a dynamic credit is added once');
assert.match(shown[3].html, /Traffic flow data © TomTom/);
assert.match(shown[3].html, /Traffic flow data © <a [^>]*>TomTom<\/a>/);
assert.equal(registerDynamicCredit({}, { key: 'none', html: 'x' }), false);
});
+6 -1
View File
@@ -23,12 +23,13 @@ import {
LOGO_SCHEMES,
creditFromMarkup,
safeUrl,
splitLink,
} from '../creditMarkup.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']);
const CREDIT_KEYS = new Set(['text', 'href', 'linkText', 'logoUrl', 'inline']);
/** Readings kept per Cesium credit id; past this many, the cache restarts. */
const READINGS_KEPT = 512;
@@ -61,6 +62,10 @@ export function creditMarkup(credit) {
.filter(Boolean)
.join(' ');
const href = credit.href && safeUrl(credit.href, LINK_SCHEMES);
if (href && !logo && credit.linkText) {
const [before, words, after] = splitLink(credit);
return `<span>${escape(before)}<a href="${escape(href)}" target="_blank" rel="noopener">${escape(words)}</a>${escape(after)}</span>`;
}
return href
? `<a href="${escape(href)}" target="_blank" rel="noopener">${body}</a>`
: `<span>${body}</span>`;
+11
View File
@@ -94,6 +94,7 @@ test('Cesium’s own credits are listed as plain data, the logo first', () => {
{
text: 'Data © Example & others',
href: 'https://example.com/a',
linkText: 'Example',
inline: false,
},
]);
@@ -422,3 +423,13 @@ test('a Cesium viewer with no renderer gets a credits port over its display', ()
assert.equal(listeners.size, 1);
assert.equal(findCredits({ creditDisplay: display }), null, 'no scene');
});
test('only the linked words are the link in the credit markup', () => {
const html = creditMarkup({
text: 'Street Level: imagery © Mapillary contributors, CC BY-SA 4.0',
href: 'https://www.mapillary.com/',
linkText: 'Mapillary',
});
assert.ok(html.includes('© <a href="https://www.mapillary.com/"'), html);
assert.ok(html.includes('>Mapillary</a> contributors, CC BY-SA 4.0'), html);
});
+2
View File
@@ -11,6 +11,8 @@ export interface Credit {
text: string;
/** Where the credit links to. */
href?: string;
/** The words of `text` the link covers; the whole text when absent. */
linkText?: string;
/** A logo, drawn in place of the text; the text names it. */
logoUrl?: string;
/** Shown on the map itself, rather than in the expanded list. */
+34 -2
View File
@@ -74,9 +74,36 @@ function attribute(tag, name) {
return match ? decode(match[1] ?? match[2] ?? match[3]) : undefined;
}
/** The first link's own words, as plain text, or ''. */
function linkWords(markup) {
const inside = new RegExp(`<a\\b${TAG_BODY}>([\\s\\S]*?)</a\\s*>`, 'i').exec(
markup,
)?.[1];
if (!inside) return '';
return decode(inside.replace(ANY_TAG, '')).replace(/\s+/g, ' ').trim();
}
/**
* Where `credit`'s link sits in its text: the words before it, the linked
* words and the words after. The whole text is linked without `linkText`.
* @param {{text: string, linkText?: string}} credit
* @returns {[string, string, string]}
*/
export function splitLink(credit) {
const words = credit.linkText;
const at = words ? credit.text.indexOf(words) : -1;
if (at < 0) return ['', credit.text, ''];
return [
credit.text.slice(0, at),
words,
credit.text.slice(at + words.length),
];
}
/**
* @param {string} html
* @returns {{text: string, href?: string, logoUrl?: string}} A logo with no
* @returns {{text: string, href?: string, linkText?: string,
* logoUrl?: string}} A logo with no
* text around it is named by its title or alt text.
*/
export function creditFromMarkup(html) {
@@ -95,7 +122,12 @@ export function creditFromMarkup(html) {
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;
if (href && safeUrl(href, LINK_SCHEMES)) {
credit.href = href;
const words = linkWords(markup);
if (words && words !== text && text.includes(words))
credit.linkText = words;
}
const logoUrl = image && attribute(image, 'src');
if (logoUrl && safeUrl(logoUrl, LOGO_SCHEMES)) credit.logoUrl = logoUrl;
return credit;
+24 -1
View File
@@ -1,6 +1,6 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { creditFromMarkup } from './creditMarkup.js';
import { creditFromMarkup, splitLink } from './creditMarkup.js';
test('text, the first link and the first logo are read from markup', () => {
assert.deepEqual(
@@ -10,6 +10,7 @@ test('text, the first link and the first logo are read from markup', () => {
{
text: 'Map data © OpenStreetMap (ODbL)',
href: 'https://www.openstreetmap.org/copyright',
linkText: '© OpenStreetMap',
},
);
assert.deepEqual(
@@ -53,3 +54,25 @@ test('character references decode; tags, comments and scripts drop', () => {
assert.deepEqual(creditFromMarkup(''), { text: '' });
assert.deepEqual(creditFromMarkup(undefined), { text: '' });
});
test('a link around some of the words keeps those words as the linked ones', () => {
const credit = creditFromMarkup(
'Street Level: imagery © <a href="https://www.mapillary.com" target="_blank" rel="noopener">Mapillary</a> contributors, CC BY-SA 4.0',
);
assert.equal(
credit.text,
'Street Level: imagery © Mapillary contributors, CC BY-SA 4.0',
);
assert.equal(credit.linkText, 'Mapillary');
assert.deepEqual(splitLink(credit), [
'Street Level: imagery © ',
'Mapillary',
' contributors, CC BY-SA 4.0',
]);
});
test('a link around all of the words needs no linked words', () => {
const credit = creditFromMarkup('<a href="https://example.com/">Example</a>');
assert.equal(credit.linkText, undefined);
assert.deepEqual(splitLink(credit), ['', 'Example', '']);
});
+20 -1
View File
@@ -9,7 +9,12 @@
* or logo is kept only for a safe scheme.
*/
import { findCredits } from '../render/rendererOf.js';
import { LINK_SCHEMES, LOGO_SCHEMES, safeUrl } from '../render/creditMarkup.js';
import {
LINK_SCHEMES,
LOGO_SCHEMES,
safeUrl,
splitLink,
} from '../render/creditMarkup.js';
import { onLocaleChange, t } from '../i18n/index.js';
const PANEL_ID = 'gev-credits-panel';
@@ -28,6 +33,20 @@ function distinct(list) {
/** One credit as DOM: its logo or its text, linked when it has a link. */
function creditElement(doc, credit) {
const href = credit.href ? safeUrl(credit.href, LINK_SCHEMES) : null;
const logoUrl = credit.logoUrl ? safeUrl(credit.logoUrl, LOGO_SCHEMES) : null;
// Only the credit's linked words are the link, as the source wrote it.
if (href && !logoUrl && credit.linkText) {
const [before, words, after] = splitLink(credit);
const element = doc.createElement('span');
element.className = 'gev-credit';
const link = doc.createElement('a');
link.href = href;
link.target = '_blank';
link.rel = 'noopener noreferrer';
link.textContent = words;
element.append(before, link, after);
return element;
}
const element = doc.createElement(href ? 'a' : 'span');
element.className = 'gev-credit';
if (href) {
+20
View File
@@ -132,3 +132,23 @@ test('focus in the list stays in it as it redraws, and goes to the line as it em
);
off();
});
test('a credit with linked words links only those words', () => {
const { viewer, renderer } = scene();
renderer.credits.addStatic({
text: 'Street Level: imagery © Mapillary contributors, CC BY-SA 4.0',
href: 'https://www.mapillary.com/',
linkText: 'Mapillary',
inline: true,
});
const off = mountCreditsView(viewer);
const credit = document.querySelector('#gev-credits .gev-credit');
const link = credit.querySelector('a');
assert.equal(link.textContent, 'Mapillary');
assert.equal(link.href, 'https://www.mapillary.com/');
assert.equal(
credit.textContent,
'Street Level: imagery © Mapillary contributors, CC BY-SA 4.0',
);
off();
});