Skip to content

Commit

Permalink
restore tests and dynamic hx-on behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
1cg committed May 16, 2024
1 parent 20b42aa commit 8928efc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 53 deletions.
15 changes: 13 additions & 2 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -2257,12 +2257,20 @@ var htmx = (function() {
getRawAttribute(elt, 'href').indexOf('#') !== 0
}

/**
* @param {Element} elt
*/
function eltIsDisabled(elt) {
return closest(elt, htmx.config.disableSelector);

Check failure on line 2264 in src/htmx.js

View workflow job for this annotation

GitHub Actions / test_suite

Extra semicolon
}

/**
* @param {Element} elt
* @param {HtmxNodeInternalData} nodeData
* @param {HtmxTriggerSpecification[]} triggerSpecs
*/
function boostElement(elt, nodeData, triggerSpecs) {

Check failure on line 2272 in src/htmx.js

View workflow job for this annotation

GitHub Actions / test_suite

Block must not be padded by blank lines

if ((elt instanceof HTMLAnchorElement && isLocalLink(elt) && (elt.target === '' || elt.target === '_self')) || elt.tagName === 'FORM') {
nodeData.boosted = true
let verb, path
Expand All @@ -2279,7 +2287,7 @@ var htmx = (function() {
triggerSpecs.forEach(function(triggerSpec) {
addEventListener(elt, function(node, evt) {
const elt = asElement(node)
if (closest(elt, htmx.config.disableSelector)) {
if (eltIsDisabled(elt)) {
cleanUpElement(elt)
return
}
Expand Down Expand Up @@ -2727,10 +2735,13 @@ var htmx = (function() {
/** @type EventListener */
const listener = function(e) {
maybeEval(elt, function() {
if (eltIsDisabled(elt)) {
return;

Check failure on line 2739 in src/htmx.js

View workflow job for this annotation

GitHub Actions / test_suite

Extra semicolon
}
if (!func) {
func = new Function('event', code)
}
func.call(elt, e)
func.call(elt, e);

Check failure on line 2744 in src/htmx.js

View workflow job for this annotation

GitHub Actions / test_suite

Extra semicolon
})
}
elt.addEventListener(eventName, listener)
Expand Down
86 changes: 35 additions & 51 deletions test/core/security.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,65 +120,49 @@ describe('security options', function() {
btn.click()
})

it("can disable hx-on on a single elt", function(){
var btn = make("<button hx-disable hx-on:click='window.foo = true'>Foo</button>");
btn.click();
should.equal(window.foo, undefined);
delete window.foo;
})


it("can disable hx-on on a parent elt", function(){
var div = make("<div hx-disable><button id='b1' hx-on:click='window.foo = true'>Foo</button></div>");
var btn = byId("b1")
btn.click();
should.equal(window.foo, undefined);
delete window.foo;
})


it("can disable hx-on on a single elt dynamically", function(){
var btn = make("<button hx-on:click='window.foo = true'>Foo</button>");
btn.click();
should.equal(window.foo, true);
delete window.foo;
it('can disable hx-on on a single elt', function() {
var btn = make("<button hx-disable hx-on:click='window.foo = true'>Foo</button>")
btn.click()
should.equal(window.foo, undefined)
delete window.foo
})

btn.setAttribute("hx-disable", "");
it('can disable hx-on on a parent elt', function() {
var div = make("<div hx-disable><button id='b1' hx-on:click='window.foo = true'>Foo</button></div>")
var btn = byId('b1')
btn.click()
should.equal(window.foo, undefined)
delete window.foo
})

btn.click();
should.equal(window.foo, undefined);
delete window.foo;
})
it('can disable hx-on on a single elt dynamically', function() {
var btn = make("<button hx-on:click='window.foo = true'>Foo</button>")
btn.click()
should.equal(window.foo, true)
delete window.foo

btn.setAttribute('hx-disable', '')

it("can disable hx-on on a parent elt dynamically", function(){
var div = make("<div><button id='b1' hx-on:click='window.foo = true'>Foo</button></div>");
var btn = byId("b1")
btn.click();
should.equal(window.foo, true);
delete window.foo;
btn.click()
should.equal(window.foo, undefined)
delete window.foo
})

div.setAttribute("hx-disable", "");
it('can disable hx-on on a parent elt dynamically', function() {
var div = make("<div><button id='b1' hx-on:click='window.foo = true'>Foo</button></div>")
var btn = byId('b1')
btn.click()
should.equal(window.foo, true)
delete window.foo

btn.click();
should.equal(window.foo, undefined);
delete window.foo;
})
div.setAttribute('hx-disable', '')

it("can make egress cross site requests when htmx.config.selfRequestsOnly is enabled", function(done){
this.timeout(4000)
// should trigger send error, rather than reject
var listener = htmx.on("htmx:sendError", function (){
htmx.off("htmx:sendError", listener);
done();
});
this.server.restore(); // use real xhrs
// will 404, but should respond
var btn = make('<button hx-get="https://hypermedia.systems/www/test">Initial</button>')
btn.click();
})
btn.click()
should.equal(window.foo, undefined)
delete window.foo
})

it("can't make egress cross site requests when htmx.config.selfRequestsOnly is enabled", function(done) {
it("can't make egress cross site requests when htmx.config.selfRequestsOnly is true", function(done) {
this.timeout(4000)
// should trigger send error, rather than reject
var listener = htmx.on('htmx:invalidPath', function() {
Expand Down

0 comments on commit 8928efc

Please sign in to comment.