initial inheritance disabling work

This commit is contained in:
Carson Gross
2023-11-14 16:27:49 -07:00
parent 8c5e053377
commit 69c053c136
3 changed files with 88 additions and 5 deletions

View File

@@ -75,7 +75,8 @@ return (function () {
globalViewTransitions: false,
methodsThatUseUrlParams: ["get"],
selfRequestsOnly: false,
scrollIntoViewOnBoost: true
scrollIntoViewOnBoost: true,
disableInheritance: false
},
parseInterval:parseInterval,
_:internalEval,
@@ -202,11 +203,20 @@ return (function () {
function getAttributeValueWithDisinheritance(initialElement, ancestor, attributeName){
var attributeValue = getAttributeValue(ancestor, attributeName);
var disinherit = getAttributeValue(ancestor, "hx-disinherit");
if (initialElement !== ancestor && disinherit && (disinherit === "*" || disinherit.split(" ").indexOf(attributeName) >= 0)) {
return "unset";
} else {
return attributeValue
var inherit = getAttributeValue(ancestor, "hx-inherit");
if (initialElement !== ancestor) {
if (htmx.config.disableInheritance) {
if (inherit && (inherit === "*" || inherit.split(" ").indexOf(attributeName) >= 0)) {
return attributeValue;
} else {
return null;
}
}
if (disinherit && (disinherit === "*" || disinherit.split(" ").indexOf(attributeName) >= 0)) {
return "unset";
}
}
return attributeValue;
}
/**

View File

@@ -0,0 +1,72 @@
describe("hx-inherit attribute", function() {
beforeEach(function () {
this.server = makeServer();
clearWorkArea();
htmx.config.disableInheritance = true;
});
afterEach(function () {
this.server.restore();
clearWorkArea();
htmx.config.disableInheritance = false;
});
it('can disable inheritance', function () {
this.server.respondWith("GET", "/test", "Test");
var div = make('<div hx-target="#cta">' +
'<button id="btn1" hx-get="/test"></button>' +
'<span id="cta">Click Me!</span>' +
'</div>')
var btn = byId("btn1");
btn.click();
this.server.respond();
btn.innerText.should.equal("Test");
})
it('can enable inheritance for all', function () {
this.server.respondWith("GET", "/test", "Test");
var div = make('<div hx-target="#cta" hx-inherit="*">' +
'<button id="btn1" hx-get="/test"></button>' +
'<span id="cta">Click Me!</span>' +
'</div>')
var btn = byId("btn1");
var span = byId("cta");
btn.click();
this.server.respond();
btn.innerText.should.equal("");
span.innerText.should.equal("Test");
})
it('can enable inheritance by name', function () {
this.server.respondWith("GET", "/test", "Test");
var div = make('<div hx-target="#cta" hx-inherit="hx-target">' +
'<button id="btn1" hx-get="/test"></button>' +
'<span id="cta">Click Me!</span>' +
'</div>')
var btn = byId("btn1");
var span = byId("cta");
btn.click();
this.server.respond();
btn.innerText.should.equal("");
span.innerText.should.equal("Test");
})
it('can enable inheritance by name 2', function () {
this.server.respondWith("GET", "/test", "Test");
var div = make('<div hx-target="#cta" hx-inherit="hx-swap">' +
'<button id="btn1" hx-get="/test"></button>' +
'<span id="cta">Click Me!</span>' +
'</div>')
var btn = byId("btn1");
var span = byId("cta");
btn.click();
this.server.respond();
console.log(div.innerHTML)
btn.innerText.should.equal("Test");
span.innerText.should.equal("Click Me!");
})
});

View File

@@ -79,6 +79,7 @@
<script src="attributes/hx-include.js"></script>
<script src="attributes/hx-indicator.js"></script>
<script src="attributes/hx-disabled-elt.js"></script>
<script src="attributes/hx-inherit.js"></script>
<script src="attributes/hx-disinherit.js"></script>
<script src="attributes/hx-on.js"></script>
<script src="attributes/hx-on-wildcard.js"></script>