Class String
In: lib/htmlentities.rb
Parent: Object



ENCODE_ENTITIES_COMMAND_ORDER = { :basic => 0, :named => 1, :decimal => 2, :hexadecimal => 3   Because there’s no need to make the user worry about the order here, let’s handle it.

Public Instance methods

Decode XML and HTML 4.01 entities in a string into their UTF-8 equivalents. Obviously, if your string is not already in UTF-8, you’d better convert it before using this method, or the output will be mixed up. Unknown named entities are not converted

Encode codepoints into their corresponding entities. Various operations are possible, and may be specified in order:

:basic :Convert the five XML entities (’"<>&)
:named :Convert non-ASCII characters to their named HTML 4.01 equivalent
:decimal :Convert non-ASCII characters to decimal entities (e.g. &1234;)
:hexadecimal :Convert non-ASCII characters to hexadecimal entities (e.g. # &x12ab;)

You can specify the commands in any order, but they will be executed in the order listed above to ensure that entity ampersands are not clobbered and that named entities are replaced before numeric ones.

If no instructions are specified, :basic will be used.


  str.encode_entities - XML-safe
  str.encode_entities(:basic, :decimal) - XML-safe and 7-bit clean
  str.encode_entities(:basic, :named, :decimal) - 7-bit clean, with all
  non-ASCII characters replaced with their named entity where possible, and
  decimal equivalents otherwise.

Note: It is the program’s responsibility to ensure that the string contains valid UTF-8 before calling this method.