Sha256: fbb0cf9063d5ec01e93f39ab0d22c1b3fe2504e5c672ca02e9342daff39f7cf8

Contents?: true

Size: 1.32 KB

Versions: 3

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.0.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

3 entries across 3 versions & 1 rubygems

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