Class: HTMLEntities

Class | HTMLEntities
In: | lib/htmlentities.rb lib/htmlentities/html4.rb lib/htmlentities/legacy.rb lib/htmlentities/xhtml1.rb
Parent: | Object

HTML entity encoding and decoding for Ruby

### Methods

decode decode_entities encode encode_entities new

### Classes and Modules

Class HTMLEntities::InstructionError
Class HTMLEntities::UnknownFlavor

### Constants

VERSION | = | '4.0.0'
FLAVORS | = | %w[html4 xhtml1]
INSTRUCTIONS | = | [:basic, :named, :decimal, :hexadecimal]
MAPPINGS | = | {} unless defined? MAPPINGS
MAPPINGS | = | {} unless defined? MAPPINGS

### Public Class methods

decode_entities(*args)

Legacy compatibility class method allowing direct decoding of XHTML1 entities. See HTMLEntities#decode for description of parameters.

encode_entities(*args)

Legacy compatibility class method allowing direct encoding of XHTML1 entities. See HTMLEntities#encode for description of parameters.

new(flavor='xhtml1')

Create a new HTMLEntities coder for the specified flavor. Available flavors are ‘html4’ and ‘xhtml1’ (the default). The only difference in functionality between the two is in the handling of the apos (apostrophe) named entity, which is not defined in HTML4.

### Public Instance methods

decode(source)

Decode 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 will not be converted

encode(source, *instructions)

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.

Examples:

      encode_entities(str) - XML-safe
      encode_entities(str, :basic, :decimal) - XML-safe and 7-bit clean
      encode_entities(str, :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 source contains valid UTF-8 before calling this method.

[Validate]