From dfeb2de6233199f3b0273244dd54612612020d23 Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Wed, 28 Sep 2022 17:48:51 +0200 Subject: [PATCH] fix(pat navigation): Don't check query string and hash fragments. When setting navigation markers based on URL comparison do not set the query string and hash fragments. When using hash navigation within one page, use pat-scroll to mark items as current. --- src/pat/navigation/navigation.js | 7 ++++++- src/pat/navigation/navigation.test.js | 26 +++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/pat/navigation/navigation.js b/src/pat/navigation/navigation.js index bc2af7410..5ecb3162d 100644 --- a/src/pat/navigation/navigation.js +++ b/src/pat/navigation/navigation.js @@ -207,7 +207,12 @@ export default Base.extend({ * @returns {String} - The prepared url. */ prepare_url(url) { - return url?.replace("/view", "").replaceAll("@@", "").replace(/\/$/, ""); + return url + ?.replace("/view", "") // Remove Plone-specific default view. + .replaceAll("@@", "") // Remove Plone-specific @@ identifier of views. + .split("?")[0] // Remove query string. + .split("#")[0] // Remove hash. + .replace(/\/$/, ""); // Remove trailing slash. }, /** diff --git a/src/pat/navigation/navigation.test.js b/src/pat/navigation/navigation.test.js index c68cdb9fc..23ee60bfe 100644 --- a/src/pat/navigation/navigation.test.js +++ b/src/pat/navigation/navigation.test.js @@ -289,7 +289,7 @@ describe("3 - Navigation pattern tests - Mark items based on URL", () => { `; - set_url("https://patternslib.com/"); + set_url("https://patternslib.com/?a=1&b=2#hash"); const instance = new Pattern(document.querySelector(".pat-navigation")); @@ -315,6 +315,30 @@ describe("3 - Navigation pattern tests - Mark items based on URL", () => { expect(document.querySelectorAll(".inPath").length).toBe(0); expect(document.querySelector(".current a")).toBe(it1); + // Check query string + instance.clear_items(); + instance.mark_items_url("https://patternslib.com/path1?a=1&b=2"); + + expect(document.querySelectorAll(".current").length).toBe(2); + expect(document.querySelectorAll(".inPath").length).toBe(0); + expect(document.querySelector(".current a")).toBe(it1); + + // Check hash + instance.clear_items(); + instance.mark_items_url("https://patternslib.com/path1/#hash"); + + expect(document.querySelectorAll(".current").length).toBe(2); + expect(document.querySelectorAll(".inPath").length).toBe(0); + expect(document.querySelector(".current a")).toBe(it1); + + // Check query string and hash + instance.clear_items(); + instance.mark_items_url("https://patternslib.com/path1?a=1&b=2#hash"); + + expect(document.querySelectorAll(".current").length).toBe(2); + expect(document.querySelectorAll(".inPath").length).toBe(0); + expect(document.querySelector(".current a")).toBe(it1); + instance.clear_items(); instance.mark_items_url("https://patternslib.com/path2");