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

+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', '']);
});