This commit is contained in:
star7th
2025-04-23 20:28:47 +08:00
parent 260536c962
commit cc5c5f7028
3 changed files with 44 additions and 13 deletions
+34 -7
View File
@@ -36,7 +36,8 @@ export default function EncodingConverter() {
{ id: 'base64', name: 'Base64', description: t('tools.encoding_converter.base64_desc') },
{ id: 'url', name: 'URL', description: t('tools.encoding_converter.url_desc') },
{ id: 'unicode', name: 'Unicode', description: t('tools.encoding_converter.unicode_desc') },
{ id: 'html', name: 'HTML', description: t('tools.encoding_converter.html_desc') }
{ id: 'html_entity', name: 'HTML实体编码', description: t('tools.encoding_converter.html_entity_desc') },
{ id: 'html_escape', name: 'HTML转义', description: t('tools.encoding_converter.html_escape_desc') }
];
// 状态管理
@@ -97,7 +98,24 @@ export default function EncodingConverter() {
);
}
}
else if (type === 'html') {
else if (type === 'html_entity') {
if (op === 'encode') {
// 将文本转换为HTML十六进制实体格式 (汉)
return Array.from(text)
.map(char => {
const codePoint = char.codePointAt(0) as number;
return '&#x' + codePoint.toString(16).toLowerCase() + ';';
})
.join('');
} else {
// 将HTML十六进制实体格式转换回文本
// 正则表达式匹配 &#xXXXX; 格式的十六进制值
return text.replace(/&#x([0-9a-fA-F]+);/g, (_, hex) =>
String.fromCodePoint(parseInt(hex, 16))
);
}
}
else if (type === 'html_escape') {
if (op === 'encode') {
const el = document.createElement('div');
el.textContent = text;
@@ -117,8 +135,10 @@ export default function EncodingConverter() {
throw new Error(t('tools.encoding_converter.invalid_url'));
} else if (type === 'unicode' && op === 'decode') {
throw new Error(t('tools.encoding_converter.invalid_unicode'));
} else if (type === 'html' && op === 'decode') {
throw new Error(t('tools.encoding_converter.invalid_html'));
} else if (type === 'html_entity' && op === 'decode') {
throw new Error(t('tools.encoding_converter.invalid_html_entity'));
} else if (type === 'html_escape' && op === 'decode') {
throw new Error(t('tools.encoding_converter.invalid_html_escape'));
}
throw err;
}
@@ -180,7 +200,11 @@ export default function EncodingConverter() {
encode: t('tools.encoding_converter.example_text'),
decode: '\\u4f60\\u597d\\uff0c\\u4e16\\u754c\\uff01'
},
html: {
html_entity: {
encode: t('tools.encoding_converter.example_text'),
decode: '你好,世界!'
},
html_escape: {
encode: '<div class="example">' + t('tools.encoding_converter.html_example') + '</div>',
decode: '&lt;div class=&quot;example&quot;&gt;HTML示例 &amp; 特殊字符&lt;/div&gt;'
}
@@ -334,8 +358,11 @@ export default function EncodingConverter() {
{encodingType === 'unicode' && (
"Unicode编码使用\\u前缀后跟四位十六进制数字表示字符,可以表示几乎所有语言的字符,如中文、日文等。"
)}
{encodingType === 'html' && (
"HTML编码将特殊字符(如<、>、&等)转换为HTML实体,以防止它们被浏览器解释为HTML标签或特殊结构。"
{encodingType === 'html_entity' && (
"HTML实体编码使用十六进制实体格式(如&#x6C49;)表示Unicode字符,每个字符转换为对应的码点十六进制值。"
)}
{encodingType === 'html_escape' && (
"HTML转义将特殊字符(如<、>、&等)转换为HTML实体,以防止它们被浏览器解释为HTML标签或特殊结构。"
)}
</p>
</div>
@@ -1,10 +1,11 @@
export const encodingConverterEn = {
title: 'Encoding Converter',
description: 'Base64/URL/Unicode encoding and decoding tool',
description: 'Base64/URL/Unicode/HTML Entity/HTML Escape encoding and decoding tool',
base64_desc: 'Encode text to Base64 string or decode Base64 string to text',
url_desc: 'Encode or decode special characters in URLs',
unicode_desc: 'Convert text to Unicode encoding or Unicode encoding to text',
html_desc: 'Encode or decode special characters in HTML',
html_entity_desc: 'Convert text to HTML hexadecimal entities or HTML hexadecimal entities to text',
html_escape_desc: 'Escape or unescape special characters in HTML',
encode: 'Encode',
decode: 'Decode',
load_example: 'Load Example',
@@ -25,7 +26,8 @@ export const encodingConverterEn = {
invalid_base64: 'Invalid Base64 encoded string',
invalid_url: 'Invalid URL encoded string',
invalid_unicode: 'Invalid Unicode encoded string',
invalid_html: 'Invalid HTML encoded string',
invalid_html_entity: 'Invalid HTML entity encoded string',
invalid_html_escape: 'Invalid HTML escaped string',
copy_failed: 'Copy failed:',
clipboard_error: 'Failed to copy to clipboard'
};
@@ -1,10 +1,11 @@
export const encodingConverterZh = {
title: '编码转换工具',
description: 'Base64/URL/Unicode编解码转换工具',
description: 'Base64/URL/Unicode/HTML实体/HTML转义编解码转换工具',
base64_desc: '将文本编码为Base64字符串或将Base64字符串解码为文本',
url_desc: '对URL中的特殊字符进行编码或解码',
unicode_desc: '将文本转换为Unicode编码或将Unicode编码转换为文本',
html_desc: '对HTML中的特殊字符进行编码或解码',
html_entity_desc: '将文本转换为HTML十六进制实体格式或将HTML十六进制实体转换为文本',
html_escape_desc: '对HTML中的特殊字符进行转义或反转义',
encode: '编码',
decode: '解码',
load_example: '加载示例',
@@ -25,7 +26,8 @@ export const encodingConverterZh = {
invalid_base64: '无效的Base64编码字符串',
invalid_url: '无效的URL编码字符串',
invalid_unicode: '无效的Unicode编码字符串',
invalid_html: '无效的HTML编码字符串',
invalid_html_entity: '无效的HTML实体编码字符串',
invalid_html_escape: '无效的HTML转义字符串',
copy_failed: '复制失败:',
clipboard_error: '复制到剪贴板失败'
};