# All files / lib/tasks patch-node.js

100% Statements 47/47
100% Branches 5/5
100% Functions 2/2
100% Lines 47/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    482x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    263x
    7x
    7x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
    224x
     import patch from '../node/patch';
    import Transaction from '../transaction';
    import { CreateNodeHookCache, VTree } from '../util/types';
    import globalThis from '../util/global';

    /**
     * Processes a set of patches onto a tracked DOM Node.
     *
     * @param {Transaction} transaction
     * @return {void}
     */
    export default function patchNode(transaction) {
      const { mount, state, patches } = transaction;
      const { mutationObserver, measure, scriptsToExecute } = state;

      measure('patch node');

      const { ownerDocument } = /** @type {HTMLElement} */ (mount);
      const promises = transaction.promises || [];

      state.ownerDocument = ownerDocument || globalThis.document;

      // Always disconnect a MutationObserver before patching.
      if (mutationObserver) {
        mutationObserver.disconnect();
      }

      // Hook into the Node creation process to find all script tags, and mark them
      // for execution.
      const collectScripts = (/** @type {VTree} */vTree) => {
        if (vTree.nodeName === 'script') {
          scriptsToExecute.set(vTree, vTree.attributes.type);
        }
      };

      CreateNodeHookCache.add(collectScripts);

      // Skip patching completely if we aren't in a DOM environment.
      if (state.ownerDocument) {
        promises.push(...patch(patches, state));
      }

      CreateNodeHookCache.delete(collectScripts);

      transaction.promises = promises;
      measure('patch node');
    }