File size: 1,062 Bytes
d605f27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

const traverse = (node, handle) => {
	handle(node);

	if (node.childNodes) {
		for (let i = 0; i < node.childNodes.length; ++i)
			traverse(node.childNodes[i], handle);
	}
};


const childrenWithTag = (node: any, tagName: string): any[] => {
	const children = Array.from(node.childNodes);
	return children.filter((node: any) => node.tagName === tagName);
};


const hasChildrenWithTag = (node: any, tagName: string): boolean => {
	const children = Array.from(node.childNodes);
	return children.some((node: any) => node.tagName === tagName);
};


const findPreviousSibling = (node: any, tagName: string): any => {
	let sibling = node.previousSibling;
	while (sibling && sibling.tagName !== tagName)
		sibling = sibling.previousSibling;

	return sibling;
};


const findNextSibling = (node: any, tagName: string): any => {
	let sibling = node.nextSibling;
	while (sibling && sibling.tagName !== tagName)
		sibling = sibling.nextSibling;

	return sibling;
};



export {
	traverse,
	childrenWithTag,
	hasChildrenWithTag,
	findPreviousSibling,
	findNextSibling,
};