Skip to content

Commit fd9171b

Browse files
committed
Add XML Formatter component and update styles #282
- Implemented XmlFormatter component for formatting XML input. - Updated index.css to enhance textarea and input styles. - Registered XmlFormatter in DevTools and marked it as available in the DevArea.
1 parent 1e4ca0c commit fd9171b

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import React, { useState } from "react";
2+
// import { button } from "@/components/ui/button";
3+
// import { textarea } from "@/components/ui/textarea";
4+
5+
const XmlFormatter = () => {
6+
const [input, setInput] = useState("");
7+
const [output, setOutput] = useState("");
8+
9+
const formatXml = (xml) => {
10+
try {
11+
const PADDING = " ";
12+
const reg = /(>)(<)(\/*)/g;
13+
let formatted = "";
14+
let pad = 0;
15+
16+
xml = xml.replace(reg, "$1\r\n$2$3");
17+
xml.split("\r\n").forEach((node) => {
18+
let indent = 0;
19+
if (node.match(/.+<\/\w[^>]*>$/)) indent = 0;
20+
else if (node.match(/^<\/\w/)) {
21+
if (pad !== 0) pad -= 1;
22+
} else if (node.match(/^<\w[^>]*[^\/]>.*$/)) indent = 1;
23+
formatted += PADDING.repeat(pad) + node + "\r\n";
24+
pad += indent;
25+
});
26+
return formatted.trim();
27+
} catch {
28+
return "Invalid XML input.";
29+
}
30+
};
31+
32+
const handleFormat = () => {
33+
try {
34+
const parser = new DOMParser();
35+
const xmlDoc = parser.parseFromString(input, "application/xml");
36+
const parseError = xmlDoc.getElementsByTagName("parsererror");
37+
if (parseError.length) throw new Error("Invalid XML");
38+
setOutput(formatXml(input));
39+
} catch {
40+
setOutput("❌ Invalid XML structure");
41+
}
42+
};
43+
44+
const handleClear = () => {
45+
setInput("");
46+
setOutput("");
47+
};
48+
49+
return (
50+
<div className="flex flex-col gap-4 w-full max-w-2xl mx-auto">
51+
<h2 className="text-xl font-semibold text-slate-800">XML Formatter</h2>
52+
<textarea
53+
placeholder="Paste your XML here..."
54+
value={input}
55+
onChange={(e) => setInput(e.target.value)}
56+
className="resize-none border border-slate-300"
57+
rows={6}
58+
/>
59+
60+
<div className="flex gap-2">
61+
<button onClick={handleFormat}>Format</button>
62+
<button onClick={handleClear} variant="outline">Clear</button>
63+
</div>
64+
65+
<textarea
66+
placeholder="Formatted XML output..."
67+
value={output}
68+
readOnly
69+
className="resize-none border border-slate-300 font-mono text-sm"
70+
rows={8}
71+
/>
72+
</div>
73+
);
74+
};
75+
76+
export default XmlFormatter;

src/index.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,4 +597,19 @@ html {
597597
margin-top: 5px;
598598
padding: 5px;
599599
background-color: rgba(255, 255, 255, 0.2);
600+
}
601+
602+
textarea, input {
603+
background: #010201;
604+
border: none;
605+
width: 100%;
606+
border-radius: 10px;
607+
border: 1px solid #ffffff40 !important;
608+
color: white;
609+
padding-inline: 20px;
610+
padding-top: 10px;
611+
font-size: 18px;
612+
}
613+
textarea:focus, input:focus {
614+
outline: none;
600615
}

src/pages/DevArea/DevTools.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import MarkDownEditor from "../../components/DevAreaTools/MarkDownEditor";
44
import JSONFormatter from "../../components/DevAreaTools/JSONFormatter";
55
import JWTDecoder from "../../components/DevAreaTools/JwtDecoder";
66
import URLEncoderDecoder from "../../components/DevAreaTools/URLEncoderDecoder";
7+
import XmlFormatter from "../../components/DevAreaTools/XmlFormatter";
78

89
const DevTools = () => {
910
const { tool } = useParams();
@@ -13,6 +14,7 @@ const DevTools = () => {
1314
jwtdecoder: <JWTDecoder />,
1415
"json-formatter": <JSONFormatter />,
1516
"url-encoder-decoder": <URLEncoderDecoder />,
17+
"xml-formatter": <XmlFormatter />,
1618
}
1719

1820

src/pages/DevArea/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const DevArea = () => {
3434
{
3535
name: "XML Formatter",
3636
link: "/devarea/xml-formatter",
37-
isAvailable: false
37+
isAvailable: true
3838
},
3939
{
4040
name: "YAML Formatter",

0 commit comments

Comments
 (0)