erb.rb 4.1 KB
Newer Older
J
Jeremy Kemper 已提交
1
require 'active_support/core_ext/class/attribute_accessors'
2
require 'active_support/core_ext/string/output_safety'
3
require "action_view/template"
4
require 'erubis'
5

6
module ActionView
7
  class OutputBuffer < ActiveSupport::SafeBuffer
8 9
    def initialize(*)
      super
10
      encode! if encoding_aware?
11 12
    end

13
    def <<(value)
14
      super(value.to_s)
15
    end
16
    alias :append= :<<
C
Carlhuda 已提交
17 18 19 20 21 22 23

    def append_if_string=(value)
      if value.is_a?(String) && !value.is_a?(NonConcattingString)
        ActiveSupport::Deprecation.warn("<% %> style block helpers are deprecated. Please use <%= %>", caller)
        self << value
      end
    end
24 25
  end

26 27 28 29 30 31
  class Template
    module Handlers
      class Erubis < ::Erubis::Eruby
        def add_preamble(src)
          src << "@output_buffer = ActionView::OutputBuffer.new;"
        end
32

33 34 35 36
        def add_text(src, text)
          return if text.empty?
          src << "@output_buffer.safe_concat('" << escape_text(text) << "');"
        end
37

38
        BLOCK_EXPR = /\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/
C
Carlhuda 已提交
39

40 41 42 43 44 45
        def add_expr_literal(src, code)
          if code =~ BLOCK_EXPR
            src << '@output_buffer.append= ' << code
          else
            src << '@output_buffer.append= (' << code << ');'
          end
46
        end
47

48 49 50 51 52 53
        def add_stmt(src, code)
          if code =~ BLOCK_EXPR
            src << '@output_buffer.append_if_string= ' << code
          else
            super
          end
C
Carlhuda 已提交
54 55
        end

56 57 58
        def add_expr_escaped(src, code)
          src << '@output_buffer.append= ' << escaped_expr(code) << ';'
        end
59

60 61 62
        def add_postamble(src)
          src << '@output_buffer.to_s'
        end
63 64
      end

65 66
      class ERB < Handler
        include Compilable
67

68 69 70 71 72 73
        ##
        # :singleton-method:
        # Specify trim mode for the ERB compiler. Defaults to '-'.
        # See ERb documentation for suitable values.
        cattr_accessor :erb_trim_mode
        self.erb_trim_mode = '-'
74

75
        self.default_format = Mime::HTML
76

77 78
        cattr_accessor :erb_implementation
        self.erb_implementation = Erubis
79

80 81 82 83 84
        ENCODING_TAG = Regexp.new("\\A(<%#{ENCODING_FLAG}-?%>)[ \\t]*")

        def self.accepts_binary?
          true
        end
85 86

        def compile(template)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
          if template.source.encoding_aware?
            # Even though Rails has given us a String tagged with the
            # default_internal encoding (likely UTF-8), it is possible
            # that the String is actually encoded using a different
            # encoding, specified via an ERB magic comment. If the
            # String is not actually UTF-8, the regular expression
            # engine will (correctly) raise an exception. For now,
            # we'll reset the String to BINARY so we can run regular
            # expressions against it
            template_source = template.source.dup.force_encoding("BINARY")

            # Erubis does not have direct support for encodings.
            # As a result, we will extract the ERB-style magic
            # comment, give the String to Erubis as BINARY data,
            # and then tag the resulting String with the extracted
            # encoding later
            erb = template_source.gsub(ENCODING_TAG, '')
            encoding = $2

            if !encoding && (template.source.encoding == Encoding::BINARY)
              raise WrongEncodingError.new(template_source, Encoding.default_external)
            end
109 110
          else
            erb = template.source.dup
111 112
          end

113 114 115 116 117
          result = self.class.erb_implementation.new(
            erb,
            :trim => (self.class.erb_trim_mode == "-")
          ).src

118 119 120 121 122 123 124 125
          # If an encoding tag was found, tag the String
          # we're returning with that encoding. Otherwise,
          # return a BINARY String, which is what ERB
          # returns. Note that if a magic comment was
          # not specified, we will return the data to
          # Rails as BINARY, which will then use its
          # own encoding logic to create a UTF-8 String.
          result = "\n#{result}".force_encoding(encoding).encode if encoding
126 127
          result
        end
128 129 130 131
      end
    end
  end
end