Fix search highlight scroll overriding hash-fragment navigation#14141
Merged
Fix search highlight scroll overriding hash-fragment navigation#14141
Conversation
When clicking a search result linking to a specific section (e.g., page.html?q=term#section), scrollToFirstVisibleMatch() overrode the browser's hash scroll, jumping to the first <mark> near the top. Additionally, quarto-nav.js baked ?q= into all link hrefs during DOMContentLoaded, causing TOC clicks to trigger full-page reloads. Two fixes: - Move replaceState (removing ?q=) to module load time, before any DOMContentLoaded handlers run, so quarto-nav.js resolves link hrefs against a clean URL - Guard scrollToFirstVisibleMatch with a hash check so browser hash navigation is preserved for section-level search results
Collaborator
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When clicking a search result that links to a specific section (e.g.,
page.html?q=term#section), the page briefly shows the correct section but then scrolls to the first<mark>near the top of the page. Additionally, TOC and sidebar links on a highlighted page include the?q=parameter in their href, causing full-page reloads instead of in-page navigation.Root Cause
Two interacting issues in
quarto-search.js:replaceState(removing?q=from the URL) ran insideDOMContentLoaded, butquarto-nav.jsalso runs duringDOMContentLoadedand resolves all<a>href attributes againstwindow.location— which still contained?q=. This baked the search param into every link on the page.scrollToFirstVisibleMatch()fired unconditionally viarequestAnimationFrameonpageshow, overriding the browser's native hash-fragment scroll.Fix
replaceStateto module load time (before anyDOMContentLoadedhandlers run), soquarto-nav.jsresolves link hrefs against a clean URLscrollToFirstVisibleMatchwith a hash check — when a#fragmentis present, the browser already scrolled to the target sectionThe original URL (including hash and query params) is captured in
currentUrlbeforereplaceState, so all downstream reads (kQuery,showSearchResults,currentUrl.hash) still work correctly.Test Plan
?q=term#sectionscrolls to hash target, not first match (Playwright)?q=parameter (Playwright)?q=handlerBuilds on #14053 and #14080.