Skip to content

Commit f1d185e

Browse files
committed
PD-5749 Fix the display or ext ids urls
1 parent 0eba9e3 commit f1d185e

2 files changed

Lines changed: 52 additions & 24 deletions

File tree

‎src/assets/print-view/fetch-orcid.js‎

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -711,23 +711,40 @@ function otherIdsTextNode(label, value, url) {
711711
}
712712

713713
const safeUrl = sanitizeUrl(url)
714-
const valueAsUrl = sanitizeUrl(value)
715-
if (safeUrl && valueAsUrl) {
716-
// If both are provided and value is a URL, render value as the link
717-
wrapper.appendChild(document.createTextNode(' ('))
714+
const safeValue = sanitizeUrl(value)
715+
716+
if (!value && safeUrl) {
717+
// Requirement 1: value is empty and url is provided
718+
const a = document.createElement('a')
719+
a.href = safeUrl
720+
a.target = '_blank'
721+
a.rel = 'noopener noreferrer'
722+
a.textContent = safeUrl
723+
wrapper.appendChild(a)
724+
} else if (value && safeUrl && value === url) {
725+
// Requirement 2: value matches url
718726
const a = document.createElement('a')
719-
a.href = valueAsUrl
727+
a.href = safeUrl
720728
a.target = '_blank'
721729
a.rel = 'noopener noreferrer'
722-
a.textContent = value || valueAsUrl
730+
a.textContent = value
723731
wrapper.appendChild(a)
724-
wrapper.appendChild(document.createTextNode(')'))
725732
} else {
726-
// Otherwise, render value as text,
727-
if (value) wrapper.appendChild(document.createTextNode(value))
733+
// Requirement 3: value is not empty and does not match url
734+
if (value) {
735+
if (safeValue) {
736+
const a = document.createElement('a')
737+
a.href = safeValue
738+
a.target = '_blank'
739+
a.rel = 'noopener noreferrer'
740+
a.textContent = value
741+
wrapper.appendChild(a)
742+
} else {
743+
wrapper.appendChild(document.createTextNode(value))
744+
}
745+
}
728746

729-
// and optionally append a (url) link if provided and safe
730-
if (safeUrl) {
747+
if (safeUrl && value !== url) {
731748
wrapper.appendChild(document.createTextNode(' ('))
732749
const a = document.createElement('a')
733750
a.href = safeUrl
@@ -741,10 +758,6 @@ function otherIdsTextNode(label, value, url) {
741758
return wrapper
742759
}
743760

744-
function safeTextNode(value) {
745-
return (value || '').replace(/\s+/g, ' ').trim()
746-
}
747-
748761
function renderActivityGroupFromJson(section, title, items, renderItem) {
749762
const entries = (items || []).filter(Boolean)
750763
if (!entries.length) return false

‎src/assets/print-view/fetch-orcid.spec.ts‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,30 +296,45 @@ describe('fetch-orcid.js', () => {
296296
'12345',
297297
'https://scopus.com/12345'
298298
)
299-
expect(node.textContent).toContain('Scopus ID: 12345')
299+
expect(node.textContent).toBe('Scopus ID: 12345 (https://scopus.com/12345)')
300300
const anchor = node.querySelector('a')
301301
expect(anchor).not.toBeNull()
302302
expect(anchor!.href).toBe('https://scopus.com/12345')
303303
expect(anchor!.textContent).toBe('https://scopus.com/12345')
304304
})
305305

306-
it('renders value as a link and IGNORES url parameter when value IS a URL', () => {
306+
it('renders value as a link and NO url in brackets when value IS a URL and matches url param', () => {
307307
const node = otherIdsTextNode(
308308
'ResearcherID',
309309
'https://researcherid.com/rid/H-1234-2012',
310-
'https://some-other-url.com'
310+
'https://researcherid.com/rid/H-1234-2012'
311311
)
312-
// This is the NEW behavior we want.
313-
// Currently it would probably render "ResearcherID: https://researcherid.com/rid/H-1234-2012 (https://some-other-url.com)"
312+
expect(node.textContent).toBe('ResearcherID: https://researcherid.com/rid/H-1234-2012')
314313
const anchor = node.querySelector('a')
315314
expect(anchor).not.toBeNull()
316315
expect(anchor!.href).toBe('https://researcherid.com/rid/H-1234-2012')
317-
expect(anchor!.textContent).toBe(
318-
'https://researcherid.com/rid/H-1234-2012'
316+
})
317+
318+
it('renders value as link and url in brackets when value is a URL but different from url param', () => {
319+
const node = otherIdsTextNode(
320+
'ResearcherID',
321+
'https://researcherid.com/rid/H-1234-2012',
322+
'https://some-other-url.com'
319323
)
324+
expect(node.textContent).toContain('ResearcherID: https://researcherid.com/rid/H-1234-2012')
325+
expect(node.textContent).toContain('(https://some-other-url.com')
326+
const anchors = node.querySelectorAll('a')
327+
expect(anchors.length).toBe(2)
328+
expect(anchors[0].href).toBe('https://researcherid.com/rid/H-1234-2012')
329+
expect(anchors[1].href).toContain('https://some-other-url.com')
330+
})
320331

321-
// Ensure the other URL is NOT present
322-
expect(node.textContent).not.toContain('https://some-other-url.com')
332+
it('renders only url when value is empty', () => {
333+
const node = otherIdsTextNode('Label', '', 'https://example.com')
334+
expect(node.textContent).toContain('Label: https://example.com')
335+
const anchor = node.querySelector('a')
336+
expect(anchor).not.toBeNull()
337+
expect(anchor!.href).toContain('https://example.com')
323338
})
324339
})
325340

0 commit comments

Comments
 (0)