Skip to content

Commit

Permalink
Handle script elements separately to ensure proper creation and appen…
Browse files Browse the repository at this point in the history
…ding.
  • Loading branch information
deeravenger committed Sep 14, 2024
1 parent d49ee4f commit 2aba77f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
30 changes: 16 additions & 14 deletions dist/liltag.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,31 +171,32 @@ class LilTag {
tempDiv.innerHTML = content.trim();
while (tempDiv.firstChild) {
const node = tempDiv.firstChild;
if (node instanceof HTMLElement) {
node.setAttribute(LilTag.DATA_ATTRIBUTE, tagId);
// Check if the node is a script element and create it programmatically
if (node instanceof HTMLScriptElement) {
const script = document.createElement("script");
script.src = node.src;
script.defer = node.defer;
script.setAttribute("data-domain", node.getAttribute("data-domain") || "");
script.setAttribute(LilTag.DATA_ATTRIBUTE, tagId); // Add tag ID attribute
// Append the script to the specified location
switch (location) {
case ContentLocation.Head:
if (node.nodeName === "SCRIPT" || node.nodeName === "NOSCRIPT") {
document.head.appendChild(node);
}
else {
console.warn("Injecting non-script content into <head> is not recommended.");
document.body.appendChild(node);
}
document.head.appendChild(script);
break;
case ContentLocation.BodyTop:
document.body.insertBefore(node, document.body.firstChild);
document.body.insertBefore(script, document.body.firstChild);
break;
case ContentLocation.BodyBottom:
document.body.appendChild(node);
document.body.appendChild(script);
break;
default:
console.warn(`Unknown location "${location}" - defaulting to body bottom.`);
document.body.appendChild(node);
document.body.appendChild(script);
}
}
else {
// If the node is not an HTMLElement, just append it to the correct location
else if (node instanceof HTMLElement) {
// For other HTML elements, append them as is
node.setAttribute(LilTag.DATA_ATTRIBUTE, tagId);
switch (location) {
case ContentLocation.Head:
document.head.appendChild(node);
Expand All @@ -210,6 +211,7 @@ class LilTag {
document.body.appendChild(node);
}
}
tempDiv.removeChild(node);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/liltag.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2aba77f

Please sign in to comment.