xpine 0.0.28 → 0.0.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/src/static/spa.js
CHANGED
|
@@ -40,16 +40,26 @@ async function getNewPageContent(href) {
|
|
|
40
40
|
const dom = parser.parseFromString(text, "text/html");
|
|
41
41
|
diffHead(dom);
|
|
42
42
|
const newScripts = removeBodyScripts(dom);
|
|
43
|
-
const
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
43
|
+
const { persistentArea, persistentElements } = replacePersistentNodesOnDocument(dom);
|
|
44
|
+
const documentRoot = document.body.querySelector("#xpine-root");
|
|
45
|
+
const domRoot = dom?.body?.querySelector("#xpine-root");
|
|
46
|
+
if (!documentRoot || !domRoot) {
|
|
47
|
+
throw {
|
|
48
|
+
message: "Every page must be wrapped in an #xpine-root tag",
|
|
49
|
+
documentRoot,
|
|
50
|
+
domRoot
|
|
51
|
+
};
|
|
49
52
|
}
|
|
53
|
+
documentRoot?.replaceWith(domRoot);
|
|
54
|
+
for (const el of persistentElements) {
|
|
55
|
+
const newNode = document.body.querySelector(`[data-persistent="${el?.getAttribute("data-persistent")}"]`);
|
|
56
|
+
newNode.replaceWith(el);
|
|
57
|
+
}
|
|
58
|
+
persistentArea.remove();
|
|
50
59
|
replaceAttributesOnDocumentBody(dom);
|
|
51
60
|
replaceDocumentContentsWithLinkResponse();
|
|
52
|
-
|
|
61
|
+
const newDocumentRoot = document.body.querySelector("#xpine-root");
|
|
62
|
+
for (const script of newScripts) newDocumentRoot.appendChild(script);
|
|
53
63
|
const event = new CustomEvent("spa:updatePageContent", {
|
|
54
64
|
detail: {
|
|
55
65
|
href
|
|
@@ -100,7 +110,7 @@ function removeBodyScripts(dom) {
|
|
|
100
110
|
const element = document.createElement("script");
|
|
101
111
|
portAttributesToElement(script, element);
|
|
102
112
|
element.innerHTML = script.innerHTML;
|
|
103
|
-
|
|
113
|
+
script.remove();
|
|
104
114
|
newElements.push(element);
|
|
105
115
|
}
|
|
106
116
|
return newElements;
|
|
@@ -110,13 +120,25 @@ function portAttributesToElement(oldEl, newEl) {
|
|
|
110
120
|
newEl.setAttribute(attribute.name, attribute.nodeValue);
|
|
111
121
|
}
|
|
112
122
|
}
|
|
113
|
-
function
|
|
114
|
-
const
|
|
123
|
+
function replacePersistentNodesOnDocument(dom) {
|
|
124
|
+
const persistentArea = document.createElement("div");
|
|
125
|
+
persistentArea.style.display = "none";
|
|
126
|
+
document.body.appendChild(persistentArea);
|
|
127
|
+
const domPersistentNodes = [...dom.querySelectorAll("[data-persistent]")];
|
|
128
|
+
const persistentNodesFromDom = domPersistentNodes.map((el) => el.getAttribute("data-persistent"));
|
|
115
129
|
const persistentNodesFromBody = [...document.body.querySelectorAll("[data-persistent]")];
|
|
116
|
-
const
|
|
117
|
-
|
|
130
|
+
const persistentElements = [];
|
|
131
|
+
persistentNodesFromBody.forEach((el) => {
|
|
132
|
+
const persistentAttribute = el.getAttribute("data-persistent");
|
|
133
|
+
if (!persistentNodesFromDom.includes(persistentAttribute)) return false;
|
|
134
|
+
dom.querySelector(`[data-persistent="${persistentAttribute}"]`).innerHTML = "";
|
|
135
|
+
persistentArea.appendChild(el);
|
|
136
|
+
persistentElements.push(el);
|
|
118
137
|
});
|
|
119
|
-
return
|
|
138
|
+
return {
|
|
139
|
+
persistentElements,
|
|
140
|
+
persistentArea
|
|
141
|
+
};
|
|
120
142
|
}
|
|
121
143
|
function replaceAttributesOnDocumentBody(dom) {
|
|
122
144
|
while (document.body.attributes.length > 0) {
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"onInit.d.ts","sourceRoot":"","sources":["../../../../src/util/hooks/onInit.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAI5C,MAAM,CAAC,OAAO,UAAU,YAAY,CAAC,MAAM,EAAE,UAAU,CAAC,QAAQ,CAAC,uBAOhE"}
|