lib/ronin/support/encoding/xml/core_ext/string.rb
# frozen_string_literal: true
#
# Copyright (c) 2006-2023 Hal Brodigan (postmodern.mod3 at gmail.com)
#
# Ronin Support is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ronin Support is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Ronin Support. If not, see <http://www.gnu.org/licenses/>.
#
require 'ronin/support/encoding/xml'
class String
#
# XML escapes the String.
#
# @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 XML escaped String.
#
# @example
# "one & two".xml_escape
# # => "one & two"
#
# @example Uppercase escaped characters:
# "one & two".xml_escape(case: :upper)
# # => "one & two"
#
# @see http://rubydoc.info/stdlib/cgi/CGI.escapeHTML
# @see Ronin::Support::Encoding::XML.escape
#
# @since 0.2.0
#
# @api public
#
def xml_escape(**kwargs)
Ronin::Support::Encoding::XML.escape(self,**kwargs)
end
#
# Unescapes the XML encoded String.
#
# @return [String]
# The unescaped String.
#
# @example
# "<p>one <span>two</span></p>".xml_unescape
# # => "<p>one <span>two</span></p>"
#
# @see http://rubydoc.info/stdlib/cgi/CGI.unescapeHash
# @see Ronin::Support::Encoding::XML.unescape
#
# @since 0.2.0
#
# @api public
#
def xml_unescape
Ronin::Support::Encoding::XML.unescape(self)
end
#
# Encodes each character in the String as an XML character.
#
# @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 XML encoded String.
#
# @example
# "abc".xml_encode
# # => "abc"
#
# @example Zero-padding:
# "abc".xml_encode(zero_pad: true)
# # => "abc"
#
# @example Hexadecimal encoded characters:
# "abc".xml_encode(format: :hex)
# # => "abc"
#
# @example Uppercase hexadecimal encoded characters:
# "abc\xff".xml_encode(format: :hex, case: :upper)
# # => "abcÿ"
#
# @see Ronin::Support::Encoding::XML.encode
#
# @since 1.0.0
#
# @api public
#
def xml_encode(**kwargs)
Ronin::Support::Encoding::XML.encode(self,**kwargs)
end
alias xml_decode xml_unescape
end