Skip to content

Commit

Permalink
support <script src='...'></script> tags
Browse files Browse the repository at this point in the history
  • Loading branch information
carson committed Sep 28, 2021
1 parent 538a8cd commit 60a6717
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
15 changes: 11 additions & 4 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -1396,13 +1396,20 @@ return (function () {

function evalScript(script) {
if (script.type === "text/javascript" || script.type === "") {
var newScript = getDocument().createElement("script");
forEach(script.attributes, function (attr) {
newScript.setAttribute(attr.name, attr.value);
});
newScript.textContent = script.textContent;
newScript.async = false;
var parent = script.parentElement;

try {
maybeEval(script, function () {
// wtf - https://stackoverflow.com/questions/9107240/1-evalthis-vs-evalthis-in-javascript
(1, eval)(script.innerText);
});
parent.insertBefore(newScript, script);
} catch (e) {
logError(e);
} finally {
parent.removeChild(script);
}
}
}
Expand Down
27 changes: 24 additions & 3 deletions test/core/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,13 +582,14 @@ describe("Core htmx AJAX Tests", function(){
it('script nodes can define global functions', function()
{
try {
this.server.respondWith("GET", "/test", "<script type='text/javascript'>function foo() { return 42 }</script>");
window.foo = {}
this.server.respondWith("GET", "/test", "<script type='text/javascript'>foo.bar = function() { return 42 }</script>");
var div = make("<div hx-get='/test'></div>");
div.click();
this.server.respond();
foo().should.equal(42);
foo.bar().should.equal(42);
} finally {
delete window.foo;
delete foo;
}
});

Expand Down Expand Up @@ -680,11 +681,14 @@ describe("Core htmx AJAX Tests", function(){

it('script node exceptions do not break rendering', function()
{
this.skip("Rendering does not break, but the exception bubbles up and mocha reports it");
this.server.respondWith("GET", "/test", "clicked<script type='text/javascript'>throw 'foo';</script>");
var div = make("<div hx-get='/test'></div>");
div.click();
this.server.respond();
div.innerText.should.equal("clicked");
console.log(div.innerText);
console.log("here");
});

it('allows empty verb values', function()
Expand Down Expand Up @@ -911,4 +915,21 @@ describe("Core htmx AJAX Tests", function(){
htmx.off("htmx:shouldSwap", handler);
});

it('scripts w/ src attribute are properly loaded', function(done)
{
try {
this.server.respondWith("GET", "/test", "<script src='setGlobal.js'></script>");
var div = make("<div hx-get='/test'></div>");
div.click();
this.server.respond();
setTimeout(function () {
window.globalWasCalled.should.equal(true);
delete window.globalWasCalled;
done();
}, 100);
} finally {
delete window.globalWasCalled;
}
});

})
1 change: 1 addition & 0 deletions test/setGlobal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window.globalWasCalled = true;

0 comments on commit 60a6717

Please sign in to comment.