Fix preventScrollToFragment intercepting cross-page links

What does this MR do?

preventScrollToFragment was calling event.preventDefault() on every link click within its container, which broke regular navigation: clicking a link without a hash, or a link pointing to a different page, did nothing.

This MR restricts the default-prevention to same-page hash links only:

  1. Bail out early when the link has no hash.
  2. Bail out when the link's URL (ignoring the hash) does not match the current page URL.

The handler now only intercepts the case it was built for — clicking a fragment link that points to an element on the current page.

Where it manifested

The only consumer is the Rapid Diffs lineLinkAdapter (app/assets/javascripts/rapid_diffs/adapters/line_link.js), where the listener is attached to the diff body. With linked-file params now baked into line link hrefs, those URLs can resolve to a different file/page — clicking them used to be silently no-ops because of the unconditional preventDefault().

How to reproduce

  1. Enable Rapid Diffs on merge requests
  2. Open any merge request
  3. Select 'Changes' tab
  4. Leave a comment with a link on any line
  5. Click on that link
  6. Observe the click on link to work
Edited by Stanislav Lashmanov

Merge request reports

Loading
Loading