搜索分类、插件与代码仓

插件详情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;).

所属分类

源码仓库

使用此插件的项目