module Ronin::Support::Encoding::HTML

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

## Features

## Core-Ext Methods

@api public

@since 1.0.0

Public Class Methods

decode(data) click to toggle source

Decoded the HTML encoded data.

@param [String] data

The HTML encoded data to decode.

@return [String]

The decoded String.
# File lib/ronin/support/encoding/html.rb, line 182
def self.decode(data)
  XML.decode(data)
end
encode(data,**kwargs) click to toggle source

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

@param [String] data

The data to HTML encode.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:decimal, :hex] :format (:decimal)

The numeric format for the escaped characters.

@option kwargs [Boolean] :zero_pad

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

@option kwargs [:lower, :upper, nil] :case

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

@return [String]

The HTML encoded String.

@raise [ArgumentError]

The `format:` or `case:` keyword argument is invalid.

@example

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

@example Zero-padding:

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

@example Hexadecimal encoded characters:

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

@example Uppercase hexadecimal encoded characters:

Encoding::HTML.encode("abc\xff", format: :hex, case: :upper)
# => "abcÿ"
# File lib/ronin/support/encoding/html.rb, line 169
def self.encode(data,**kwargs)
  XML.encode(data,**kwargs)
end
encode_byte(byte,**kwargs) click to toggle source

Encodes the byte as a HTML decimal character.

@param [Integer] byte

The byte to HTML encode.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:decimal, :hex] :format (:decimal)

The numeric format for the escaped characters.

@option kwargs [Boolean] :zero_pad

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

@option kwargs [:lower, :upper, nil] :case

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

@return [String]

The HTML decimal character.

@raise [ArgumentError]

The `format:` or `case:` keyword argument is invalid.

@example

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

@example Zero-padding:

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

@example Hexadecimal escaped characters:

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

@example Uppercase hexadecimal escaped characters:

Encoding::HTML.encode_byte(0xff, format: :hex, case: :upper)
# => "ÿ"
# File lib/ronin/support/encoding/html.rb, line 123
def self.encode_byte(byte,**kwargs)
  XML.encode_byte(byte,**kwargs)
end
escape(data,**kwargs) click to toggle source

HTML escapes the data.

@param [String] data

The data to HTML escape.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

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

@return [String]

The HTML escaped String.

@raise [ArgumentError]

The `case:` keyword argument is invalid.

@example

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

@example Uppercase escaped characters:

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

@see rubydoc.info/stdlib/cgi/CGI.escapeHTML

# File lib/ronin/support/encoding/html.rb, line 215
def self.escape(data,**kwargs)
  XML.escape(data,**kwargs)
end
escape_byte(byte,**kwargs) click to toggle source

Escapes the byte as a HTML decimal character.

@param [Integer] byte

The byte to HTML escape.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

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

@return [String]

The HTML decimal character.

@raise [ArgumentError]

The `case:` keyword argument is invalid.

@example

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

@example Uppercase encoding:

Encoding::HTML.escape_byte(0x26, case: :upper)
# => "&"
# File lib/ronin/support/encoding/html.rb, line 77
def self.escape_byte(byte,**kwargs)
  XML.escape_byte(byte,**kwargs)
end
unescape(data) click to toggle source

Unescapes the HTML encoded data.

@param [String] data

The data to HTML unescape.

@return [String]

The unescaped String.

@example

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

@see rubydoc.info/stdlib/cgi/CGI.unescapeHash

# File lib/ronin/support/encoding/html.rb, line 234
def self.unescape(data)
  XML.unescape(data)
end