html_character_entities 0.1.2+4

  • Readme
  • Changelog
  • Installing
  • 85

html_character_entities #

pub package style: effective dart

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

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

[0.1.2+2] - January 16, 2020 #

  • Formatting changes.

[0.1.2+1] - September 25, 2019 #

  • Strip leading 0s from numeric entities before lookup.

[0.1.2] - August 22, 2019 #

  • Added the [checkAmpsForEntities] option to the [encode] method.

[0.1.1] - August 16, 2019 #

  • Added support for hex code character entities.

  • Added missing ASCII codes for characters with HTML codes.

[0.1.0] - August 1, 2019 #

  • Added the [characterEntities] statically typed map to [HtmlCharacterEntities].

  • Added the [encode] method to [HtmlCharacterEntities].

  • Deprecated the [parse] method in favor of [decode]. [parse] still exists as a wrapper of [decode], but will be removed in a future release.

  • Wrote new tests.

[0.0.1 - 0.0.3] - July 7, 2019 #

  • Changed [RegExpMatch] type declarations to [Match].

  • Commented the [HtmlCharacterEntities] class.

  • Initial release.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  html_character_entities: ^0.1.2+4

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:html_character_entities/html_character_entities.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Apr 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.7

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and html_character_entities.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
effective_dart ^1.2.1
test ^1.6.5