• 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

98.33
/packages/diffhtml/lib/node/create.js
1
/**
2✔
2
 * @typedef {import('../util/types').VTreeLike} VTreeLike
2✔
3
 * @typedef {import('../util/types').VTree} VTree
2✔
4
 * @typedef {import('../util/types').ValidNode} ValidNode
2✔
5
 */
2✔
6
import internalProcess from '../util/process';
2✔
7
import globalThis from '../util/global';
2✔
8
import { NodeCache, CreateNodeHookCache, EMPTY } from '../util/types';
2✔
9
import createTree from '../tree/create';
2✔
10

2✔
11
const namespace = 'http://www.w3.org/2000/svg';
2✔
12

2✔
13
/**
2✔
14
 * Takes in a Virtual Tree Element (VTree) and creates a DOM Node from it.
2✔
15
 * Sets the node into the Node cache. If this VTree already has an
2✔
16
 * associated node, it will reuse that.
2✔
17
 *
2✔
18
 * @param {VTreeLike} vTreeLike - A Virtual Tree Element or VTree-like element
2✔
19
 * @param {Document=} ownerDocument - Document to create Nodes in, defaults to document
2✔
20
 * @param {Boolean=} isSVG - Indicates if the root element is SVG
2✔
21
 * @return {ValidNode | null} A DOM Node matching the vTree
2✔
22
 */
2✔
23
export default function createNode(vTreeLike, ownerDocument = globalThis.document, isSVG) {
2✔
24
  if (internalProcess.env.NODE_ENV !== 'production') {
501✔
25
    if (!vTreeLike) {
501✔
26
      throw new Error('Missing VTree when trying to create DOM Node');
5✔
27
    }
5✔
28
  }
501✔
29

496✔
30
  const vTree = createTree(vTreeLike);
496✔
31
  const existingNode = NodeCache.get(vTree);
496✔
32

496✔
33
  // If the DOM Node was already created, reuse the existing node. This is
496✔
34
  // required to ensure that passed in DOM Nodes are preserved, and to ensure
496✔
35
  // that elements are not constantly created with the same VTree instance.
496✔
36
  if (existingNode) {
501✔
37
    return existingNode;
229✔
38
  }
229✔
39

267✔
40
  const {
267✔
41
    nodeName,
267✔
42
    rawNodeName = nodeName,
267✔
43
    childNodes = /** @type {VTree[]} */ ([]),
267✔
44
  } = vTree;
267✔
45

267✔
46
  isSVG = isSVG || nodeName === 'svg';
501✔
47

501✔
48
  /** @type {ValidNode | null} */
501✔
49
  let domNodeCheck = null;
501✔
50

501✔
51
  /** @type {ValidNode | null | void} */
501✔
52
  let retVal = null;
501✔
53

501✔
54
  CreateNodeHookCache.forEach((fn) => {
501✔
55
    // Invoke all the `createNodeHook` functions passing along the vTree as the
221✔
56
    // only argument. These functions must return a valid DOM Node value.
221✔
57
    if (retVal = fn(vTree)) {
221✔
58
      domNodeCheck = retVal;
1✔
59
    }
1✔
60
  });
501✔
61

501✔
62
  // It is not possible to continue if this object is falsy. By returning null,
501✔
63
  // patching can gracefully no-op.
501✔
64
  if (!ownerDocument) {
501!
65
    return domNodeCheck;
×
66
  }
×
67

267✔
68
  /**
267✔
69
   * If no DOM Node was provided by CreateNode hooks, we must create it
267✔
70
   * ourselves.
267✔
71
   *
267✔
72
   * @type {ValidNode | null}
267✔
73
   */
267✔
74
  let domNode = domNodeCheck;
267✔
75

267✔
76
  // Create empty text elements. They will get filled in during the patch
267✔
77
  // process.
267✔
78
  if (!domNode) {
342✔
79
    if (nodeName === '#comment') {
266✔
80
      domNode = ownerDocument.createComment(vTree.nodeValue || EMPTY.STR);
3!
81
    }
3✔
82
    else if (nodeName === '#text') {
263✔
83
      domNode = ownerDocument.createTextNode(vTree.nodeValue || EMPTY.STR);
105✔
84
    }
105✔
85
    // Support dynamically creating document fragments.
158✔
86
    else if (nodeName === '#document-fragment') {
158✔
87
      domNode = ownerDocument.createDocumentFragment();
1✔
88
    }
1✔
89
    // Support SVG.
157✔
90
    else if (isSVG) {
157✔
91
      domNode = ownerDocument.createElementNS(namespace, rawNodeName);
8✔
92
    }
8✔
93
    // If not a Text or SVG Node, then create with the standard method.
149✔
94
    else {
149✔
95
      domNode = ownerDocument.createElement(rawNodeName);
149✔
96
    }
149✔
97

266✔
98
    // Do not execute scripts by default.
266✔
99
    if (nodeName === 'script') {
266✔
100
      // Set the type to 'no-execute'. This will be toggled after patching,
8✔
101
      // unless disabled by the consumer.
8✔
102
      /** @type {any} */(domNode).type = 'no-execute';
8✔
103
    }
8✔
104
  }
266✔
105

267✔
106
  // Add to the domNodes cache.
267✔
107
  NodeCache.set(vTree, domNode);
267✔
108

267✔
109
  // Append all the children into the domNode, making sure to run them
267✔
110
  // through this `createNode` function as well.
267✔
111
  for (let i = 0; i < childNodes.length; i++) {
501✔
112
    const validChildNode = createNode(childNodes[i], ownerDocument, isSVG);
65✔
113

65✔
114
    if (domNode && validChildNode) {
65✔
115
      domNode.appendChild(validChildNode);
65✔
116
    }
65✔
117
  }
65✔
118

267✔
119
  return domNode;
267✔
120
}
501✔
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