Merge pull request #277 from bigskysoftware/bencroker-patch-2

Change how getInputValues() works
This commit is contained in:
1cg 2020-12-29 10:11:00 -07:00 committed by GitHub
commit 20436f243c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 6 deletions

View File

@ -1568,7 +1568,11 @@ return (function () {
function getInputValues(elt, verb) {
var processed = [];
var values = {};
var values = {
form: {},
element: {},
includes: {},
};
var errors = [];
// only validate when form is directly submitted and novalidate is not set
@ -1576,23 +1580,25 @@ return (function () {
// for a non-GET include the closest form
if (verb !== 'get') {
processInputValue(processed, values, errors, closest(elt, 'form'), validate);
processInputValue(processed, values.form, errors, closest(elt, 'form'), validate);
}
// include the element itself
processInputValue(processed, values, errors, elt, validate);
processInputValue(processed, values.element, errors, elt, validate);
// include any explicit includes
var includes = getClosestAttributeValue(elt, "hx-include");
if (includes) {
var nodes = getDocument().querySelectorAll(includes);
forEach(nodes, function(node) {
processInputValue(processed, values, errors, node, validate);
processInputValue(processed, values.includes, errors, node, validate);
});
}
var mergedValues = mergeObjects(values.includes, values.element);
mergedValues = mergeObjects(mergedValues, values.form);
return {errors:errors, values:values};
return {errors:errors, values:mergedValues};
}
function appendParam(returnStr, name, realValue) {

View File

@ -34,6 +34,22 @@ describe("hx-include attribute", function() {
div.innerHTML.should.equal("Clicked!");
});
it('non-GET includes closest form and overrides values included that exist outside the form', function () {
this.server.respondWith("POST", "/include", function (xhr) {
var params = getParameters(xhr);
params['i1'].should.equal("test");
xhr.respond(200, {}, "Clicked!")
});
var div = make('<div hx-include="*" hx-target="this">' +
'<input name="i1" value="before"/>' +
'<form><div id="d1" hx-post="/include"></div><input name="i1" value="test"/></form>' +
'<input name="i1" value="after"/>')
var input = byId("d1")
input.click();
this.server.respond();
div.innerHTML.should.equal("Clicked!");
});
it('GET does not include closest form by default', function () {
this.server.respondWith("GET", "/include", function (xhr) {
var params = getParameters(xhr);
@ -150,4 +166,4 @@ describe("hx-include attribute", function() {
});
});
});