Sha256: a25a61fe273b906586bef664e4d06914b9b372e94559eb5993e6da1c12af992e

Contents?: true

Size: 1.19 KB

Versions: 11

Compression:

Stored size: 1.19 KB

Contents

require 'ndr_support/utf8_encoding'

module NdrImport
  module Xml
    # A class to remove control characters, and XML entities representing them
    class ControlCharEscaper
      include UTF8Encoding

      # Matches XML character reference entities
      CHARACTER_REFERENCES = /&#(?:(?<decimal>\d+)|x(?<hex>\h+));/.freeze

      attr_reader :data

      def initialize(data)
        @data = data
      end

      def escape!
        unescape_control_char_references!(data)
        escape_control_chars!(data)
      end

      private

      def unescape_control_char_references!(data)
        data.gsub!(CHARACTER_REFERENCES) do |reference|
          char = try_to_extract_char_from(Regexp.last_match)

          if char&.match?(CONTROL_CHARACTERS)
            escape_control_chars!(char)
          else
            reference
          end
        end
      end

      def try_to_extract_char_from(match)
        if match.nil?
          nil
        elsif match[:decimal]
          match[:decimal].to_i(10).chr
        elsif match[:hex]
          match[:hex].to_i(16).chr
        end
      rescue RangeError
        # Return everything if the match was against junk:
        match.to_s
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ndr_import-11.2.1 lib/ndr_import/xml/control_char_escaper.rb
ndr_import-11.2.0 lib/ndr_import/xml/control_char_escaper.rb
ndr_import-11.1.0 lib/ndr_import/xml/control_char_escaper.rb
ndr_import-11.0.2 lib/ndr_import/xml/control_char_escaper.rb
ndr_import-11.0.1 lib/ndr_import/xml/control_char_escaper.rb
ndr_import-11.0.0 lib/ndr_import/xml/control_char_escaper.rb
ndr_import-10.3.0 lib/ndr_import/xml/control_char_escaper.rb
ndr_import-10.2.0 lib/ndr_import/xml/control_char_escaper.rb
ndr_import-10.1.3 lib/ndr_import/xml/control_char_escaper.rb
ndr_import-10.1.2 lib/ndr_import/xml/control_char_escaper.rb
ndr_import-10.1.1 lib/ndr_import/xml/control_char_escaper.rb