diff --git a/src/assets/print-view/fetch-orcid.js b/src/assets/print-view/fetch-orcid.js index 179de3daa..74ee4db2c 100644 --- a/src/assets/print-view/fetch-orcid.js +++ b/src/assets/print-view/fetch-orcid.js @@ -711,23 +711,40 @@ function otherIdsTextNode(label, value, url) { } const safeUrl = sanitizeUrl(url) - const valueAsUrl = sanitizeUrl(value) - if (safeUrl && valueAsUrl) { - // If both are provided and value is a URL, render value as the link - wrapper.appendChild(document.createTextNode(' (')) + const safeValue = sanitizeUrl(value) + + if (!value && safeUrl) { + // Requirement 1: value is empty and url is provided + const a = document.createElement('a') + a.href = safeUrl + a.target = '_blank' + a.rel = 'noopener noreferrer' + a.textContent = safeUrl + wrapper.appendChild(a) + } else if (value && safeUrl && value === url) { + // Requirement 2: value matches url const a = document.createElement('a') - a.href = valueAsUrl + a.href = safeUrl a.target = '_blank' a.rel = 'noopener noreferrer' - a.textContent = value || valueAsUrl + a.textContent = value wrapper.appendChild(a) - wrapper.appendChild(document.createTextNode(')')) } else { - // Otherwise, render value as text, - if (value) wrapper.appendChild(document.createTextNode(value)) + // Requirement 3: value is not empty and does not match url + if (value) { + if (safeValue) { + const a = document.createElement('a') + a.href = safeValue + a.target = '_blank' + a.rel = 'noopener noreferrer' + a.textContent = value + wrapper.appendChild(a) + } else { + wrapper.appendChild(document.createTextNode(value)) + } + } - // and optionally append a (url) link if provided and safe - if (safeUrl) { + if (safeUrl && value !== url) { wrapper.appendChild(document.createTextNode(' (')) const a = document.createElement('a') a.href = safeUrl @@ -741,10 +758,6 @@ function otherIdsTextNode(label, value, url) { return wrapper } -function safeTextNode(value) { - return (value || '').replace(/\s+/g, ' ').trim() -} - function renderActivityGroupFromJson(section, title, items, renderItem) { const entries = (items || []).filter(Boolean) if (!entries.length) return false diff --git a/src/assets/print-view/fetch-orcid.spec.ts b/src/assets/print-view/fetch-orcid.spec.ts index b95ed36e1..2fff733f2 100644 --- a/src/assets/print-view/fetch-orcid.spec.ts +++ b/src/assets/print-view/fetch-orcid.spec.ts @@ -296,30 +296,45 @@ describe('fetch-orcid.js', () => { '12345', 'https://scopus.com/12345' ) - expect(node.textContent).toContain('Scopus ID: 12345') + expect(node.textContent).toBe('Scopus ID: 12345 (https://scopus.com/12345)') const anchor = node.querySelector('a') expect(anchor).not.toBeNull() expect(anchor!.href).toBe('https://scopus.com/12345') expect(anchor!.textContent).toBe('https://scopus.com/12345') }) - it('renders value as a link and IGNORES url parameter when value IS a URL', () => { + it('renders value as a link and NO url in brackets when value IS a URL and matches url param', () => { const node = otherIdsTextNode( 'ResearcherID', 'https://researcherid.com/rid/H-1234-2012', - 'https://some-other-url.com' + 'https://researcherid.com/rid/H-1234-2012' ) - // This is the NEW behavior we want. - // Currently it would probably render "ResearcherID: https://researcherid.com/rid/H-1234-2012 (https://some-other-url.com)" + expect(node.textContent).toBe('ResearcherID: https://researcherid.com/rid/H-1234-2012') const anchor = node.querySelector('a') expect(anchor).not.toBeNull() expect(anchor!.href).toBe('https://researcherid.com/rid/H-1234-2012') - expect(anchor!.textContent).toBe( - 'https://researcherid.com/rid/H-1234-2012' + }) + + it('renders value as link and url in brackets when value is a URL but different from url param', () => { + const node = otherIdsTextNode( + 'ResearcherID', + 'https://researcherid.com/rid/H-1234-2012', + 'https://some-other-url.com' ) + expect(node.textContent).toContain('ResearcherID: https://researcherid.com/rid/H-1234-2012') + expect(node.textContent).toContain('(https://some-other-url.com') + const anchors = node.querySelectorAll('a') + expect(anchors.length).toBe(2) + expect(anchors[0].href).toBe('https://researcherid.com/rid/H-1234-2012') + expect(anchors[1].href).toContain('https://some-other-url.com') + }) - // Ensure the other URL is NOT present - expect(node.textContent).not.toContain('https://some-other-url.com') + it('renders only url when value is empty', () => { + const node = otherIdsTextNode('Label', '', 'https://example.com') + expect(node.textContent).toContain('Label: https://example.com') + const anchor = node.querySelector('a') + expect(anchor).not.toBeNull() + expect(anchor!.href).toContain('https://example.com') }) })