-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
277 lines (250 loc) · 8.42 KB
/
index.html
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Interactive DOM Tree Visualizer for HTML Structures</title>
<meta
name="description"
content="Explore and visualize the DOM tree structure of your HTML input with this interactive DOM Visualizer. Paste your HTML and see the tree representation."
/>
<script
src="https://cdn.usefathom.com/script.js"
data-site="XKKLLWKP"
defer
></script>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
margin: 0;
padding: 20px;
background-color: #f9f9f9;
}
header {
text-align: center;
margin-bottom: 20px;
}
header h1 {
margin: 0;
font-size: 2em;
}
header p {
margin: 0;
font-size: 1.2em;
color: #666;
}
#inputArea {
width: 90%;
max-width: 800px;
margin-bottom: 20px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
font-family: monospace;
font-size: 1em;
resize: vertical;
}
#svgCanvas {
width: 100%;
height: 60vh;
min-height: 400px;
max-height: calc(100vh - 160px);
border: 1px solid #ccc;
border-radius: 5px;
background-color: #fff;
}
.error {
color: red;
margin-bottom: 10px;
}
</style>
<meta
property="og:title"
content="Interactive DOM Tree Visualizer for HTML Structures"
/>
<meta
property="og:description"
content="Explore and visualize the DOM tree structure of your HTML input with this interactive DOM Visualizer. Paste your HTML and see the tree representation."
/>
<meta property="og:type" content="website" />
<meta property="og:url" content="http://example.com" />
<meta property="og:image" content="http://example.com/image.jpg" />
<meta name="twitter:card" content="summary_large_image" />
<meta
name="twitter:title"
content="Interactive DOM Tree Visualizer for HTML Structures"
/>
<meta
name="twitter:description"
content="Explore and visualize the DOM tree structure of your HTML input with this interactive DOM Visualizer. Paste your HTML and see the tree representation."
/>
<meta name="twitter:image" content="http://example.com/image.jpg" />
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "WebApplication",
"name": "DOM Visualizer",
"description": "Explore and visualize the DOM tree structure of your HTML input.",
"url": "http://example.com",
"author": {
"@type": "Person",
"name": "Your Name"
},
"applicationCategory": "WebApplication"
}
</script>
</head>
<body>
<header>
<h1>Interactive DOM Visualizer</h1>
<p>Visualize the DOM tree structure from your HTML input</p>
</header>
<textarea
placeholder="Paste in some HTML here..."
id="inputArea"
rows="10"
aria-label="HTML Input Area"
>
<div class="container">
<p>Hello</p>
<p class="highlight">World</p>
</div>
</textarea>
<div id="error" class="error" role="alert"></div>
<svg id="svgCanvas" role="img" aria-label="DOM Tree Visualization"></svg>
<script src="https://d3js.org/d3.v6.min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", () => {
const inputArea = document.getElementById("inputArea");
const svgCanvas = d3.select("#svgCanvas");
const errorDiv = document.getElementById("error");
inputArea.addEventListener("input", updateTree);
window.addEventListener("resize", updateTree);
const svgGroup = svgCanvas.append("g");
const zoom = d3
.zoom()
.scaleExtent([0.1, 3])
.on("zoom", (event) => {
svgGroup.attr("transform", event.transform);
});
svgCanvas.call(zoom);
let i = 0;
function updateTree() {
const html = inputArea.value.trim();
if (!html) {
clearSvg();
return;
}
try {
const parser = new DOMParser();
const doc = parser.parseFromString(html, "text/html");
if (doc.body.children.length === 0) {
throw new Error("No valid HTML content.");
}
const treeData = convertToTreeData(doc.body);
renderTree(treeData);
errorDiv.textContent = "";
} catch (error) {
errorDiv.textContent = `Error: ${error.message}`;
clearSvg();
}
}
function convertToTreeData(element) {
const node = {
name: `${element.tagName.toLowerCase()}${
element.id ? `#${element.id}` : ""
}${element.className ? `.${element.className}` : ""}`,
children: [],
};
Array.from(element.children).forEach((child) => {
node.children.push(convertToTreeData(child));
});
return node;
}
function renderTree(data) {
const margin = { top: 20, right: 90, bottom: 30, left: 90 };
const width =
svgCanvas.node().clientWidth - margin.left - margin.right;
const height =
svgCanvas.node().clientHeight - margin.top - margin.bottom;
const treemap = d3.tree().size([height, width]);
const root = d3.hierarchy(data, (d) => d.children);
root.x0 = height / 2;
root.y0 = 0;
const treeData = treemap(root);
const nodes = treeData.descendants();
const links = treeData.descendants().slice(1);
nodes.forEach((d) => {
d.y = d.depth * 180;
});
svgGroup.selectAll("*").remove();
const link = svgGroup
.selectAll("path.link")
.data(links, (d) => d.id)
.enter()
.append("path")
.attr("class", "link")
.attr("d", (d) => {
return `M${d.y},${d.x}
C${(d.y + d.parent.y) / 2},${d.x}
${(d.y + d.parent.y) / 2},${d.parent.x}
${d.parent.y},${d.parent.x}`;
})
.style("fill", "none")
.style("stroke", "#ccc")
.style("stroke-width", "2px");
const node = svgGroup
.selectAll("g.node")
.data(nodes, (d) => d.id || (d.id = ++i))
.enter()
.append("g")
.attr("class", "node")
.attr("transform", (d) => `translate(${d.y},${d.x})`);
node
.append("circle")
.attr("r", 10)
.style("fill", (d) =>
d.data.name.includes(".highlight") ? "yellow" : "#fff"
)
.style("stroke", "steelblue")
.style("stroke-width", "3px");
node
.append("text")
.attr("dy", ".35em")
.attr("x", (d) => (d.children ? -13 : 13))
.attr("text-anchor", (d) => (d.children ? "end" : "start"))
.text((d) => d.data.name)
.style("font", "12px sans-serif");
adjustInitialZoom(root);
}
function adjustInitialZoom(root) {
const nodes = root.descendants();
const minX = d3.min(nodes, (d) => d.x);
const maxX = d3.max(nodes, (d) => d.x);
const minY = d3.min(nodes, (d) => d.y);
const maxY = d3.max(nodes, (d) => d.y);
const width = svgCanvas.node().clientWidth;
const height = svgCanvas.node().clientHeight;
const scaleX = width / (maxY - minY + 40);
const scaleY = height / (maxX - minX + 40);
const scale = Math.min(scaleX, scaleY);
const translateX = (width - (maxY + minY) * scale) / 2;
const translateY = (height - (maxX + minX) * scale) / 2;
svgCanvas
.transition()
.call(
zoom.transform,
d3.zoomIdentity.translate(translateX, translateY).scale(scale)
);
}
function clearSvg() {
svgGroup.selectAll("*").remove();
}
updateTree();
});
</script>
</body>
</html>