Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Fix anchor links being changed due to base href

Oliver Günther6945ff8fa6d4
diff --git a/frontend/src/app/core/setup/globals/global-listeners.ts b/frontend/src/app/core/setup/globals/global-listeners.ts
index 2b33949011b..38d794b9d56 100644
--- a/frontend/src/app/core/setup/globals/global-listeners.ts
+++ b/frontend/src/app/core/setup/globals/global-listeners.ts
@@ -38,6 +38,7 @@ import { setupServerResponse } from 'core-app/core/setup/globals/global-listener
import { listenToSettingChanges } from 'core-app/core/setup/globals/global-listeners/settings';
import { detectOnboardingTour } from 'core-app/core/setup/globals/onboarding/onboarding_tour_trigger';
import { performAnchorHijacking } from './global-listeners/link-hijacking';
+import { fixFragmentAnchors } from 'core-app/core/setup/globals/global-listeners/fix-fragment-anchors';
/**
* A set of listeners that are relevant on every page to set sensible defaults
@@ -139,4 +140,7 @@ export function initializeGlobalListeners():void {
// Bootstrap legacy app code
setupServerResponse();
+
+ // Replace fragment
+ fixFragmentAnchors();
}
diff --git a/frontend/src/app/core/setup/globals/global-listeners/fix-fragment-anchors.ts b/frontend/src/app/core/setup/globals/global-listeners/fix-fragment-anchors.ts
new file mode 100644
index 00000000000..9098604d60d
--- /dev/null
+++ b/frontend/src/app/core/setup/globals/global-listeners/fix-fragment-anchors.ts
@@ -0,0 +1,18 @@
+/**
+ * Due to having a <base /> tag, links that only contains anchors break as they
+ * reference the application base url + the anchor instead of the current page.
+ */
+export function fixFragmentAnchors():void {
+ // Get current document URL without any existing fragments
+ const baseUrl = document.location.href.replace(/#.*$/, '');
+
+ Array
+ .from(document.getElementsByTagName('A'))
+ .forEach((el:HTMLAnchorElement) => {
+ const href = el.getAttribute('href') as string;
+
+ if (href && href !== '#' && href.startsWith('#')) {
+ el.setAttribute('href', baseUrl + href);
+ }
+ });
+}
diff --git a/spec/features/wiki/wiki_page_navigation_spec.rb b/spec/features/wiki/wiki_page_navigation_spec.rb
index 136d62ab711..de961eeba7f 100644
--- a/spec/features/wiki/wiki_page_navigation_spec.rb
+++ b/spec/features/wiki/wiki_page_navigation_spec.rb
@@ -57,5 +57,9 @@ describe 'Wiki page navigation spec', js: true do
# Expect scrolled to menu node
expect_element_in_view page.find('.tree-menu--item.-selected', text: 'Wiki Page No. 55')
+
+ # Expect permalink being correct (Regression #46351)
+ permalink = page.all('.op-uc-link_permalink', visible: :all).first
+ expect(permalink['href']).to include "/projects/#{project.identifier}/wiki/wiki-page-no-55#wiki-page-no-55"
end
end