From 4516c98c09ff775892707c3f2b61c857dd82707d Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 14 Jul 2023 12:27:39 -0300 Subject: handle special characters in URLs more carefully --- src/misc-templates.js | 2 +- src/url-spec.js | 4 +++- src/util/html.js | 8 -------- src/util/urls.js | 17 ++++++++++++----- src/write/page-template.js | 6 +++--- 5 files changed, 19 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/misc-templates.js b/src/misc-templates.js index 8f3f0166..a34771c7 100644 --- a/src/misc-templates.js +++ b/src/misc-templates.js @@ -552,7 +552,7 @@ function unbound_iconifyURL(url, { html.tag('svg', [ html.tag('title', msg), html.tag('use', { - href: to('shared.staticFile', `icons.svg#icon-${id}`), + href: to('shared.staticIcon', id), }), ])); } diff --git a/src/url-spec.js b/src/url-spec.js index 0af613cc..d1e347e5 100644 --- a/src/url-spec.js +++ b/src/url-spec.js @@ -60,7 +60,9 @@ const urlSpec = { staticRoot: 'static', utilityFile: 'util/<>', - staticFile: 'static/<>', + staticFile: 'static/<>?<>', + + staticIcon: 'static/icons.svg#icon-<>', }, }, 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}), }; }; diff --git a/src/write/page-template.js b/src/write/page-template.js index 8a3b44e8..fcd8759c 100644 --- a/src/write/page-template.js +++ b/src/write/page-template.js @@ -665,7 +665,7 @@ export function generateDocumentHTML(pageInfo, { html.tag('link', { rel: 'stylesheet', - href: to('shared.staticFile', `site3.css?${cachebust}`), + href: to('shared.staticFile', 'site3.css', cachebust), }), html.tag('style', @@ -676,7 +676,7 @@ export function generateDocumentHTML(pageInfo, { ]), html.tag('script', { - src: to('shared.staticFile', `lazy-loading.js?${cachebust}`), + src: to('shared.staticFile', 'lazy-loading.js', cachebust), }), ]), @@ -694,7 +694,7 @@ export function generateDocumentHTML(pageInfo, { html.tag('script', { type: 'module', - src: to('shared.staticFile', `client.js?${cachebust}`), + src: to('shared.staticFile', 'client.js', cachebust), }), ]), ]); -- cgit 1.3.0-6-gf8a5 From d3315efaf0cfb481fa32a8ea1739431f32aa1a98 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 14 Jul 2023 12:37:17 -0300 Subject: never encode slashes in URLs --- src/util/urls.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/util/urls.js b/src/util/urls.js index ad912908..4d099acd 100644 --- a/src/util/urls.js +++ b/src/util/urls.js @@ -94,7 +94,9 @@ export function generateURLs(urlSpec) { if (device) { return value; } else { - return encodeURIComponent(value); + let encoded = encodeURIComponent(value); + encoded = encoded.replaceAll('%2F', '/'); + return encoded; } } else { missing++; -- cgit 1.3.0-6-gf8a5