1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Initial work to improve the state of encodings for templates

This commit is contained in:
wycats 2010-05-15 03:47:24 -07:00
parent f8432108e8
commit af0d1a8815
6 changed files with 88 additions and 52 deletions

View file

@ -58,6 +58,8 @@ module ActionView
end
autoload :TestCase, 'action_view/test_case'
ENCODING_FLAG = "#.*coding[:=]\s*(\S+)[ \t]*"
end
require 'active_support/i18n'

View file

@ -27,6 +27,12 @@ module ActionView
end
def initialize(source, identifier, handler, details)
if source.encoding_aware? && source =~ %r{\A#{ENCODING_FLAG}}
# don't snip off the \n to preserve line numbers
source.sub!(/\A[^\n]*/, '')
source.force_encoding($1).encode
end
@source = source
@identifier = identifier
@handler = handler

View file

@ -1,5 +1,6 @@
require 'active_support/core_ext/class/attribute_accessors'
require 'active_support/core_ext/string/output_safety'
require "action_view/template"
require 'erubis'
module ActionView
@ -17,7 +18,8 @@ module ActionView
end
end
module Template::Handlers
class Template
module Handlers
class Erubis < ::Erubis::Eruby
def add_preamble(src)
src << "@output_buffer = ActionView::OutputBuffer.new;"
@ -55,7 +57,7 @@ module ActionView
end
end
class ERB < Template::Handler
class ERB < Handler
include Compilable
##
@ -70,13 +72,19 @@ module ActionView
cattr_accessor :erb_implementation
self.erb_implementation = Erubis
ENCODING_TAG = Regexp.new("\A(<%#{ENCODING_FLAG}-?%>)[ \t]*")
def compile(template)
source = template.source.gsub(/\A(<%(#.*coding[:=]\s*(\S+)\s*)-?%>)\s*\n?/, '')
erb = "<% __in_erb_template=true %>#{source}"
result = self.class.erb_implementation.new(erb, :trim=>(self.class.erb_trim_mode == "-")).src
erb = template.source.gsub(ENCODING_TAG, '')
result = self.class.erb_implementation.new(
erb,
:trim => (self.class.erb_trim_mode == "-")
).src
result = "#{$2}\n#{result}" if $2
result
end
end
end
end
end

View file

@ -0,0 +1,11 @@
class String
if defined?(Encoding) && "".respond_to?(:encode)
def encoding_aware?
true
end
else
def encoding_aware?
false
end
end
end

View file

@ -15,6 +15,7 @@ require 'active_support/core_ext/enumerable'
require 'active_support/core_ext/process/daemon'
require 'active_support/core_ext/string/conversions'
require 'active_support/core_ext/string/interpolation'
require 'active_support/core_ext/string/encoding'
require 'active_support/core_ext/rexml'
require 'active_support/core_ext/time/conversions'
require 'active_support/core_ext/file/path'

View file

@ -439,6 +439,14 @@ class OutputSafetyTest < ActiveSupport::TestCase
test 'emits normal string yaml' do
assert_equal 'foo'.to_yaml, 'foo'.html_safe.to_yaml(:foo => 1)
end
test 'knows whether it is encoding aware' do
if RUBY_VERSION >= "1.9"
assert 'ruby'.encoding_aware?
else
assert !'ruby'.encoding_aware?
end
end
end
class StringExcludeTest < ActiveSupport::TestCase