Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent nodeData.onHandlers overwrite #1862

Closed
wants to merge 5 commits into from
Closed
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
4 changes: 3 additions & 1 deletion src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -1950,7 +1950,9 @@ return (function () {

function addHxOnEventHandler(elt, eventName, code) {
var nodeData = getInternalData(elt);
nodeData.onHandlers = [];
if (!Array.isArray(nodeData.onHandlers)) {
nodeData.onHandlers = [];
}
var func;
var listener = function (e) {
return maybeEval(elt, function() {
Expand Down
18 changes: 18 additions & 0 deletions test/attributes/hx-on-wildcard.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,22 @@ describe("hx-on:* attribute", function() {
delete window.foo;
delete window.bar;
});

it("cleans up all handlers when the DOM updates", function () {
// setup
window.foo = 0;
window.bar = 0;
var div = make("<div hx-on:increment-foo='window.foo++' hx-on:increment-bar='window.bar++'>Foo</div>");
make("<div>Another Div</div>"); // sole purpose is to update the DOM

// check there is just one handler against each event
htmx.trigger(div, "increment-foo");
htmx.trigger(div, "increment-bar");
window.foo.should.equal(1);
window.bar.should.equal(1);

// teardown
delete window.foo;
delete window.bar;
});
});
18 changes: 18 additions & 0 deletions test/attributes/hx-on.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,22 @@ describe("hx-on attribute", function() {
delete window.foo;
delete window.bar;
});

it("cleans up all handlers when the DOM updates", function () {
// setup
window.foo = 0;
window.bar = 0;
var div = make("<div hx-on='increment-foo: window.foo++\nincrement-bar: window.bar++'>Foo</div>");
make("<div>Another Div</div>"); // sole purpose is to update the DOM

// check there is just one handler against each event
htmx.trigger(div, "increment-foo");
htmx.trigger(div, "increment-bar");
window.foo.should.equal(1);
window.bar.should.equal(1);

// teardown
delete window.foo;
delete window.bar;
});
});