-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (147 loc) · 4.18 KB
/
index.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* @param {{
tagName: string;
state: number;
attributes: { name: string, value: string }[];
}} node
* @returns {HTMLDivElement}
*/
function getTagDescriptionElement(node) {
const result = document.createElement("span");
result.appendChild(document.createTextNode(`<${node.tagName}`));
node.attributes.forEach(a => {
const name = document.createElement("span");
name.classList.add("view__attrName");
name.innerText = ` ${a.name}=`;
result.appendChild(name);
const value = document.createElement("span");
value.classList.add("view__attrValue");
value.innerText = `"${a.value}"`;
result.appendChild(value);
});
result.appendChild(document.createTextNode(`${node.state === NODE_STATE.selfClosed ? " /" : ""}>`));
return result;
}
/**
* @returns {HTMLDivElement}
*/
function getExpandMarkerElement() {
const result = document.createElement("div");
result.classList.add("view__expandMarker");
result.innerText = "...";
return result;
}
/**
* @param {string | {
selected?: boolean;
opened: boolean;
tagName: string;
state: number;
attributes: { name: string, value: string }[];
children: (object | string)[]
}} node
* @param {Function} update
* @returns {HTMLDivElement}
*/
function getNodeView(node, update) {
const result = document.createElement("div");
const title = document.createElement("div");
title.classList.add("view__title");
if (node.selected) {
title.classList.add("view__title-selected");
}
let titleContent;
// if node is text
if (typeof node === "string") {
title.classList.add("view__title-text");
titleContent = document.createTextNode(node);
}
else {// if node is object
titleContent = getTagDescriptionElement(node);
}
title.appendChild(titleContent);
result.appendChild(title);
// if node has children
if (node.children && node.children.length > 0) {
title.classList.add("view__title-expandable");
if (node.opened) {
result.appendChild(getTreeView(node.children, update));
}
else {
title.appendChild(getExpandMarkerElement());
}
title.addEventListener("click", () => {
node.opened = !node.opened;
update();
});
}
return result;
}
/**
* @param {(string | {
selected?: boolean;
opened: boolean;
tagName: string;
state: number;
attributes: { name: string, value: string }[];
children: (object | string)[]
})[]} contentTree
* @param {Function} update
* @returns {HTMLDivElement}
*/
function getTreeView(contentTree, update) {
const result = document.createElement("div");
result.style.marginLeft = 20;
contentTree.forEach(node => {
result.appendChild(getNodeView(node, update));
});
return result;
}
/**
* @param {HTMLDivElement} container
* @param {(string | {
selected?: boolean;
opened: boolean;
tagName: string;
state: number;
attributes: { name: string, value: string }[];
children: (object | string)[]
})[]} contentTree
*/
function updateView(container, contentTree, update) {
if (contentTree.length > 0) {
container.innerHTML = "";
container.appendChild(getTreeView(contentTree, update));
}
}
window.onload = () => {
const textarea = document.getElementById("textarea");
const error = document.getElementById("error");
const view = document.getElementById("view");
const search = document.getElementById("search");
let contentTree = [];
const update = () => {
updateView(view, contentTree, update);
};
search.addEventListener("keyup", () => {
findAllBySelector(contentTree, search.value);
update();
});
textarea.addEventListener("keyup", () => {
error.innerHTML = "";
let visibility = "visible";
try {
contentTree = parseHtml(textarea.value);
update();
if (contentTree.length === 0) {
throw new Error(`Content tree is empty`);
}
}
catch (e) {
visibility = "hidden";
error.innerText = e.message;
}
view.style.visibility = visibility;
search.style.visibility = visibility;
});
};