123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- (function(){
- var api = null;
- function log() {
-
- }
- function mergeHead(newContent, defaultMergeStrategy) {
- if (newContent && newContent.indexOf('<head') > -1) {
- const htmlDoc = document.createElement("html");
-
- var contentWithSvgsRemoved = newContent.replace(/<svg(\s[^>]*>|>)([\s\S]*?)<\/svg>/gim, '');
-
- var headTag = contentWithSvgsRemoved.match(/(<head(\s[^>]*>|>)([\s\S]*?)<\/head>)/im);
-
- if (headTag) {
- var added = []
- var removed = []
- var preserved = []
- var nodesToAppend = []
- htmlDoc.innerHTML = headTag;
- var newHeadTag = htmlDoc.querySelector("head");
- var currentHead = document.head;
- if (newHeadTag == null) {
- return;
- } else {
-
- var srcToNewHeadNodes = new Map();
- for (const newHeadChild of newHeadTag.children) {
- srcToNewHeadNodes.set(newHeadChild.outerHTML, newHeadChild);
- }
- }
-
- var mergeStrategy = api.getAttributeValue(newHeadTag, "hx-head") || defaultMergeStrategy;
-
- for (const currentHeadElt of currentHead.children) {
-
- var inNewContent = srcToNewHeadNodes.has(currentHeadElt.outerHTML);
- var isReAppended = currentHeadElt.getAttribute("hx-head") === "re-eval";
- var isPreserved = api.getAttributeValue(currentHeadElt, "hx-preserve") === "true";
- if (inNewContent || isPreserved) {
- if (isReAppended) {
-
- removed.push(currentHeadElt);
- } else {
-
-
- srcToNewHeadNodes.delete(currentHeadElt.outerHTML);
- preserved.push(currentHeadElt);
- }
- } else {
- if (mergeStrategy === "append") {
-
-
- if (isReAppended) {
- removed.push(currentHeadElt);
- nodesToAppend.push(currentHeadElt);
- }
- } else {
-
- if (api.triggerEvent(document.body, "htmx:removingHeadElement", {headElement: currentHeadElt}) !== false) {
- removed.push(currentHeadElt);
- }
- }
- }
- }
-
-
- nodesToAppend.push(...srcToNewHeadNodes.values());
- log("to append: ", nodesToAppend);
- for (const newNode of nodesToAppend) {
- log("adding: ", newNode);
- var newElt = document.createRange().createContextualFragment(newNode.outerHTML);
- log(newElt);
- if (api.triggerEvent(document.body, "htmx:addingHeadElement", {headElement: newElt}) !== false) {
- currentHead.appendChild(newElt);
- added.push(newElt);
- }
- }
-
-
- for (const removedElement of removed) {
- if (api.triggerEvent(document.body, "htmx:removingHeadElement", {headElement: removedElement}) !== false) {
- currentHead.removeChild(removedElement);
- }
- }
- api.triggerEvent(document.body, "htmx:afterHeadMerge", {added: added, kept: preserved, removed: removed});
- }
- }
- }
- htmx.defineExtension("head-support", {
- init: function(apiRef) {
-
- api = apiRef;
- htmx.on('htmx:afterSwap', function(evt){
- var serverResponse = evt.detail.xhr.response;
- if (api.triggerEvent(document.body, "htmx:beforeHeadMerge", evt.detail)) {
- mergeHead(serverResponse, evt.detail.boosted ? "merge" : "append");
- }
- })
- htmx.on('htmx:historyRestore', function(evt){
- if (api.triggerEvent(document.body, "htmx:beforeHeadMerge", evt.detail)) {
- if (evt.detail.cacheMiss) {
- mergeHead(evt.detail.serverResponse, "merge");
- } else {
- mergeHead(evt.detail.item.head, "merge");
- }
- }
- })
- htmx.on('htmx:historyItemCreated', function(evt){
- var historyItem = evt.detail.item;
- historyItem.head = document.head.outerHTML;
- })
- }
- });
- })()
|