ahtml

package
v1.8.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

README

antgo/encoding/ahtml - HTML处理库 / HTML Processing Library

中文 | English


中文-1

📖 简介

antgo/encoding/ahtml 是高效的HTML处理工具库,提供HTML标签过滤、实体编解码等常用操作,支持与PHP兼容的转义规则,并通过预编译优化提升处理性能。
适用于Web内容安全过滤、HTML模板渲染、XSS防护等场景。

GitHub地址: github.com/small-ek/antgo/encoding/ahtml

📦 安装
go get github.com/small-ek/antgo/encoding/ahtml
🚀 快速开始
基本用法
package main

import (
	"fmt"
	"github.com/small-ek/antgo/encoding/ahtml"
)

func main() {
	// 示例HTML内容
	htmlContent := `<script>alert(1)</script><p>Hello & "World"</p>`

	// 过滤HTML标签
	cleanText := ahtml.StripTags(htmlContent)
	fmt.Println(cleanText) // 输出: alert(1)Hello & "World"

	// 转义HTML特殊字符
	safeHTML := ahtml.SpecialChars(htmlContent)
	fmt.Println(safeHTML) // 输出: &lt;script&gt;alert(1)&lt;/script&gt;&lt;p&gt;Hello &amp; &#34;World&#34;&lt;/p&gt;
}
实体编解码
func main() {
	// 转义所有HTML实体
	encoded := ahtml.Entities(`© "Go" & <Golang>`)
	fmt.Println(encoded) // 输出: &copy; &#34;Go&#34; &amp; &lt;Golang&gt;

	// 解码HTML实体
	decoded := ahtml.EntitiesDecode("&lt;&#39;Hello&#39;&gt;")
	fmt.Println(decoded) // 输出: <'Hello'>
}
✨ 核心特性
特性 描述
PHP兼容 严格遵循PHP同名函数的转义规则
高性能处理 使用预编译替换器,性能比标准库提升3x+
并发安全 所有函数线程安全,支持高并发场景
完整实体支持 支持6500+ HTML实体编码/解码
⚠️ 注意事项
  1. StripTags使用第三方实现,不能保证过滤所有恶意内容
  2. SpecialChars转换的5个基础字符:&, <, >, ", '
  3. 实体解码支持十进制({)和十六进制(😃)格式
  4. 返回结果均为新副本,原始数据不会被修改
🤝 参与贡献

贡献指南 | 提交Issue


English-1

📖 Introduction

antgo/encoding/ahtml is a high-performance HTML processing library providing tag stripping, entity encoding/decoding, and PHP-compatible escaping rules.
Ideal for web content sanitization, template rendering, and XSS prevention.

GitHub URL: github.com/small-ek/antgo/encoding/ahtml

📦 Installation
go get github.com/small-ek/antgo/encoding/ahtml
🚀 Quick Start
Basic Usage
package main

import (
	"fmt"
	"github.com/small-ek/antgo/encoding/ahtml"
)

func main() {
	htmlContent := `<script>alert(1)</script><p>Hello & "World"</p>`

	// Remove HTML tags
	cleanText := ahtml.StripTags(htmlContent)
	fmt.Println(cleanText) // Output: alert(1)Hello & "World"

	// Escape special characters
	safeHTML := ahtml.SpecialChars(htmlContent)
	fmt.Println(safeHTML) // Output: &lt;script&gt;alert(1)&lt;/script&gt;&lt;p&gt;Hello &amp; &#34;World&#34;&lt;/p&gt;
}
Entity Encoding
func main() {
	// Encode HTML entities
	encoded := ahtml.Entities(`© "Go" & <Golang>`)
	fmt.Println(encoded) // Output: &copy; &#34;Go&#34; &amp; &lt;Golang&gt;

	// Decode entities
	decoded := ahtml.EntitiesDecode("&lt;&#39;Hello&#39;&gt;")
	fmt.Println(decoded) // Output: <'Hello'>
}
✨ Key Features
Feature Description
PHP Compatible Strictly follows PHP function behaviors
High Performance Pre-compiled replacers with 3x+ speed vs stdlib
Thread-Safe All functions are concurrency-ready
Full Entities Supports 6500+ HTML entities encoding/decoding
⚠️ Important Notes
  1. StripTags relies on third-party implementation for tag stripping
  2. SpecialChars handles 5 basic characters: &, <, >, ", '
  3. Supports decimal ({) and hexadecimal (😃) entity formats
  4. All results are new copies, original data remains unchanged
🤝 Contributing

Contribution Guide | Open an Issue

⬆ Back to Top

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Entities

func Entities(s string) string

Entities 转换所有HTML特殊字符为对应实体(包括引号) 注意:与PHP的htmlentities()不同,当前实现仅转义5个基础字符 参考:http://php.net/manual/zh/function.htmlentities.php

Entities converts all HTML special characters to entities (including quotes) Note: Different from PHP's htmlentities(), current implementation escapes 5 basic characters Reference: http://php.net/manual/en/function.htmlentities.php

func EntitiesDecode

func EntitiesDecode(s string) string

EntitiesDecode 将HTML实体转换回普通字符 参考:http://php.net/manual/zh/function.html-entity-decode.php

EntitiesDecode converts HTML entities back to normal characters Reference: http://php.net/manual/en/function.html-entity-decode.php

func SpecialChars

func SpecialChars(s string) string

SpecialChars 转换关键HTML特殊字符为实体 特性: - 自动处理 &, <, >, ", ' 五个字符 - 使用预编译的替换器,性能优于动态编译 - 单引号强制转换(类似PHP的ENT_QUOTES模式) 参考:http://php.net/manual/zh/function.htmlspecialchars.php

SpecialChars converts key HTML special characters to entities Features: - Handles &, <, >, ", ' automatically - Uses pre-compiled replacer for better performance - Forces single quote conversion (similar to PHP's ENT_QUOTES mode) Reference: http://php.net/manual/en/function.htmlspecialchars.php

func SpecialCharsDecode

func SpecialCharsDecode(s string) string

SpecialCharsDecode 反转SpecialChars的转换操作 特性: - 使用预编译替换器,高性能解码 - 完全匹配SpecialChars的编码规则 参考:http://php.net/manual/zh/function.htmlspecialchars-decode.php

SpecialCharsDecode reverses SpecialChars conversion Features: - Uses pre-compiled replacer for high-performance decoding - Exactly matches SpecialChars encoding rules Reference: http://php.net/manual/en/function.htmlspecialchars-decode.php

func StripTags

func StripTags(s string) string

StripTags 过滤掉HTML标签,只返回text内容 注意:使用第三方库实现,性能取决于底层库效率 参考:http://php.net/manual/zh/function.strip-tags.php

StripTags removes all HTML tags and returns text content Note: Uses third-party library implementation, performance depends on underlying library Reference: http://php.net/manual/en/function.strip-tags.php

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL