搜尋分類、套件與程式碼儲存庫

套件詳情v1.0.0+1

html_character_entities

一個工具類別,包含 HTML 4.01 中每個字元實體的映射,包括 ASCII 和十六進位代碼,以及用這些字元實體編碼/解碼字串的方法。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
43
喜歡數
21
Pub 分數
150

安裝套件

flutter pub add html_character_entities

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Flutter/Dart 工具套件,提供 HTML 4.01 字元實體的完整對應,包含 ASCII 和十六進位代碼。它提供方法以這些實體編碼和解碼字串,確保在網頁內容中準確處理特殊字元。適合從事 HTML 文字處理、呈現或資料交換且需精確字元編碼的開發者。此套件支援所有主要平台,並提供清楚的原始碼存取。

適用情境

  • 使用特殊字元渲染HTML內容
  • 在純文字和編碼格式之間轉換文字
  • 處理遺留的HTML資料
  • 對Web輸出的使用者輸入進行清理
  • 保留文字匯出中的格式

主要特色

  • 完整的 HTML 4.01 实體映射
  • 支援 ASCII 與十六進制代碼
  • 編碼/解碼字串工具
  • 跨平台相容性
  • 開源且文件完整
  • 透過 pub 實現輕鬆整合

適合對象

  • 使用 Flutter 的 Web 開發人員
  • 內容渲染應用
  • 文字處理工具
  • 舊系統整合
  • HTML 編碼的教育專案

使用建議

  • 僅限 HTML 4.01 標準
  • 不支援 HTML5 特定實體
  • 需要手動處理格式錯誤的輸入
  • 未針對高頻編碼場景進行優化
  • 依賴外部對實體列表的維護

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

html_character_entities

A Dart utility class containing maps of key/value pairs for every HTML 4.01 character entity, including ASCII and hex codes, and methods to encode and decode strings to and from those character entities.

Usage

import 'package:html_character_entities/html_character_entities.dart';

Decoding Strings

The decode method accepts a string as an argument and returns it after replacing every character entity with their corresponding characters.

String string = 'An ampersand can be written as & and &.';

print(HtmlCharacterEntities.decode(string)); // An ampersand can be written as & and &.

Encoding Strings

The encode() method has an argument, [characters], that accepts a string of all characters that should be encoded. It defaults to the 5 reserved characters in both HTML and XML: less-than (<), greater-than (>), ampersand (&), apostrophe or single quote ('), and double-quote (").

String string = '<, >, &, \', and " aren\'t safe to use in HTML and XML.';

print(HtmlCharacterEntities.encode(string));
// &lt;, &gt;, &amp;, &apos;, and &quot; aren&apos;t safe to use in HTML and XML.

print(HtmlCharacterEntities.encode(string, characters: 'XHTML'));
// <, >, &, ', and " aren't safe to use in &#72;&#84;&#77;&#76; and &#88;&#77;&#76;.

If [characters] is null, every character in the HtmlCharacterEntities.characters map will be encoded.

Maps

A map of every HTML 4.01 character code, as well as their ASCII and hex code entities can be referenced from HtmlCharacterEntities.characters.

Note: The hex codes in the map don't have leading 0s. I.e. the hex code &#x0000a5; is in the map as &#xa5;. When decoding hex code entities, the leading 0s will be removed by the parser before being looked up.

Map<String, String> c = HtmlCharacterEntities.characters;

String string = 'There are 4 suites of cards: ${c['&spades;']} Spades, ${c['&clubs;']} Clubs, ${c['&hearts;']} Hearts, ${c['&diams;']} Diamonds';

print(string); // There are 4 suites of cards: ♠ Spades, ♣ Clubs, ♥ Hearts, ♦ Diamonds

Maps containing the characters with their respective character codes as values can also be referenced. They are used by the encode method.

// A map with only ASCII code character entities as values.
Map<String, String> ascii = HtmlCharacterEntities.asciiCodes;

// A map with all non-ASCII code character entities as values.
Map<String, String> entity = HtmlCharacterEntities.entities;

String string = '< and > exist as both ASCII codes (${ascii['<']} and ${ascii['>']}) and character entities (${entity['<']} and ${entity['>']}).';

print(string); // < and > exist as both ASCII codes (&#60; and &#62;) and character entities (&lt; and &gt;).

所屬分類

原始碼儲存庫

使用此套件的專案