一个轻量级的 Flutter/Dart 包,用于解码 HTML 实体,包括命名实体、十进制和十六进制字符引用。它可安全地将转义的 HTML 文本转换回原始形式,非常适合用于渲染来自网络源或用户输入的内容。该库高效、简洁,专为在多个平台上的 Dart 应用中使用而设计。
适合场景
在UI中渲染HTML编码的文本
处理用户生成的内容
从网络API解析数据
从CMS显示格式化文本
在显示前对输入进行清理
主要特点
支持所有 HTML 实体类型
轻量且快速
跨平台兼容性
简单 API,方法清晰
无外部依赖
适合对象
处理原始 HTML 字符串的应用程序
内容渲染应用
Web 抓取工具
文本处理工具
教育类或文档类应用
使用建议
不解析完整的HTML文档
不适合用于XSS防护
需要手动清理未转义的内容
限制字符引用解码
不适用于复杂的HTML操作
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
html_unescape
A Dart library for unescaping HTML-encoded strings.
Supports:
Named Character References ( )
2099 of them
Decimal Character References (á)
Hexadecimal Character References (ã)
The idea is that while you seldom need encoding to such a level (most of the
time, all you need to escape is <, >, /, & and "), you do want to
make sure that you cover the whole spectrum when decoding from HTML-escaped
strings.
import'package:html_unescape/html_unescape.dart';
main() {
var unescape = HtmlUnescape();
var text = unescape.convert("<strong>This "escaped" string");
print(text);
}
You can also use the converter to transform a stream. For example, the code
below will transform a POSIX stdin into an HTML-unencoded stdout.
If you're sure you will only encounter the most common escaped characters,
you can import 'package:html_unescape/html_unescape_small.dart' instead of
the full version. This will decrease code size and increase performance. The
only difference is in the size of the Named Character Reference dictionary.
The full set includes the likes of ⥐ or ⤒
while the small set only includes the first 255 charcodes.