diff --git a/src/HashTarget.ts b/src/HashTarget.ts index 46dc695752b0ea3855c035817b3e82aa4d778fd5..b21ff122f8cc4db44769bc3c8226ac5e7051dd81 100644 --- a/src/HashTarget.ts +++ b/src/HashTarget.ts @@ -5,22 +5,15 @@ export class HashTarget { private constructor( private readonly value: string, - private readonly targetElement: HTMLElement, ) {} - public static fromString(value: string, document: HTMLDocument): HashTarget + public static fromString(value: string): HashTarget { if (value === '' || value === '#') { throw new Error('Hash does not contain any fragment.'); } - const targetElementId = value.substring(1); - const targetElement = document.getElementById(targetElementId); - if (targetElement === null) { - throw new Error(`No referenced element with ID ${targetElementId} exists.`); - } - - return new this(value, targetElement); + return new this(value); } public getHash(): string @@ -28,8 +21,14 @@ export class HashTarget return this.value; } - public getElement(): HTMLElement + public getRefElement(document: HTMLDocument): HTMLElement { - return this.targetElement; + const targetElementId = this.value.substring(1); + const targetElement = document.getElementById(targetElementId); + if (targetElement === null) { + throw new Error(`No referenced element with ID ${targetElementId} exists.`); + } + + return targetElement; } } diff --git a/src/handlers/linkClickScroll/initializeOnLinkClickScroll.ts b/src/handlers/linkClickScroll/initializeOnLinkClickScroll.ts index 49d2cfdf5b675b645ccfca6ec31dbc6f40dcf60d..31806d41e21171c13106a35d6e1c6dcdfe75c4bf 100644 --- a/src/handlers/linkClickScroll/initializeOnLinkClickScroll.ts +++ b/src/handlers/linkClickScroll/initializeOnLinkClickScroll.ts @@ -15,6 +15,6 @@ export function initializeOnLinkClickScroll(): void } event.preventDefault(); - scrollToTarget(HashTarget.fromString(element.hash, document)); + scrollToTarget(HashTarget.fromString(element.hash)); }))); } diff --git a/src/handlers/loadScroll/initializeOnLoadScroll.ts b/src/handlers/loadScroll/initializeOnLoadScroll.ts index 335d65d07655b28c8c0aa6f35cc89d8d50033cea..b4a42ce7d1849faca893b75db29c5f84b8e3a933 100644 --- a/src/handlers/loadScroll/initializeOnLoadScroll.ts +++ b/src/handlers/loadScroll/initializeOnLoadScroll.ts @@ -22,7 +22,7 @@ export function initializeOnLoadScroll(): void document.addEventListener('DOMContentLoaded', () => { try { - hashTarget = HashTarget.fromString(hash, document); + hashTarget = HashTarget.fromString(hash); } catch (e) {} // when URL contains hash which has no corresponding DOM element, just ignore it }); diff --git a/src/scrollers/scrollToTarget.ts b/src/scrollers/scrollToTarget.ts index a31ec01a924844f14d9b9036a38710a438309ade..5906c2b2abf33685b7951062a9bb9e09715c3c05 100644 --- a/src/scrollers/scrollToTarget.ts +++ b/src/scrollers/scrollToTarget.ts @@ -5,11 +5,11 @@ import {assert} from '../assert'; export function scrollToTarget(hashTarget: HashTarget|string, onScrollFinishedCallback?: () => void): void { if (typeof hashTarget === 'string') { - hashTarget = HashTarget.fromString(hashTarget, document); + hashTarget = HashTarget.fromString(hashTarget); } scrollToElement( - hashTarget.getElement(), + hashTarget.getRefElement(document), () => { assert(hashTarget instanceof HashTarget); window.location.hash = hashTarget.getHash();