Sha256: 22b8d01ee453e9e715e6d8e623a02bb85034a65b41b1dc852e80f09ae5812735

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true
module Slim
  module Smart
    # Perform smart entity escaping in the
    # expressions `[:slim, :text, type, Expression]`.
    #
    # @api private
    class Escaper < ::Slim::Filter
      define_options smart_text_escaping: true

      def call(exp)
        if options[:smart_text_escaping]
          super
        else
          exp
        end
      end

      def on_slim_text(type, content)
        [:escape, type != :verbatim, [:slim, :text, type, compile(content)]]
      end

      def on_static(string)
        # Prevent obvious &foo; and &#1234; and &#x00ff; entities from escaping.
        block = [:multi]
        until string.empty?
          case string
          when /\A&([a-z][a-z0-9]*|#x[0-9a-f]+|#\d+);/i
            # Entity.
            block << [:escape, false, [:static, $&]]
            string = $'
          when /\A&?[^&]*/
            # Other text.
            block << [:static, $&]
            string = $'
          end
        end
        block
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slim-5.1.1 lib/slim/smart/escaper.rb