diff --git a/src/core.ts b/src/core.ts index 3ce5dd63d..75fac0ecd 100644 --- a/src/core.ts +++ b/src/core.ts @@ -41,7 +41,7 @@ export type Core = CoreDriver & interface Adapter { setup: (settings: Settings) => Footnote[] addListeners: (core: CoreDriver) => () => void - cleanup: () => void + cleanup: (footnotes: Footnote[]) => void } function createActivate(settings: Settings): FootnoteAction { @@ -142,8 +142,7 @@ export function createCore(adapter: Adapter, settings: Settings): Core { ...core, unmount() { removeListeners() - footnotes.forEach(footnote => footnote.destroy()) - adapter.cleanup() + adapter.cleanup(footnotes) } } } diff --git a/src/dom/index.ts b/src/dom/index.ts index 13f919bdc..318e9de44 100644 --- a/src/dom/index.ts +++ b/src/dom/index.ts @@ -23,8 +23,12 @@ function queryAll( function getFirstElementByClass( container: HTMLElement, className: string -): E | null { - return container.querySelector('.' + className) +): E { + return ( + container.querySelector('.' + className) || + (container.firstElementChild as E) || + container + ) } function createElementFromHTML(html: string): HTMLElement { @@ -148,9 +152,8 @@ function createElements(buttonTemplate: string, popoverTemplate: string) { popover.dataset.footnotePopover = '' popover.dataset.footnoteId = id - const wrapper = getFirstElementByClass(popover, CLASS_WRAPPER) || popover - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const content = getFirstElementByClass(popover, CLASS_CONTENT)! + const wrapper = getFirstElementByClass(popover, CLASS_WRAPPER) + const content = getFirstElementByClass(popover, CLASS_CONTENT) bindScrollHandler(content, popover) return { id, button, host, popover, content, wrapper } @@ -186,7 +189,8 @@ export function setup(settings: Settings): Footnote[] { .map(createFootnote) } -export function cleanup(): void { +export function cleanup(footnotes: Footnote[]): void { + footnotes.forEach(footnote => footnote.destroy()) queryAll(document, '.' + CLASS_PRINT_ONLY).forEach(element => element.classList.remove(CLASS_PRINT_ONLY) ) diff --git a/test/fixtures/backlink.html b/test/fixtures/backlink.html new file mode 100644 index 000000000..78a34b45c --- /dev/null +++ b/test/fixtures/backlink.html @@ -0,0 +1,17 @@ +
+

+ This paragraph is footnoted. + [1] +

+ +
diff --git a/test/options/anchorParentSelector.test.ts b/test/options/anchorParentSelector.test.ts index ec36b0770..bd10f8694 100644 --- a/test/options/anchorParentSelector.test.ts +++ b/test/options/anchorParentSelector.test.ts @@ -1,11 +1,16 @@ -import { setDocumentBody, queryAll } from '../helper' +import { fireEvent } from '@testing-library/dom' +import { setDocumentBody, getButton, getPopover, queryAll } from '../helper' import littlefoot from '../../src' -beforeEach(() => { - setDocumentBody('default.html') -}) - test('hides original footnote anchor parent', () => { + setDocumentBody('default.html') littlefoot({ anchorParentSelector: 'sup' }) expect(queryAll('sup.footnote-print-only')).toHaveLength(4) }) + +test.skip('strips backlink parent from the footnote body', () => { + setDocumentBody('backlink.html') + littlefoot() + fireEvent.click(getButton('1')) + expect(getPopover('1').querySelector('sup')).toBeNull() +})