• 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

95.87
/packages/diffhtml/lib/to-string.js
1
/**
2✔
2
 * @typedef {import('./util/types').ValidInput} ValidInput
2✔
3
 * @typedef {import('./util/types').TransactionConfig} TransactionConfig
2✔
4
 * @typedef {import('./util/types').VTree} VTree
2✔
5
 * @typedef {import('./util/types').VTreeAttributes} VTreeAttributes
2✔
6
 */
2✔
7
import createTree from './tree/create';
2✔
8
import Transaction, { defaultTasks, tasks } from './transaction';
2✔
9
import release from './release';
2✔
10

2✔
11
const { keys } = Object;
2✔
12

2✔
13
/**
2✔
14
 * Renders input to a string.
2✔
15
 *
2✔
16
 * Works like outerHTML by rendering a VTree and returning the source. This
2✔
17
 * will accept any input that `outerHTML` normally accepts. This is a true
2✔
18
 * render, but omits the DOM patching task.
2✔
19
 *
2✔
20
 * @param {ValidInput} input
2✔
21
 * @param {TransactionConfig} config
2✔
22
 * @return {string}
2✔
23
 */
2✔
24
export default function toString(input, config = {}) {
2✔
25
  const oldTree = createTree();
10✔
26
  const activeTasks = new Set(config.tasks || defaultTasks);
10✔
27

10✔
28
  activeTasks.delete(tasks.patchNode);
10✔
29
  // Replace the `endAsTransaction` task with the string return value.
10✔
30
  activeTasks.delete(tasks.endAsTransaction);
10✔
31
  activeTasks.add(function endAsString(/** @type {Transaction} */ transaction) {
10✔
32
    return serializeVTree(transaction.oldTree);
10✔
33
  });
10✔
34

10✔
35
  config.tasks = [...activeTasks];
10✔
36
  config.inner = true;
10✔
37

10✔
38
  let markup = '';
10✔
39

10✔
40
  try {
10✔
41
    markup = /** @type {string} */ (
10✔
42
      Transaction.create(oldTree, input, config).start()
10✔
43
    );
10✔
44
  }
10✔
45
  catch (e) {
10!
46
    release(oldTree);
×
47
    throw e;
×
48
  }
×
49

10✔
50
  release(oldTree);
10✔
51

10✔
52
  return markup;
10✔
53
}
10✔
54

2✔
55
/**
2✔
56
 * serializeAttributes
2✔
57
 *
2✔
58
 * Takes in a diffHTML VTree attributes object and turns it into a key=value
2✔
59
 * string.
2✔
60
 *
2✔
61
 * @param {VTreeAttributes} attributes
2✔
62
 * @return {String}
2✔
63
 */
2✔
64
function serializeAttributes(attributes) {
16✔
65
  const attrs = keys(attributes);
16✔
66

16✔
67
  return attrs.length ? ' ' + attrs.map((keyName) => {
16✔
68
    const value = attributes[keyName];
3✔
69
    const isFalsy = !value;
3✔
70
    const isDynamic = typeof value === 'object' || typeof value === 'function';
3✔
71

3✔
72
    if (value === true) {
3✔
73
      return keyName;
1✔
74
    }
1✔
75

2✔
76
    return `${keyName}${(!isFalsy && !isDynamic) ? `="${String(value)}"` : ''}`;
3✔
77
  }).join(' ') : '';
16✔
78
}
16✔
79

2✔
80
/**
2✔
81
 * serializeVTree
2✔
82
 *
2✔
83
 * Takes in a diffHTML VTree object and turns it into a string of HTML.
2✔
84
 *
2✔
85
 * @param {VTree=} vTree
2✔
86
 * @return {String}
2✔
87
 */
2✔
88
function serializeVTree(vTree) {
29✔
89
  let output = '';
29✔
90

29✔
91
  if (!vTree) {
29!
92
    return output;
×
93
  }
×
94

29✔
95
  const { childNodes, nodeType, nodeName: tag, nodeValue, attributes } = vTree;
29✔
96

29✔
97
  // Document fragment.
29✔
98
  if (nodeType === 11) {
29✔
99
    for (let i = 0; i < childNodes.length; i++) {
10✔
100
      output += serializeVTree(childNodes[i]);
15✔
101
    }
15✔
102
  }
10✔
103
  // Empty element.
19✔
104
  else if (!(childNodes.length) && nodeType === 1) {
19✔
105
    output += `<${tag}${serializeAttributes(attributes)}></${tag}>`;
13✔
106
  }
13✔
107
  // Text Nodes.
6✔
108
  else if (nodeType === 3) {
6✔
109
    output += nodeValue;
3✔
110
  }
3✔
111
  // Presentational DOM Node.
3✔
112
  else if (childNodes.length) {
3✔
113
    const children = childNodes.map(childNode =>
3✔
114
      `${serializeVTree(childNode)}`
4✔
115
    ).join('');
3✔
116

3✔
117
    output += `<${tag}${serializeAttributes(attributes)}>${children}</${tag}>`;
3✔
118
  }
3✔
119

29✔
120
  return output;
29✔
121
}
29✔
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