• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tbranyen / diffhtml / 12189631411

05 Dec 2024 11:09PM CUT coverage: 98.581%. Remained the same
12189631411

Pull #349

github

web-flow
Merge 3ee60590a into f25d1c4ac
Pull Request #349: Bump path-to-regexp and express in /packages/diffhtml-static-sync

840 of 896 branches covered (93.75%)

4795 of 4864 relevant lines covered (98.58%)

427.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.74
/packages/diffhtml/lib/tasks/sync-trees.js
1
/**
2✔
2
 * @typedef {import('../util/types').Mount} Mount
2✔
3
 * @typedef {import('../transaction').default} Transaction
2✔
4
 */
2✔
5
import syncTree from '../tree/sync';
2✔
6
import createNode from '../node/create';
2✔
7
import { StateCache, NODE_TYPE, PATCH_TYPE, EMPTY } from '../util/types';
2✔
8
import internalProcess from '../util/process';
2✔
9

2✔
10
export default function syncTrees(/** @type {Transaction} */ transaction) {
2✔
11
  const { state, state: { measure }, oldTree, newTree, mount } = transaction;
164✔
12

164✔
13
  measure('sync trees');
164✔
14

164✔
15
  if (internalProcess.env.NODE_ENV !== 'production') {
164✔
16
    if (!oldTree) {
162!
17
      throw new Error('Missing old tree during synchronization');
×
18
    }
×
19

162✔
20
    if (!newTree) {
162!
21
      throw new Error('Missing new tree during synchronization');
×
22
    }
×
23
  }
162✔
24

162✔
25
  // Do a global replace of the element, unable to do this at a lower level.
162✔
26
  // Ignore this for document fragments, they don't appear in the DOM and we
162✔
27
  // treat them as transparent containers.
162✔
28
  if (
162✔
29
    oldTree &&
162✔
30
    newTree &&
164✔
31
    oldTree.nodeName !== newTree.nodeName &&
164✔
32
    newTree.nodeType !== NODE_TYPE.FRAGMENT
5✔
33
  ) {
164✔
34
    // If there is no `parentNode` for the replace operation, we will need to
3✔
35
    // throw an error and prevent the `StateCache` from being updated.
3✔
36
    if (internalProcess.env.NODE_ENV !== 'production') {
3✔
37
      if (!/** @type {HTMLElement} */ (mount).parentNode) {
3✔
38
        throw new Error('Unable to replace top level node without a parent');
1✔
39
      }
1✔
40
    }
3✔
41

2✔
42
    // Replace the top level elements.
2✔
43
    transaction.patches = [
2✔
44
      PATCH_TYPE.REPLACE_CHILD,
2✔
45
      newTree,
2✔
46
      oldTree,
2✔
47
    ];
2✔
48

2✔
49
    // Clean up the existing old tree, and mount the new tree.
2✔
50
    transaction.oldTree = state.oldTree = newTree;
2✔
51

2✔
52
    const newNode = createNode(newTree);
2✔
53

2✔
54
    // Update the StateCache since we are changing the top level element.
2✔
55
    StateCache.delete(mount);
2✔
56
    StateCache.set(/** @type {Mount} */ (newNode), state);
2✔
57

2✔
58
    transaction.mount = /** @type {HTMLElement} */ (newNode);
2✔
59

2✔
60
    if (newTree.nodeName === 'script') {
3✔
61
      state.scriptsToExecute.set(newTree, newTree.attributes.type || EMPTY.STR);
1✔
62
    }
1✔
63
  }
3✔
64
  // Synchronize the top level elements.
159✔
65
  else {
159✔
66
    transaction.patches = syncTree(
159✔
67
      oldTree || null,
159!
68
      newTree || null,
159!
69
      [],
159✔
70
      state,
159✔
71
      transaction,
159✔
72
    );
159✔
73
  }
159✔
74

161✔
75
  measure('sync trees');
161✔
76
}
164✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc