Sha256: 9219b80f07fa1134b59e74f3e376d5e04d6a48764a968b9c8feeca7526441435

Contents?: true

Size: 1.64 KB

Versions: 23

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module Asciidoctor
  module PDF
    module Sanitizer
      XMLSpecialChars = {
        '&lt;' => '<',
        '&gt;' => '>',
        '&amp;' => '&',
      }
      XMLSpecialCharsRx = /(?:#{XMLSpecialChars.keys.join '|'})/
      InverseXMLSpecialChars = XMLSpecialChars.invert
      InverseXMLSpecialCharsRx = /[#{InverseXMLSpecialChars.keys.join}]/
      (BuiltInNamedEntities = {
        'amp' => '&',
        'apos' => ?',
        'gt' => '>',
        'lt' => '<',
        'nbsp' => ' ',
        'quot' => '"',
      }).default = '?'
      SanitizeXMLRx = /<[^>]+>/
      CharRefRx = /&(?:amp;)?(?:([a-z][a-z]+\d{0,2})|#(?:(\d\d\d{0,4})|x(\h\h\h{0,3})));/
      UnescapedAmpersandRx = /&(?!(?:[a-z][a-z]+\d{0,2}|#(?:\d\d\d{0,4}|x\h\h\h{0,3}));)/

      # Strip leading, trailing and repeating whitespace, remove XML tags and
      # resolve all entities in the specified string.
      #
      # FIXME: move to a module so we can mix it in elsewhere
      # FIXME: add option to control escaping entities, or a filter mechanism in general
      def sanitize string
        string = string.gsub SanitizeXMLRx, '' if string.include? '<'
        string = string.gsub(CharRefRx) { $1 ? BuiltInNamedEntities[$1] : ([$2 ? $2.to_i : ($3.to_i 16)].pack 'U1') } if string.include? '&'
        string.strip.tr_s ' ', ' '
      end

      def escape_xml string
        string.gsub InverseXMLSpecialCharsRx, InverseXMLSpecialChars
      end

      def escape_amp string
        string.gsub UnescapedAmpersandRx, '&amp;'
      end

      def encode_quotes string
        (string.include? '"') ? (string.gsub '"', '&quot;') : string
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
asciidoctor-pdf-2.2.0 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.1.6 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.1.5 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.1.4 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.1.3 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.1.2 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.1.1 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.1.0 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.0.8 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.0.7 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.0.6 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.0.5 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.0.4 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.0.3 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.0.2 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.0.1 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.0.0 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.0.0.rc.1 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.0.0.beta.2 lib/asciidoctor/pdf/sanitizer.rb
asciidoctor-pdf-2.0.0.beta.1 lib/asciidoctor/pdf/sanitizer.rb