Skip to content

Commit

Permalink
Merge 5ad3622 into 2eecafe
Browse files Browse the repository at this point in the history
  • Loading branch information
tbranyen committed Jan 19, 2022
2 parents 2eecafe + 5ad3622 commit 77225d6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 47 deletions.
79 changes: 42 additions & 37 deletions packages/diffhtml/lib/transaction.js
Expand Up @@ -23,6 +23,8 @@ import release from './release';
import getConfig from './util/config';
import hasModule from './util/has-module';

const { assign } = Object;

export const defaultTasks = [
schedule, shouldUpdate, reconcileTrees, syncTrees, patchNode, endAsPromise,
];
Expand Down Expand Up @@ -230,46 +232,49 @@ export default class Transaction {
state.isDirty = true;
}

// Execute all queued scripts.
scriptsToExecute.forEach((type = EMPTY.STR, vTree) => {
const oldNode = /** @type {HTMLElement} */ (NodeCache.get(vTree));

// Reset the type value.
if (type) oldNode.setAttribute('type', type);
else oldNode.removeAttribute('type');
});

// Save the markup immediately after patching.
state.previousMarkup = 'outerHTML' in mountAsHTMLEl ? mountAsHTMLEl.outerHTML : EMPTY.STR;

// Only execute scripts if the configuration is set. By default this is set
// to true. You can toggle this behavior for your app to disable script
// execution.
if (config.executeScripts) {
// Execute deferred scripts by cloning them and reattaching into the same
// position.
scriptsToExecute.forEach((_, vTree)=> {
const oldNode = NodeCache.get(vTree);
const newNode = /** @type {any} */ (oldNode).cloneNode(true);

if (!oldNode || (hasModule() && newNode.type === 'nomodule')) {
return;
}

// If the script is now the root element, make sure we cleanup and
// re-assign.
if (StateCache.has(oldNode)) {
release(oldNode);
StateCache.set(newNode, state);
}

// Replace the node association.
NodeCache.set(vTree, newNode);

// Replace the scripts to trigger default browser behavior.
oldNode.parentNode && oldNode.parentNode.replaceChild(newNode, oldNode);
});
}
// Execute deferred scripts by cloning them and reattaching into the same
// position.
scriptsToExecute.forEach((type, vTree)=> {
const oldNode = NodeCache.get(vTree);

// Reset the type attribute back to the original.
oldNode.type = type;

if (!config.executeScripts || (hasModule() && type === 'nomodule')) {
return;
}

// Copy over properties to the new script element.
const newNode = assign(
oldNode.ownerDocument.createElement('script'),
oldNode,
);

// Copy over attributes.
for (let key in vTree.attributes) {
const value = vTree.attributes[key];
newNode.setAttribute(key, value);
}

// Copy over body.
newNode.textContent = oldNode.textContent;

// If the script is now the root element, make sure we cleanup and
// re-assign.
if (StateCache.has(oldNode)) {
release(oldNode);
StateCache.set(newNode, state);
}

// Replace the node association.
NodeCache.set(vTree, newNode);

// Replace the scripts to trigger default browser behavior.
oldNode.parentNode && oldNode.parentNode.replaceChild(newNode, oldNode);
});

// Empty the scripts to execute.
scriptsToExecute.clear();
Expand Down
1 change: 0 additions & 1 deletion packages/diffhtml/lib/util/types.js
Expand Up @@ -225,7 +225,6 @@ export const Supplemental = EMPTY.OBJ;
* @property {VTree=} oldTree
* @property {Boolean=} isRendering
* @property {Boolean=} isDirty
* @property {String=} previousMarkup
* @property {MutationObserver=} mutationObserver
* @property {import('../transaction').default} activeTransaction
* @property {import('../transaction').default=} nextTransaction
Expand Down
9 changes: 0 additions & 9 deletions packages/diffhtml/test/transaction.js
Expand Up @@ -322,15 +322,6 @@ describe('Transaction', function() {
strictEqual(transaction.completed, true);
});

it('will set the state', () => {
const { mount, input, config } = suite;
const transaction = Transaction.create(mount, input, config);

transaction.end();

strictEqual(transaction.state.previousMarkup, '<div></div>');
});

it('will change isRendering', () => {
const { mount, input, config } = suite;
const transaction = Transaction.create(mount, input, config);
Expand Down

0 comments on commit 77225d6

Please sign in to comment.