mirror of
https://github.com/bigskysoftware/htmx.git
synced 2025-10-02 23:35:13 +00:00
fix double script execution issue when using template parsing
This commit is contained in:
parent
83e939a370
commit
ef791c51eb
23
src/htmx.js
23
src/htmx.js
@ -309,10 +309,24 @@ return (function () {
|
|||||||
content = content.replace(HEAD_TAG_REGEX, '');
|
content = content.replace(HEAD_TAG_REGEX, '');
|
||||||
}
|
}
|
||||||
if (htmx.config.useTemplateFragments && partialResponse) {
|
if (htmx.config.useTemplateFragments && partialResponse) {
|
||||||
var documentFragment = parseHTML("<body><template>" + content + "</template></body>", 0);
|
var fragment = parseHTML("<body><template>" + content + "</template></body>", 0);
|
||||||
// @ts-ignore type mismatch between DocumentFragment and Element.
|
// @ts-ignore type mismatch between DocumentFragment and Element.
|
||||||
// TODO: Are these close enough for htmx to use interchangeably?
|
// TODO: Are these close enough for htmx to use interchangeably?
|
||||||
return documentFragment.querySelector('template').content;
|
if (htmx.config.allowScriptTags) {
|
||||||
|
// if there is a nonce set up, set it on the new script tags
|
||||||
|
forEach(fragment.querySelectorAll("script"), function (script) {
|
||||||
|
if (htmx.config.inlineScriptNonce) {
|
||||||
|
script.nonce = htmx.config.inlineScriptNonce;
|
||||||
|
}
|
||||||
|
getInternalData(script).executed = true; // mark as executed due to template insertion semantics
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
forEach(fragment.querySelectorAll("script"), function (script) {
|
||||||
|
// remove all script tags if scripts are disabled
|
||||||
|
removeElement(script);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return fragment.querySelector('template').content;
|
||||||
}
|
}
|
||||||
switch (startTag) {
|
switch (startTag) {
|
||||||
case "thead":
|
case "thead":
|
||||||
@ -1892,7 +1906,8 @@ return (function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function evalScript(script) {
|
function evalScript(script) {
|
||||||
if (htmx.config.allowScriptTags && (script.type === "text/javascript" || script.type === "module" || script.type === "") ) {
|
if (!getInternalData(script).executed && htmx.config.allowScriptTags &&
|
||||||
|
(script.type === "text/javascript" || script.type === "module" || script.type === "") ) {
|
||||||
var newScript = getDocument().createElement("script");
|
var newScript = getDocument().createElement("script");
|
||||||
forEach(script.attributes, function (attr) {
|
forEach(script.attributes, function (attr) {
|
||||||
newScript.setAttribute(attr.name, attr.value);
|
newScript.setAttribute(attr.name, attr.value);
|
||||||
@ -1918,6 +1933,7 @@ return (function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function processScripts(elt) {
|
function processScripts(elt) {
|
||||||
|
if (!htmx.config.useTemplateFragments) {
|
||||||
if (matches(elt, "script")) {
|
if (matches(elt, "script")) {
|
||||||
evalScript(elt);
|
evalScript(elt);
|
||||||
}
|
}
|
||||||
@ -1925,6 +1941,7 @@ return (function () {
|
|||||||
evalScript(script);
|
evalScript(script);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function shouldProcessHxOn(elt) {
|
function shouldProcessHxOn(elt) {
|
||||||
var attributes = elt.attributes
|
var attributes = elt.attributes
|
||||||
|
@ -257,11 +257,11 @@ describe("Core htmx Regression Tests", function(){
|
|||||||
div.click();
|
div.click();
|
||||||
this.server.respond()
|
this.server.respond()
|
||||||
|
|
||||||
htmx.config.useTemplateFragments = oldUseTemplateFragmentsValue
|
|
||||||
|
|
||||||
setTimeout(function(){
|
setTimeout(function(){
|
||||||
window.i.should.equal(1);
|
window.i.should.equal(1);
|
||||||
delete window.i;
|
delete window.i;
|
||||||
|
htmx.config.useTemplateFragments = oldUseTemplateFragmentsValue
|
||||||
done();
|
done();
|
||||||
}, 50)
|
}, 50)
|
||||||
})
|
})
|
||||||
@ -278,11 +278,10 @@ describe("Core htmx Regression Tests", function(){
|
|||||||
div.click();
|
div.click();
|
||||||
this.server.respond()
|
this.server.respond()
|
||||||
|
|
||||||
htmx.config.useTemplateFragments = oldUseTemplateFragmentsValue
|
|
||||||
|
|
||||||
setTimeout(function(){
|
setTimeout(function(){
|
||||||
window.i.should.equal(1);
|
window.i.should.equal(1);
|
||||||
delete window.i;
|
delete window.i;
|
||||||
|
htmx.config.useTemplateFragments = oldUseTemplateFragmentsValue
|
||||||
done();
|
done();
|
||||||
}, 50)
|
}, 50)
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user