pgi-docgen icon indicating copy to clipboard operation
pgi-docgen copied to clipboard

Handle percent-escaped hash in target URL fragment

Open cl0ne opened this issue 4 years ago • 3 comments

Proposed workaround for #198.

cl0ne avatar Dec 23 '20 00:12 cl0ne

Shouldn't

--- a/index.html
+++ b/index.html
@@ -21,7 +21,7 @@ function applyHash() {
         if(!endsWith(hash, ".html") && !endsWith(hash, "/") && hash.indexOf("#", 1) < 0)
             hash += "/";
 
-        new_src = hash.substring(1);
+        new_src = decodeURIComponent(hash.substring(1));
     } else {
         new_src= main_page;
     }

be enough?

lazka avatar Dec 24 '20 10:12 lazka

I reverted style-related changes.

I've looked at decodeURIComponent earlier, though I'm not sure if it won't break anything by "unescaping too much". So I decided to make the least invasive solution: unescape only the first hash found within fragment part.

Considering the fact that the site consists of static pages only decodeURIComponent should work in most (if not all) cases with a couple of precautions:

--- a/index.html
+++ b/index.html
@@ -12,18 +12,24 @@
 function applyHash() {
     // takes the current hash loads the path into the content frame
 
-    var hash = window.location.hash;
+    var hash = window.location.hash.substring(1);
     var elm = document.getElementById('Content');
-    var new_src;
+    var new_src = main_page;
 
     if(hash) {
+        hash = decodeURIComponent(hash);
+        var fragment_start = hash.indexOf("#");
+        if (fragment_start >= 0) {
+            var new_fragment = hash.substring(fragment_start+1);
+            new_fragment = encodeURIComponent(new_fragment);
+            new_src = encodeURI(hash.substring(0, fragment_start));
+            new_src += "#" + new_fragment
+        } else {
+            // needed for webkit
+            if (!endsWith(hash, ".html") && !endsWith(hash, "/"))
+                hash += "/";
+            new_src = hash;
+        }
-        // needed for webkit
-        if(!endsWith(hash, ".html") && !endsWith(hash, "/") && hash.indexOf("#", 1) < 0)
-            hash += "/";
-
-        new_src = hash.substring(1);
-    } else {
-        new_src= main_page;
     }
 
     // create a dummy element so we can compare the URLs; prevents

cl0ne avatar Dec 24 '20 22:12 cl0ne

So which one of these three solutions should I push for this PR to be accepted?

cl0ne avatar Jan 19 '21 20:01 cl0ne