Search categories, packages and repositories

Package detailsv1.0.0+1

html_character_entities

A utility class containing maps of every HTML 4.01 character entity, including ASCII and hex codes, and methods to encode/decode strings with those character entities.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
43
Likes
21
Pub points
150

Install package

flutter pub add html_character_entities

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

A Flutter/Dart utility package that provides comprehensive mappings of HTML 4.01 character entities, including ASCII and hexadecimal codes. It offers methods to encode and decode strings using these entities, ensuring accurate handling of special characters in web content. Ideal for developers working with HTML text processing, rendering, or data interchange where character encoding precision is critical. The package supports all major platforms and is maintained with clear source code access.

Use cases

  • Rendering HTML content with special characters
  • Converting text between plain and encoded formats
  • Handling legacy HTML data
  • Sanitizing user input for web output
  • Preserving formatting in text exports

Key features

  • Complete HTML 4.01 entity mapping
  • Support for ASCII and hex codes
  • Encode/decode string utilities
  • Cross-platform compatibility
  • Open-source and well-documented
  • Easy integration via pub

Suitable for

  • Web developers using Flutter
  • Content rendering applications
  • Text processing tools
  • Legacy system integrations
  • Educational projects on HTML encoding

Considerations

  • Limited to HTML 4.01 standards
  • No support for HTML5-specific entities
  • Requires manual handling of malformed inputs
  • Not optimized for high-frequency encoding scenarios
  • Depends on external maintenance of entity lists

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

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;).

Categories

Source repository

Projects using this package