Sha256: 60e1bd7ded46ca178ce6939163609acf41631af5fdeb2bf11d342716d8316218

Contents?: true

Size: 1.32 KB

Versions: 7

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

# Based on Temple::ERB::Trimming, also released under the MIT licence.
#
# Copyright (c) 2010-2023 Magnus Holm.

module Hanami
  class View
    module ERB
      module Filters
        # Trims spurious spaces from ERB-generated text.
        #
        # Deletes spaces around "<% %>" and leave spaces around "<%= %>".
        #
        # This is a copy of Temple::ERB::Trimming, with the one difference being that it descends
        # into the sexp-tree by running `compile(e)` for any non-`:static` sexps. This is necessary
        # for our implementation of ERB, because we capture block content by creating additional
        # `:multi` sexps with their own nested content.
        #
        # @api private
        # @since 2.1.0
        class Trimming < Temple::Filter
          define_options trim: true

          def on_multi(*exps)
            exps = exps.each_with_index.map do |e,i|
              if e.first == :static && i > 0 && exps[i-1].first == :code
                [:static, e.last.lstrip]
              elsif e.first == :static && i < exps.size-1 && exps[i+1].first == :code
                [:static, e.last.rstrip]
              else
                compile(e)
              end
            end if options[:trim]

            [:multi, *exps]
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hanami-view-2.2.0 lib/hanami/view/erb/filters/trimming.rb
hanami-view-2.2.0.rc1 lib/hanami/view/erb/filters/trimming.rb
hanami-view-2.2.0.beta2 lib/hanami/view/erb/filters/trimming.rb
hanami-view-2.2.0.beta1 lib/hanami/view/erb/filters/trimming.rb
hanami-view-2.1.0 lib/hanami/view/erb/filters/trimming.rb
hanami-view-2.1.0.rc3 lib/hanami/view/erb/filters/trimming.rb
hanami-view-2.1.0.rc2 lib/hanami/view/erb/filters/trimming.rb