From b3e23e1b9e53688a2a8ada78928dbf729a8727ad Mon Sep 17 00:00:00 2001 From: alex-ketch Date: Tue, 11 Feb 2020 00:50:17 -0500 Subject: [PATCH] fix(References): Fix Reference formatting selectors and type castings --- src/shared/js/index.ts | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/shared/js/index.ts b/src/shared/js/index.ts index cb7ff3aaf..092d91a51 100644 --- a/src/shared/js/index.ts +++ b/src/shared/js/index.ts @@ -3,20 +3,21 @@ */ export const formatReferences = (): void => { - const referenceListItemSel = '.references > li' - const titleSel = 'span[itemprop="headline"]' + const referenceListItemSel = + '[data-itemprop="references"] > [itemprop="citation"]' + const titleSel = '[itemprop="headline"]' const datePublishedSel = '[itemprop="datePublished"]' const publicationIssueSel = '[itemtype="https://schema.org/PublicationIssue"]' document.querySelectorAll(referenceListItemSel).forEach(node => { - const datePublished = node.querySelector(datePublishedSel) as HTMLElement - const title = node.querySelector(titleSel) as HTMLElement + const datePublished = node.querySelector(datePublishedSel) + const title = node.querySelector(titleSel) - if (title) { - const titleCopy = title.cloneNode(true) as HTMLElement + if (title !== null) { + const titleCopy = title.cloneNode(true) // Add title node after original datePublished node - if (datePublished && datePublished.parentNode) { + if (datePublished !== null && datePublished.parentNode !== null) { datePublished.parentNode.insertBefore( titleCopy, datePublished.nextSibling @@ -24,19 +25,21 @@ export const formatReferences = (): void => { } // Add datePublished node after PublicationIssue node (if exists) - const publicationIssue = node.querySelector( - publicationIssueSel - ) as HTMLElement - const datePublishedCopy = datePublished.cloneNode(true) as HTMLElement + const publicationIssue = node.querySelector(publicationIssueSel) + const datePublishedCopy = datePublished?.cloneNode(true) - if (publicationIssue && publicationIssue.parentNode) { + if ( + datePublishedCopy !== undefined && + publicationIssue !== null && + publicationIssue.parentNode !== null + ) { publicationIssue.parentNode.insertBefore( datePublishedCopy, publicationIssue.nextSibling ) - } else { + } else if (datePublishedCopy !== undefined) { // Otherwise, add node after titleCopy - if (titleCopy && titleCopy.parentNode) { + if (titleCopy !== null && titleCopy.parentNode !== null) { titleCopy.parentNode.insertBefore( datePublishedCopy, titleCopy.nextSibling