-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathdom.js
More file actions
66 lines (55 loc) · 1.47 KB
/
Copy pathdom.js
File metadata and controls
66 lines (55 loc) · 1.47 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Helper for making Elements with attributes
*
* @param {string} tagName - new Element tag name
* @param {Array|string} classNames - list or name of CSS classname(s)
* @param {object} attributes - any attributes
* @returns {Element}
*/
export function make(tagName, classNames = null, attributes = {}) {
const el = document.createElement(tagName);
if (Array.isArray(classNames)) {
el.classList.add(...classNames);
} else if (classNames) {
el.classList.add(classNames);
}
for (const attrName in attributes) {
el[attrName] = attributes[attrName];
}
return el;
}
/**
* Returns the HTML content of passed Document Fragment
*
* @param {DocumentFragment} fragment - document fragment to process
* @returns {string}
*/
export function fragmentToString(fragment) {
const div = make('div');
div.appendChild(fragment);
if (!div.innerText.trim()) {
return '';
}
return div.innerHTML;
}
/**
* breadth-first search (BFS)
* {@link https://en.wikipedia.org/wiki/Breadth-first_search}
*
* @description Pushes to stack all DOM leafs and checks for emptiness
* @param {Node} node - node to check
* @returns {boolean}
*/
export function isEmpty(node) {
let content;
if (node.nodeType !== Node.ELEMENT_NODE) {
content = node.textContent;
} else {
content = node.innerHTML;
/**
* Don't count <br>s as content
*/
content = content.replaceAll('<br>', '');
}
return content.trim().length === 0;
}