"use client"; import { useState } from "react"; import { Braces, Copy, Trash2, ArrowUpDown } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Textarea } from "@/components/ui/textarea"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { toast } from "sonner"; export default function HtmlEscapePage() { const [input, setInput] = useState(""); const copyToClipboard = async (text: string) => { if (!text) return; try { await navigator.clipboard.writeText(text); toast.success("已复制到剪贴板"); } catch { toast.error("复制失败"); } }; const escapeHtml = () => { const div = document.createElement('div'); div.textContent = input; setInput(div.innerHTML); toast.success("转义成功"); }; const unescapeHtml = () => { const div = document.createElement('div'); div.innerHTML = input; setInput(div.textContent || ""); toast.success("反转义成功"); }; const clearAll = () => setInput(""); return (
对 HTML 实体字符进行编码和解码转换
HTML 转义是将 HTML 预留字符转换为 HTML 实体。例如,将 `<` 转换为 `<`。
这通常用于在网页上安全地显示代码片段,防止浏览器将其解析为实际的 HTML 标签,或者为了防止跨站脚本(XSS)攻击。
< → <
> → >
& → &
" → "