diff --git a/scripts/qa-street-level.mjs b/scripts/qa-street-level.mjs index 18616b4ce..3dd0a3aa5 100644 --- a/scripts/qa-street-level.mjs +++ b/scripts/qa-street-level.mjs @@ -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, diff --git a/src/data/dataCredits.test.mjs b/src/data/dataCredits.test.mjs index 42d28090e..803641379 100644 --- a/src/data/dataCredits.test.mjs +++ b/src/data/dataCredits.test.mjs @@ -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\)]*>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 © ]*>TomTom<\/a>/); assert.equal(registerDynamicCredit({}, { key: 'none', html: 'x' }), false); }); diff --git a/src/render/cesium/credits.js b/src/render/cesium/credits.js index 1eb5d19ae..890d1500f 100644 --- a/src/render/cesium/credits.js +++ b/src/render/cesium/credits.js @@ -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 `${escape(before)}${escape(words)}${escape(after)}`; + } return href ? `${body}` : `${body}`; diff --git a/src/render/cesium/credits.test.mjs b/src/render/cesium/credits.test.mjs index 84b7ecf1f..0e3108533 100644 --- a/src/render/cesium/credits.test.mjs +++ b/src/render/cesium/credits.test.mjs @@ -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('© Mapillary contributors, CC BY-SA 4.0'), html); +}); diff --git a/src/render/contract/credits.d.ts b/src/render/contract/credits.d.ts index 4f4716577..28bccf4a9 100644 --- a/src/render/contract/credits.d.ts +++ b/src/render/contract/credits.d.ts @@ -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. */ diff --git a/src/render/creditMarkup.js b/src/render/creditMarkup.js index 70a6b9c97..cf8f83a46 100644 --- a/src/render/creditMarkup.js +++ b/src/render/creditMarkup.js @@ -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(`([\\s\\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; diff --git a/src/render/creditMarkup.test.mjs b/src/render/creditMarkup.test.mjs index 0440b0724..7abf2f4a5 100644 --- a/src/render/creditMarkup.test.mjs +++ b/src/render/creditMarkup.test.mjs @@ -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 © Mapillary 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('Example'); + assert.equal(credit.linkText, undefined); + assert.deepEqual(splitLink(credit), ['', 'Example', '']); +}); diff --git a/src/ui/creditsView.js b/src/ui/creditsView.js index c46398db0..03acd1358 100644 --- a/src/ui/creditsView.js +++ b/src/ui/creditsView.js @@ -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) { diff --git a/src/ui/creditsView.test.mjs b/src/ui/creditsView.test.mjs index 649fae4e3..17e443b52 100644 --- a/src/ui/creditsView.test.mjs +++ b/src/ui/creditsView.test.mjs @@ -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(); +});