一個輕量級的 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.