Module: Ronin::Support::Encoding::HTML

Defined in:
lib/ronin/support/encoding/html.rb

Overview

Contains methods for encoding/decoding escaping/unescaping HTML data.

Features

  • Supports lowercase (ex: &) and uppercase (ex: &) encoding.
  • Supports decimal (ex: A) and hexadecimal (ex: A) character encoding.
  • Supports zero-padding (ex: A).

Core-Ext Methods

Since:

  • 1.0.0

Class Method Summary collapse

Class Method Details

.decode(data) ⇒ String

Decoded the HTML encoded data.

Parameters:

  • data (String)

    The HTML encoded data to decode.

Returns:

  • (String)

    The decoded String.

Since:

  • 1.0.0



182
183
184
# File 'lib/ronin/support/encoding/html.rb', line 182

def self.decode(data)
  XML.decode(data)
end

.encode(data, **kwargs) ⇒ String

Encodes each character in the given data as an HTML character.

Examples:

Encoding::HTML.encode("abc")
# => "abc"

Zero-padding:

Encoding::HTML.encode("abc", zero_pad: true)
# => "abc"

Hexadecimal encoded characters:

Encoding::HTML.encode("abc", format: :hex)
# => "abc"

Uppercase hexadecimal encoded characters:

Encoding::HTML.encode("abc\xff", format: :hex, case: :upper)
# => "abcÿ"

Parameters:

  • data (String)

    The data to HTML encode.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :format (:decimal, :hex) — default: :decimal

    The numeric format for the escaped characters.

  • :zero_pad (Boolean)

    Controls whether the escaped characters will be left-padded with up to seven 0 characters.

  • :case (:lower, :upper, nil)

    Controls whether to output lowercase or uppercase XML special characters. Defaults to lowercase hexadecimal.

Returns:

  • (String)

    The HTML encoded String.

Raises:

  • (ArgumentError)

    The format: or case: keyword argument is invalid.

Since:

  • 1.0.0



169
170
171
# File 'lib/ronin/support/encoding/html.rb', line 169

def self.encode(data,**kwargs)
  XML.encode(data,**kwargs)
end

.encode_byte(byte, **kwargs) ⇒ String

Encodes the byte as a HTML decimal character.

Examples:

Encoding::HTML.encode_byte(0x41)
# => "A"

Zero-padding:

Encoding::HTML.encode_byte(0x41, zero_pad: true)
# => "A"

Hexadecimal escaped characters:

Encoding::HTML.encode_byte(0x41, format: :hex)
# => "A"

Uppercase hexadecimal escaped characters:

Encoding::HTML.encode_byte(0xff, format: :hex, case: :upper)
# => "ÿ"

Parameters:

  • byte (Integer)

    The byte to HTML encode.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :format (:decimal, :hex) — default: :decimal

    The numeric format for the escaped characters.

  • :zero_pad (Boolean)

    Controls whether the escaped characters will be left-padded with up to seven 0 characters.

  • :case (:lower, :upper, nil)

    Controls whether to output lowercase or uppercase XML special characters. Defaults to lowercase hexadecimal.

Returns:

  • (String)

    The HTML decimal character.

Raises:

  • (ArgumentError)

    The format: or case: keyword argument is invalid.

Since:

  • 1.0.0



123
124
125
# File 'lib/ronin/support/encoding/html.rb', line 123

def self.encode_byte(byte,**kwargs)
  XML.encode_byte(byte,**kwargs)
end

.escape(data, **kwargs) ⇒ String

HTML escapes the data.

Examples:

Encoding::HTML.escape("one & two")
# => "one & two"

Uppercase escaped characters:

Encoding::HTML.encode("one & two", case: :upper)
# => "one & two"

Parameters:

  • data (String)

    The data to HTML escape.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

    Controls whether to output lowercase or uppercase XML special characters. Defaults to lowercase hexadecimal.

Returns:

  • (String)

    The HTML escaped String.

Raises:

  • (ArgumentError)

    The case: keyword argument is invalid.

See Also:

Since:

  • 1.0.0



215
216
217
# File 'lib/ronin/support/encoding/html.rb', line 215

def self.escape(data,**kwargs)
  XML.escape(data,**kwargs)
end

.escape_byte(byte, **kwargs) ⇒ String

Escapes the byte as a HTML decimal character.

Examples:

Encoding::HTML.escape_byte(0x41)
# => "A"
Encoding::HTML.escape_byte(0x26)
# => "&"

Uppercase encoding:

Encoding::HTML.escape_byte(0x26, case: :upper)
# => "&"

Parameters:

  • byte (Integer)

    The byte to HTML escape.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

    Controls whether to output lowercase or uppercase XML special characters. Defaults to lowercase hexadecimal.

Returns:

  • (String)

    The HTML decimal character.

Raises:

  • (ArgumentError)

    The case: keyword argument is invalid.

Since:

  • 1.0.0



77
78
79
# File 'lib/ronin/support/encoding/html.rb', line 77

def self.escape_byte(byte,**kwargs)
  XML.escape_byte(byte,**kwargs)
end

.unescape(data) ⇒ String

Unescapes the HTML encoded data.

Examples:

Encoding::HTML.unescape("<p>one <span>two</span></p>")
# => "<p>one <span>two</span></p>"

Parameters:

  • data (String)

    The data to HTML unescape.

Returns:

  • (String)

    The unescaped String.

See Also:

Since:

  • 1.0.0



234
235
236
# File 'lib/ronin/support/encoding/html.rb', line 234

def self.unescape(data)
  XML.unescape(data)
end