« get me outta code hell

handle special characters in URLs more carefully - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-07-14 12:27:39 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-07-14 12:27:39 -0300
commit4516c98c09ff775892707c3f2b61c857dd82707d (patch)
tree1ffb3b50cf1c9adc7409b4c01f682536e2730a48 /src/util
parent7830c73e6047ab8ec1322c4a56ed6a450bfb11e6 (diff)
handle special characters in URLs more carefully
Diffstat (limited to 'src/util')
-rw-r--r--src/util/html.js8
-rw-r--r--src/util/urls.js17
2 files changed, 12 insertions, 13 deletions
diff --git a/src/util/html.js b/src/util/html.js
index 1c55fb8c..2db1f2eb 100644
--- a/src/util/html.js
+++ b/src/util/html.js
@@ -132,14 +132,6 @@ export function attributes(attribs) {
         throw new Error(`Attribute value for ${key} should be primitive or array, got ${typeof val}`);
     })
     .filter(([_key, _val, keep]) => keep)
-    .map(([key, val]) => {
-      switch (key) {
-        case 'href':
-          return [key, encodeURI(val)];
-        default:
-          return [key, val];
-      }
-    })
     .map(([key, val]) =>
       typeof val === 'boolean'
         ? `${key}`
diff --git a/src/util/urls.js b/src/util/urls.js
index c2119b8d..ad912908 100644
--- a/src/util/urls.js
+++ b/src/util/urls.js
@@ -79,16 +79,23 @@ export function generateURLs(urlSpec) {
     );
 
     const toHelper =
-      (delimiterMode) =>
+      ({device}) =>
       (key, ...args) => {
         const {
-          value: {[delimiterMode]: template},
+          value: {
+            [device ? 'device' : 'posix']: template,
+          },
         } = getValueForFullKey(relative, key);
 
         let missing = 0;
         let result = template.replaceAll(/<([0-9]+)>/g, (match, n) => {
           if (n < args.length) {
-            return args[n];
+            const value = args[n];
+            if (device) {
+              return value;
+            } else {
+              return encodeURIComponent(value);
+            }
           } else {
             missing++;
           }
@@ -106,8 +113,8 @@ export function generateURLs(urlSpec) {
       };
 
     return {
-      to: toHelper('posix'),
-      toDevice: toHelper('device'),
+      to: toHelper({device: false}),
+      toDevice: toHelper({device: true}),
     };
   };