From 5bee1a49bab5298fa3654fa2d9b4fa68c34b7e1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jyri-Matti=20La=CC=88hteenma=CC=88ki?= Date: Sat, 25 Feb 2023 20:56:47 +0200 Subject: [PATCH 1/2] add extension to populate path variables with request parameters --- src/ext/path-params.js | 11 ++++++ test/ext/path-params.js | 51 +++++++++++++++++++++++++++ test/index.html | 3 ++ www/content/extensions/path-params.md | 24 +++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 src/ext/path-params.js create mode 100644 test/ext/path-params.js create mode 100644 www/content/extensions/path-params.md diff --git a/src/ext/path-params.js b/src/ext/path-params.js new file mode 100644 index 000000000..e251b8927 --- /dev/null +++ b/src/ext/path-params.js @@ -0,0 +1,11 @@ +htmx.defineExtension('path-params', { + onEvent: function(name, evt) { + if (name === "htmx:configRequest") { + evt.detail.path = evt.detail.path.replace(/{([^}]+)}/g, function (_, param) { + var val = evt.detail.parameters[param]; + delete evt.detail.parameters[param]; + return val === undefined ? "{" + param + "}" : encodeURIComponent(val); + }) + } + } +}); \ No newline at end of file diff --git a/test/ext/path-params.js b/test/ext/path-params.js new file mode 100644 index 000000000..3f0ac3a3c --- /dev/null +++ b/test/ext/path-params.js @@ -0,0 +1,51 @@ +describe("path-params extension", function() { + beforeEach(function () { + this.server = makeServer(); + clearWorkArea(); + }); + afterEach(function () { + this.server.restore(); + clearWorkArea(); + }); + + it('uses parameter to populate path variable', function () { + var request; + htmx.on("htmx:beforeRequest", function (evt) { + request = evt; + }); + var div = make("
") + div.click(); + should.equal(request.detail.requestConfig.path, '/items/42'); + }); + + it('parameter is removed when used', function () { + var request; + htmx.on("htmx:beforeRequest", function (evt) { + request = evt; + }); + var div = make("
") + div.click(); + should.equal(request.detail.requestConfig.parameters.other, 43); + should.equal(request.detail.requestConfig.parameters.itemId, undefined); + }); + + it('parameter value is encoded', function () { + var request; + htmx.on("htmx:beforeRequest", function (evt) { + request = evt; + }); + var div = make("
") + div.click(); + should.equal(request.detail.requestConfig.path, '/items/a%2Fb'); + }); + + it('missing variables are ignored', function () { + var request; + htmx.on("htmx:beforeRequest", function (evt) { + request = evt; + }); + var div = make("
") + div.click(); + should.equal(request.detail.requestConfig.path, '/items/42/{subitem}'); + }); +}); diff --git a/test/index.html b/test/index.html index eb6f15b18..88b113150 100644 --- a/test/index.html +++ b/test/index.html @@ -161,6 +161,9 @@

Mocha Test Suite

+ + + diff --git a/www/content/extensions/path-params.md b/www/content/extensions/path-params.md new file mode 100644 index 000000000..6746ea4b6 --- /dev/null +++ b/www/content/extensions/path-params.md @@ -0,0 +1,24 @@ +--- +layout: layout.njk +title: htmx - high power tools for html +--- + +## The `path-params` Extension + +This extension uses request parameters to populate path variables. Used parameters are removed so they won't be sent in the query string or body anymore. + +### Install + +```html +