-
Notifications
You must be signed in to change notification settings - Fork 0
#14: Link click listeners #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -80,7 +80,7 @@ export function isLocalURL(url) { | |||||||||||
| export function bootstrap() { | ||||||||||||
| console.assert( | ||||||||||||
| !!window, | ||||||||||||
| "boostrap error: must be called from within a browser context", | ||||||||||||
| "bootstrap error: must be called from within a browser context", | ||||||||||||
| ); | ||||||||||||
| const link = window.document.querySelector( | ||||||||||||
| 'head link[rel*="alternate"][type="application/vnd.api+json"]', | ||||||||||||
|
|
@@ -93,7 +93,53 @@ export function bootstrap() { | |||||||||||
| ) | ||||||||||||
| : new URL(link.getAttribute("href")); | ||||||||||||
| const client = new Client(initialURL.href); | ||||||||||||
| // Register a global listener for history updates. | ||||||||||||
|
|
||||||||||||
| // Add click event listener to div#app | ||||||||||||
| const appDiv = document.getElementById("app"); | ||||||||||||
| console.assert(!!appDiv, "bootstrap error: missing div#app element"); | ||||||||||||
| appDiv.addEventListener("click", (event) => { | ||||||||||||
| // Check if the target is an anchor element | ||||||||||||
| if (!(event.target instanceof HTMLAnchorElement)) return; | ||||||||||||
|
|
||||||||||||
| const anchor = event.target; | ||||||||||||
|
Comment on lines
+102
to
+104
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is too strict, for example the markup could be something like
Suggested change
|
||||||||||||
| const href = anchor.href; // Safe to access href directly | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the
Suggested change
|
||||||||||||
|
|
||||||||||||
| // Check if href is external or has a non-web protocol | ||||||||||||
| let url; | ||||||||||||
| try { | ||||||||||||
| url = new URL(href, appDiv.baseURI); | ||||||||||||
| } catch { | ||||||||||||
| console.warn(`Invalid href: ${href}`); | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
| const isExternal = url.protocol !== "http:" && url.protocol !== "https:" || | ||||||||||||
| url.origin !== new URL(appDiv.baseURI).origin; | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not critical to change now but this could just be |
||||||||||||
| if (isExternal) return; | ||||||||||||
|
|
||||||||||||
| // Check if anchor has a type attribute (opt-out) | ||||||||||||
| if (anchor.hasAttribute("type")) return; | ||||||||||||
gabesullice marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
|
|
||||||||||||
| // Check if anchor has a download attribute | ||||||||||||
| if (anchor.hasAttribute("download")) return; | ||||||||||||
|
|
||||||||||||
| // Check if anchor has a target attribute that is not _blank | ||||||||||||
| if ( | ||||||||||||
| anchor.hasAttribute("target") && | ||||||||||||
| anchor.getAttribute("target") !== "_blank" | ||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||||||||
| ) return; | ||||||||||||
|
|
||||||||||||
| // Prevent default behavior and stop propagation | ||||||||||||
| event.preventDefault(); | ||||||||||||
| event.stopPropagation(); | ||||||||||||
|
|
||||||||||||
| // Use normalized href | ||||||||||||
| const normalizedHREF = url.href; | ||||||||||||
|
|
||||||||||||
| // Call the client's follow function | ||||||||||||
| client.follow(normalizedHREF); | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here
Suggested change
|
||||||||||||
| }); | ||||||||||||
|
|
||||||||||||
| // Register a global listener for history updates | ||||||||||||
| addEventListener("popstate", (event) => { | ||||||||||||
| // Not navigating without a state. | ||||||||||||
| if (event.state) { | ||||||||||||
|
|
@@ -106,6 +152,7 @@ export function bootstrap() { | |||||||||||
| } | ||||||||||||
| } | ||||||||||||
| }); | ||||||||||||
|
|
||||||||||||
| return client; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
Uh oh!
There was an error while loading. Please reload this page.