MacOS WebBrowserComponent::goToURL() drops #anchor fragment of file:// URLs

Hi all,

I ran into an issue where WebBrowserComponent::goToURL() with a file://.../page.html#someAnchor URL loads the page fine on Windows (WebView2) and Linux (WebKitGTK), but on macOS it always lands at the top of the document. The #someAnchor fragment is silently lost.

Example:

webBrowser.goToURL("file:///Users/me/help.html#SectionTwo");

If you log the URL inside pageFinishedLoading(), on macOS it comes back as file:///Users/me/help.html , no fragment at all.

Digging into juce_WebBrowserComponent_mac.mm, the file: branch of goToURL() does:

auto file = URL (url).getLocalFile();
NSURL* nsUrl = [NSURL fileURLWithPath: juceStringToNS (file.getFullPathName())];
...
[webView.get() loadFileURL: appendParametersToFileURL (url, nsUrl) allowingReadAccessToURL: accessPath];

juce::URL’s constructor splits off anything after # into an internal anchor member (juce_URL.cpp, URL::init()), and getLocalFile() only returns the plain path. The anchor is gone at that point. appendParametersToFileURL() only re-adds ?query parameters, never the anchor, so it’s lost for good.

I believe this is actually the same code path that was fixed back in 2021 for query parameters being dropped (commit 42ea1189, “Fix for WebBrowserComponent dropping parameters when using a file:// URI”, JUCE 6.1.0) — that fix handled ?query strings but never got extended to #anchor fragments. I checked and this is still present in the current 9.0.5 release.

Would be great to get this fixed the same way the query-parameter case was, i.e. re-attach the original URL’s anchor onto the NSURL passed to loadFileURL:. Happy to provide more repro details if useful.

Thanks!

1 Like