Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down
16 changes: 16 additions & 0 deletions test/tests/unit/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});

});
Original file line number Diff line number Diff line change
@@ -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 `:`)

Expand Down
2 changes: 1 addition & 1 deletion www/src/content/reference/04-config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down