Skip to content

Commit

Permalink
refactor(types): tidy types in client and server utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Oct 21, 2023
1 parent ebf0bc9 commit 775975c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
27 changes: 15 additions & 12 deletions src/client/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Comment, Element, ProcessingInstruction, Text } from 'domhandler';

import { CASE_SENSITIVE_TAG_NAMES_MAP } from './constants';
import type { DOMNode } from '../types';

/**
* Gets case-sensitive tag name.
Expand Down Expand Up @@ -59,10 +61,10 @@ function formatTagName(tagName: string): string {
*/
export function formatDOM(
nodes: NodeList,
parent: Element | null = null,
parent: DOMNode | null = null,
directive?: string,
): (Comment | Element | ProcessingInstruction | Text)[] {
const result = [];
): DOMNode[] {
const domNodes = [];
let current;
let index = 0;
const nodesLength = nodes.length;
Expand Down Expand Up @@ -105,32 +107,33 @@ export function formatDOM(
}

// set previous node next
const prev = result[index - 1] || null;
const prev = domNodes[index - 1] || null;
if (prev) {
prev.next = current;
}

// set properties for current node
current.parent = parent;
current.parent = parent as Element;
current.prev = prev;
current.next = null;

result.push(current);
domNodes.push(current);
}

if (directive) {
current = new ProcessingInstruction(
directive.substring(0, directive.indexOf(' ')).toLowerCase(),
directive,
);
current.next = result[0] || null;
current.parent = parent;
result.unshift(current);

if (result[1]) {
result[1].prev = result[0];
current.next = domNodes[0] || null;
current.parent = parent as Element;
domNodes.unshift(current);

if (domNodes[1]) {
domNodes[1].prev = domNodes[0];
}
}

return result;
return domNodes;
}
14 changes: 4 additions & 10 deletions src/server/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import type {
ChildNode,
Comment,
Element,
ProcessingInstruction,
Text,
} from 'domhandler';
import type { ChildNode } from 'domhandler';

type Node = Element | Text | Comment | ProcessingInstruction;
import type { DOMNode } from '../types';

/**
* Sets root parent to null.
*
* @param nodes - Nodes.
* @returns - Nodes.
*/
export function unsetRootParent(nodes: ChildNode[]): Node[] {
export function unsetRootParent(nodes: ChildNode[]): DOMNode[] {
let index = 0;
const nodesLength = nodes.length;

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

return nodes as Node[];
return nodes as DOMNode[];
}
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Comment, Element, ProcessingInstruction, Text } from 'domhandler';

export { Comment, Element, ProcessingInstruction, Text };

export type DOMNode = Comment | Element | ProcessingInstruction | Text;

0 comments on commit 775975c

Please sign in to comment.