Skip to content

Commit

Permalink
deep clone preserved nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
codediodeio committed Aug 29, 2022
1 parent d569695 commit cbfed25
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
8 changes: 8 additions & 0 deletions example/about/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ <h1 id="heading">About</h1>
<a href="/">Home</a> | <a href="#chapter">Heading</a> | <a href="https://google.com">External</a> |
<button id="go">Go</button>
<button id="back">Back</button>

<div id="keep" flamethrower-preserve>
<article>
<p>This text should be preserved when starting from about</p>
</article>
</div>


<script type="module">
const router = window.flamethrower;
document.getElementById('go').onclick = () => {
Expand Down
8 changes: 7 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@
<div id="load-bar"></div>
<h1 id="heading">Home</h1>
<a href="/">Home</a> | <a id="about" href="/about">About</a> |
<!-- <a id="test" data-cold href="/test">Test</a> #} -->

<div id="keep" flamethrower-preserve>
<article>
<p>This text should be preserved when starting from home</p>
</article>
</div>

<script>
// test
window.addEventListener('flamethrower:router:fetch-progress', ({ detail }) => {
Expand Down
25 changes: 2 additions & 23 deletions lib/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@ export function formatNextDocument(html: string): Document {
return parser.parseFromString(html, 'text/html');
}

/**
* @param {Element} target
* @param {Element} source
* Clone Attributes to new Node
*/
function cloneAttributes(target: Element, source: Element) {
Array.from(source.attributes).forEach((attr) => {
target.setAttribute(attr.nodeName, attr.nodeValue);
});
}

/**
* @param {Document} nextDoc
* Replace Body
Expand All @@ -27,18 +16,8 @@ export function replaceBody(nextDoc: Document): void {
nodesToPreserve.forEach((oldDocElement) => {
let nextDocElement = nextDoc.body.querySelector('[flamethrower-preserve][id="' + oldDocElement.id + '"]');
if (nextDocElement) {
// same element found in next doc

// copy attributes
cloneAttributes(oldDocElement, nextDocElement);

// copy new child nodes
while (oldDocElement.firstChild) {
oldDocElement.removeChild(oldDocElement.firstChild);
}
nextDocElement.childNodes.forEach((node) => oldDocElement.appendChild(node));

nextDocElement.replaceWith(oldDocElement);
const clone = oldDocElement.cloneNode(true);
nextDocElement.replaceWith(clone);
}
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flamethrower-router",
"version": "0.0.0-meme.10",
"version": "0.0.0-meme.11",
"description": "Blazingly fast SPA-like router for static sites",
"main": "dist/main.js",
"types": "dist/main.d.ts",
Expand Down

0 comments on commit cbfed25

Please sign in to comment.