カテゴリー・パッケージ・リポジトリを検索

GitHub リポジトリjames-alex/html_character_entities

html_character_entities

HTML 4.01 文字エンティティのすべてのキー/値ペアを含む、ASCIIおよび16進コードを含むDartユーティリティクラスであり、文字列をこれらの文字エンティティにエンコードおよびデコードするためのメソッドを提供します。

html_character_entities のプロジェクト画像
スター
1
フォーク
2
最終プッシュ(UTC)
2023/04/21
プロジェクト状態
公開中

元の README

以下は英語原文のスナップショットです。最新版は GitHub をご覧ください。

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;).
James GitHub avatar
GitHub 作者

James ↗

言語Dart

このリポジトリが公開するパッケージ

使用している依存関係

依存関係一覧 2 件

  • pedantic開発用^1.11.0
  • test開発用^1.16.5

プロジェクトに合うリポジトリを選ぶには?

用途で絞り込み、開発状況を比較し、採用前に元のリポジトリで要件を確認しましょう。

  1. 名前・作者・説明で検索し、技術トピックで絞り込みます。
  2. スター、フォーク、最近のプッシュを比較します。スターは関心の目安であり、品質の保証ではありません。
  3. 利用前に GitHub で README、ライセンス、Issues を確認します。

情報源と管理

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗