Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
1cg committed Aug 24, 2023
2 parents 7247e8a + 94623d1 commit a9ae4e3
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/ext/ws.js
Expand Up @@ -341,7 +341,7 @@ This extension adds support for WebSockets to htmx. See /www/extensions/ws.md f

/** @type {WebSocketWrapper} */
var socketWrapper = api.getInternalData(socketElt).webSocket;
var headers = api.getHeaders(sendElt, socketElt);
var headers = api.getHeaders(sendElt, api.getTarget(sendElt));
var results = api.getInputValues(sendElt, 'post');
var errors = results.errors;
var rawParameters = results.values;
Expand Down
12 changes: 8 additions & 4 deletions src/htmx.js
Expand Up @@ -1929,18 +1929,22 @@ return (function () {
function addHxOnEventHandler(elt, eventName, code) {
var nodeData = getInternalData(elt);
nodeData.onHandlers = [];
var func = new Function("event", code + "; return;");
var func;
var listener = function (e) {
return func.call(elt, e);
return maybeEval(elt, function() {
if (!func) {
func = new Function("event", code);
}
func.call(elt, e);
});
};
elt.addEventListener(eventName, listener);
nodeData.onHandlers.push({event:eventName, listener:listener});
return {nodeData:nodeData, code:code, func:func, listener:listener};
}

function processHxOn(elt) {
var hxOnValue = getAttributeValue(elt, 'hx-on');
if (hxOnValue && htmx.config.allowEval) {
if (hxOnValue) {
var handlers = {}
var lines = hxOnValue.split("\n");
var currentEvent = null;
Expand Down
17 changes: 17 additions & 0 deletions test/attributes/hx-on-wildcard.js
Expand Up @@ -130,4 +130,21 @@ describe("hx-on:* attribute", function() {
delete window.tempCount;
});

it("is not evaluated when allowEval is false", function () {
var calledEvent = false;
var handler = htmx.on("htmx:evalDisallowedError", function(){
calledEvent = true;
});
htmx.config.allowEval = false;
try {
var btn = make("<button hx-on:click='window.foo = true'>Foo</button>");
btn.click();
should.not.exist(window.foo);
} finally {
htmx.config.allowEval = true;
htmx.off("htmx:evalDisallowedError", handler);
delete window.foo;
}
calledEvent.should.equal(true);
});
});
17 changes: 17 additions & 0 deletions test/attributes/hx-on.js
Expand Up @@ -119,4 +119,21 @@ describe("hx-on attribute", function() {
delete window.tempCount;
});

it("is not evaluated when allowEval is false", function () {
var calledEvent = false;
var handler = htmx.on("htmx:evalDisallowedError", function(){
calledEvent = true;
});
htmx.config.allowEval = false;
try {
var btn = make("<button hx-on='click: window.foo = true'>Foo</button>");
btn.click();
should.not.exist(window.foo);
} finally {
htmx.config.allowEval = true;
htmx.off("htmx:evalDisallowedError", handler);
delete window.foo;
}
calledEvent.should.equal(true);
});
});
45 changes: 45 additions & 0 deletions test/attributes/hx-vals.js
Expand Up @@ -252,4 +252,49 @@ describe("hx-vals attribute", function() {
div.innerHTML.should.equal("Clicked!");
});

it('javascript: is not evaluated when allowEval is false', function () {
var calledEvent = false;
var handler = htmx.on("htmx:evalDisallowedError", function(){
calledEvent = true;
});
try {
htmx.config.allowEval = false;
this.server.respondWith("POST", "/vars", function (xhr) {
var params = getParameters(xhr);
should.not.exist(params['i1']);
xhr.respond(200, {}, "Clicked!")
});
var div = make('<div hx-post="/vars" hx-vals="javascript:i1:\'test\'"></div>')
div.click();
this.server.respond();
div.innerHTML.should.equal("Clicked!");
} finally {
htmx.config.allowEval = true;
htmx.off("htmx:evalDisallowedError", handler);
}
calledEvent.should.equal(true);
});

it('js: is not evaluated when allowEval is false', function () {
var calledEvent = false;
var handler = htmx.on("htmx:evalDisallowedError", function(){
calledEvent = true;
});
try {
htmx.config.allowEval = false;
this.server.respondWith("POST", "/vars", function (xhr) {
var params = getParameters(xhr);
should.not.exist(params['i1']);
xhr.respond(200, {}, "Clicked!")
});
var div = make('<div hx-post="/vars" hx-vals="js:i1:\'test\'"></div>')
div.click();
this.server.respond();
div.innerHTML.should.equal("Clicked!");
} finally {
htmx.config.allowEval = true;
htmx.off("htmx:evalDisallowedError", handler);
}
calledEvent.should.equal(true);
});
});
22 changes: 22 additions & 0 deletions test/attributes/hx-vars.js
Expand Up @@ -152,4 +152,26 @@ describe("hx-vars attribute", function() {
div.innerHTML.should.equal("Clicked!");
});

it('is not evaluated when allowEval is false', function () {
var calledEvent = false;
var handler = htmx.on("htmx:evalDisallowedError", function(){
calledEvent = true;
});
try {
htmx.config.allowEval = false;
this.server.respondWith("POST", "/vars", function (xhr) {
var params = getParameters(xhr);
should.not.exist(params['i1']);
xhr.respond(200, {}, "Clicked!")
});
var div = make('<div hx-post="/vars" hx-vals="javascript:i1:\'test\'"></div>')
div.click();
this.server.respond();
div.innerHTML.should.equal("Clicked!");
} finally {
htmx.config.allowEval = true;
htmx.off("htmx:evalDisallowedError", handler);
}
calledEvent.should.equal(true);
});
});
21 changes: 21 additions & 0 deletions test/ext/ws.js
Expand Up @@ -88,6 +88,27 @@ describe("web-sockets extension", function () {
this.messages.length.should.equal(1);
})

it('sends expected headers to the server', function () {
var div = make('<div hx-ext="ws" ws-connect="ws://localhost:8080"><button hx-trigger="click" hx-target="#target" ws-send id="d1" name="d1-name">div1</button><output id="target"></output></div>');
this.tickMock();

byId("d1").click();

this.tickMock();

this.messages.length.should.equal(1);
var message = JSON.parse(this.messages[0]);
var headers = message.HEADERS;

console.log(headers);

headers['HX-Request'].should.be.equal('true');
headers['HX-Current-URL'].should.be.equal(document.location.href)
headers['HX-Trigger'].should.be.equal('d1');
headers['HX-Trigger-Name'].should.be.equal('d1-name');
headers['HX-Target'].should.be.equal('target');
})

it('handles message from the server', function () {
var div = make('<div hx-ext="ws" ws-connect="ws://localhost:8080"><div id="d1">div1</div><div id="d2">div2</div></div>');
this.tickMock();
Expand Down

0 comments on commit a9ae4e3

Please sign in to comment.