Skip to content

Commit 775975c

Browse files
refactor(types): tidy types in client and server utilities
1 parent ebf0bc9 commit 775975c

3 files changed

Lines changed: 24 additions & 22 deletions

File tree

src/client/utilities.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Comment, Element, ProcessingInstruction, Text } from 'domhandler';
2+
23
import { CASE_SENSITIVE_TAG_NAMES_MAP } from './constants';
4+
import type { DOMNode } from '../types';
35

46
/**
57
* Gets case-sensitive tag name.
@@ -59,10 +61,10 @@ function formatTagName(tagName: string): string {
5961
*/
6062
export function formatDOM(
6163
nodes: NodeList,
62-
parent: Element | null = null,
64+
parent: DOMNode | null = null,
6365
directive?: string,
64-
): (Comment | Element | ProcessingInstruction | Text)[] {
65-
const result = [];
66+
): DOMNode[] {
67+
const domNodes = [];
6668
let current;
6769
let index = 0;
6870
const nodesLength = nodes.length;
@@ -105,32 +107,33 @@ export function formatDOM(
105107
}
106108

107109
// set previous node next
108-
const prev = result[index - 1] || null;
110+
const prev = domNodes[index - 1] || null;
109111
if (prev) {
110112
prev.next = current;
111113
}
112114

113115
// set properties for current node
114-
current.parent = parent;
116+
current.parent = parent as Element;
115117
current.prev = prev;
116118
current.next = null;
117119

118-
result.push(current);
120+
domNodes.push(current);
119121
}
120122

121123
if (directive) {
122124
current = new ProcessingInstruction(
123125
directive.substring(0, directive.indexOf(' ')).toLowerCase(),
124126
directive,
125127
);
126-
current.next = result[0] || null;
127-
current.parent = parent;
128-
result.unshift(current);
129128

130-
if (result[1]) {
131-
result[1].prev = result[0];
129+
current.next = domNodes[0] || null;
130+
current.parent = parent as Element;
131+
domNodes.unshift(current);
132+
133+
if (domNodes[1]) {
134+
domNodes[1].prev = domNodes[0];
132135
}
133136
}
134137

135-
return result;
138+
return domNodes;
136139
}

src/server/utilities.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
import type {
2-
ChildNode,
3-
Comment,
4-
Element,
5-
ProcessingInstruction,
6-
Text,
7-
} from 'domhandler';
1+
import type { ChildNode } from 'domhandler';
82

9-
type Node = Element | Text | Comment | ProcessingInstruction;
3+
import type { DOMNode } from '../types';
104

115
/**
126
* Sets root parent to null.
137
*
148
* @param nodes - Nodes.
159
* @returns - Nodes.
1610
*/
17-
export function unsetRootParent(nodes: ChildNode[]): Node[] {
11+
export function unsetRootParent(nodes: ChildNode[]): DOMNode[] {
1812
let index = 0;
1913
const nodesLength = nodes.length;
2014

@@ -23,5 +17,5 @@ export function unsetRootParent(nodes: ChildNode[]): Node[] {
2317
node.parent = null;
2418
}
2519

26-
return nodes as Node[];
20+
return nodes as DOMNode[];
2721
}

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { Comment, Element, ProcessingInstruction, Text } from 'domhandler';
2+
3+
export { Comment, Element, ProcessingInstruction, Text };
4+
5+
export type DOMNode = Comment | Element | ProcessingInstruction | Text;

0 commit comments

Comments
 (0)