From 21941d14d77ae0f6a14b132951aa2733997e48be Mon Sep 17 00:00:00 2001
From: Luis Rodrigues
Date: Sat, 17 Aug 2019 20:11:40 +0100
Subject: [PATCH] fix: improved backlink reference detection
improved backlink reference detection
fix #25
---
src/dom/index.ts | 27 +++++++++++++----------
test/fixtures/backlink.html | 7 ++++++
test/options/anchorParentSelector.test.ts | 10 ++++++++-
3 files changed, 31 insertions(+), 13 deletions(-)
diff --git a/src/dom/index.ts b/src/dom/index.ts
index ea6ee83fb..54c61b002 100644
--- a/src/dom/index.ts
+++ b/src/dom/index.ts
@@ -12,7 +12,7 @@ type TemplateData = Readonly<{
reference: string
}>
-type RefBody = readonly [HTMLElement, HTMLElement]
+type RefBody = readonly [HTMLElement, string, HTMLElement]
type RefData = readonly [HTMLElement, TemplateData]
const CLASS_PRINT_ONLY = 'footnote-print-only'
@@ -69,7 +69,6 @@ function findRefBody(
footnoteSelector: string
) {
const processed: Element[] = []
-
return (link: HTMLAnchorElement): RefBody | undefined => {
const fragment = link.href.split('#')[1]
const selector = '#' + fragment.replace(/[:.+~*[\]]/g, '\\$&')
@@ -80,8 +79,9 @@ function findRefBody(
if (body) {
processed.push(body)
- const reference = link.closest(anchorParentSelector) as HTMLElement
- return [reference || link, body]
+ const parent = link.closest(anchorParentSelector) as HTMLElement | null
+ const reference = parent || link
+ return [reference, reference.id ? reference.id : link.id, body]
}
}
}
@@ -96,14 +96,14 @@ function hideFootnoteContainer(container: HTMLElement): void {
}
}
-function hideOriginalFootnote([reference, body]: RefBody): RefBody {
+function hideOriginalFootnote([reference, refId, body]: RefBody): RefBody {
setPrintOnly(reference)
setPrintOnly(body)
hideFootnoteContainer(body.parentElement as HTMLElement)
- return [reference, body]
+ return [reference, refId, body]
}
-function unmountRecursive(element: HTMLElement) {
+function recursiveUnmount(element: HTMLElement) {
const parent = element.parentElement
unmount(element)
const html =
@@ -113,20 +113,23 @@ function unmountRecursive(element: HTMLElement) {
.replace(' ', ' ')
.trim()
if (parent && !html) {
- unmountRecursive(parent)
+ recursiveUnmount(parent)
}
}
-function prepareTemplateData([reference, body]: RefBody, idx: number): RefData {
+function prepareTemplateData(
+ [reference, referenceId, body]: RefBody,
+ idx: number
+): RefData {
const content = body.cloneNode(true) as HTMLElement
- queryAll(content, '[href$="#' + reference.id + '"]').forEach(
- unmountRecursive
+ queryAll(content, '[href$="#' + referenceId + '"]').forEach(
+ recursiveUnmount
)
const data: TemplateData = {
id: `${idx + 1}`,
number: idx + 1,
- reference: reference.id,
+ reference: referenceId,
content: content.innerHTML.startsWith('<')
? content.innerHTML
: '' + content.innerHTML + '
'
diff --git a/test/fixtures/backlink.html b/test/fixtures/backlink.html
index eeb9d8a65..c9db81148 100644
--- a/test/fixtures/backlink.html
+++ b/test/fixtures/backlink.html
@@ -3,6 +3,7 @@
This paragraph is footnoted.
[1]
[2]
+ [3]
diff --git a/test/options/anchorParentSelector.test.ts b/test/options/anchorParentSelector.test.ts
index 3daa62096..799eccd3a 100644
--- a/test/options/anchorParentSelector.test.ts
+++ b/test/options/anchorParentSelector.test.ts
@@ -1,4 +1,5 @@
-import { setDocumentBody, queryAll } from '../helper'
+import { fireEvent } from '@testing-library/dom'
+import { setDocumentBody, queryAll, getButton, getPopover } from '../helper'
import littlefoot from '../../src'
test('hides original footnote anchor parent', () => {
@@ -6,3 +7,10 @@ test('hides original footnote anchor parent', () => {
littlefoot({ anchorParentSelector: 'sup' })
expect(queryAll('sup.footnote-print-only')).toHaveLength(4)
})
+
+test('uses reference ID from the link', () => {
+ setDocumentBody('backlink.html')
+ littlefoot({ activateDelay: 1 })
+ fireEvent.click(getButton('3'))
+ expect(getPopover('3').querySelector('sup')).toBeNull()
+})