From 1c2e524f8445f2ebfc04c3ec9c32dbf05e7d25b7 Mon Sep 17 00:00:00 2001 From: Lluczel <124431759+Lluczel@users.noreply.github.com> Date: Sat, 10 Oct 2026 18:27:21 -0700 Subject: [PATCH] fix: log the dispatched event name when metaCharacter is set __trigger logged the original event name but dispatched the metaCharacter-adjusted one, so logAll/error/warn output named events that no listener receives. Compute the dispatched name once and use it for logging and dispatch. Document that metaCharacter also renames dispatched events. Closes #4108 --- src/htmx.js | 9 +++++---- test/tests/unit/logger.js | 16 ++++++++++++++++ .../04-config/25-htmx-config-metaCharacter.md | 4 ++-- www/src/content/reference/04-config/index.md | 2 +- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/htmx.js b/src/htmx.js index 0ff735822..1a4857794 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -1489,20 +1489,21 @@ var htmx = (() => { } __trigger(on, eventName, detail = {}, bubbles = true) { + let dispatchedName = this.__maybeAdjustMetaCharacter(eventName); // Convention: events with detail.error log at error level, detail.warn at warn level, // otherwise at event level (gated by config.logAll). One emit per event. if (detail.error) { - let prefix = `htmx: ${eventName}: ${detail.error.message ?? detail.error}`; + let prefix = `htmx: ${dispatchedName}: ${detail.error.message ?? detail.error}`; if (detail.error instanceof Error) console.error(prefix, detail.error, { elt: on, detail }); else console.error(prefix, { elt: on, detail }); } else if (detail.warn) { - console.warn(`htmx: ${eventName}: ${detail.warn}`, { elt: on, detail }); + console.warn(`htmx: ${dispatchedName}: ${detail.warn}`, { elt: on, detail }); } else if (this.config.logAll) { - console.log(`htmx: ${eventName}`, { elt: on, detail }); + console.log(`htmx: ${dispatchedName}`, { elt: on, detail }); } on = this.__normalizeElement(on) this.__triggerExtensions(on, eventName, detail); - return this.trigger(on, this.__maybeAdjustMetaCharacter(eventName), detail, bubbles) + return this.trigger(on, dispatchedName, detail, bubbles) } __triggerExtensions(elt, eventName, detail = {}) { diff --git a/test/tests/unit/logger.js b/test/tests/unit/logger.js index 43bcf3e85..5f4e9c440 100644 --- a/test/tests/unit/logger.js +++ b/test/tests/unit/logger.js @@ -81,4 +81,20 @@ describe('logger unit tests', function() { } }); + it('logAll logs the dispatched event name when metaCharacter is set', function() { + htmx.config.logAll = true; + htmx.config.metaCharacter = '-'; + let div = document.createElement('div'); + document.body.appendChild(div); + try { + htmx.__trigger(div, 'htmx:custom', { foo: 1 }); + let logs = captured.filter(c => c[0] === 'log'); + assert.equal(logs.length, 1); + assert.include(logs[0][1], 'htmx-custom'); + } finally { + htmx.config.metaCharacter = null; + div.remove(); + } + }); + }); diff --git a/www/src/content/reference/04-config/25-htmx-config-metaCharacter.md b/www/src/content/reference/04-config/25-htmx-config-metaCharacter.md index f42dbacdc..1bc2130d6 100644 --- a/www/src/content/reference/04-config/25-htmx-config-metaCharacter.md +++ b/www/src/content/reference/04-config/25-htmx-config-metaCharacter.md @@ -1,9 +1,9 @@ --- title: "htmx.config.metaCharacter" -description: "Replaces `:` in attribute modifiers" +description: "Replaces `:` in attribute modifiers and event names" --- -The `htmx.config.metaCharacter` option allows you to replace the `:` character used in attribute modifiers (e.g., `hx-get:inherited`) with a custom character of your choice. +The `htmx.config.metaCharacter` option allows you to replace the `:` character used in attribute modifiers (e.g., `hx-get:inherited`) with a custom character of your choice. It also replaces `:` in the names of dispatched events (e.g. with `-`, `htmx:after:settle` becomes `htmx-after-settle`), so listeners and `hx-on` attributes must use the adjusted names. **Default:** `undefined` (uses `:`) diff --git a/www/src/content/reference/04-config/index.md b/www/src/content/reference/04-config/index.md index 9daa92f46..c0a6612a8 100644 --- a/www/src/content/reference/04-config/index.md +++ b/www/src/content/reference/04-config/index.md @@ -8,7 +8,7 @@ description: "Settings to customize htmx behavior and defaults." - [htmx.config](/reference/config/htmx-config) - configures htmx behavior globally - [htmx.version](/reference/config/htmx-config-version) - reports current htmx version (read-only) - [htmx.config.prefix](/reference/config/htmx-config-prefix) - sets secondary attribute prefix -- [htmx.config.metaCharacter](/reference/config/htmx-config-metaCharacter) - replaces `:` in attribute modifiers +- [htmx.config.metaCharacter](/reference/config/htmx-config-metaCharacter) - replaces `:` in attribute modifiers and event names - [htmx.config.extensions](/reference/config/htmx-config-extensions) - restricts extension registration by name ### Request Settings