Skip to content

Commit

Permalink
EscapeHTML expression changed
Browse files Browse the repository at this point in the history
[:escape, true/false, content]
  • Loading branch information
minad committed Mar 22, 2011
1 parent 5e2b410 commit 72936c2
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 31 deletions.
1 change: 1 addition & 0 deletions lib/temple.rb
Expand Up @@ -29,6 +29,7 @@ module Filters
end

module HTML
autoload :Dispatcher, 'temple/html/dispatcher'
autoload :Fast, 'temple/html/fast'
autoload :Pretty, 'temple/html/pretty'
end
Expand Down
2 changes: 1 addition & 1 deletion lib/temple/erb/engine.rb
Expand Up @@ -2,8 +2,8 @@ module Temple
module ERB
class Engine < Temple::Engine
use Temple::ERB::Parser, :auto_escape
use Temple::ERB::Trimming, :trim_mode
filter :EscapeHTML, :use_html_safe
use Temple::ERB::Trimming, :trim_mode
filter :MultiFlattener
filter :StaticMerger
filter :DynamicInliner
Expand Down
2 changes: 1 addition & 1 deletion lib/temple/erb/parser.rb
Expand Up @@ -25,7 +25,7 @@ def call(input)
when '#'
code.count("\n").times { result << [:newline] }
when /=/
result << (indicator.length > 1 || !options[:auto_escape] ? [:dynamic, code] : [:escape, :dynamic, code])
result << [:escape, indicator.length <= 1 && options[:auto_escape], [:dynamic, code]]
else
result << [:block, code]
end
Expand Down
29 changes: 16 additions & 13 deletions lib/temple/filters/escape_html.rb
@@ -1,29 +1,32 @@
module Temple
module Filters
class EscapeHTML < Filter
temple_dispatch :escape, :html
include Temple::HTML::Dispatcher

temple_dispatch :html

# Activate the usage of html_safe? if it is available (for Rails 3 for example)
default_options[:use_html_safe] = ''.respond_to?(:html_safe?)

def on_escape_static(value)
[:static, options[:use_html_safe] ? escape_html_safe(value) : escape_html(value)]
end

def on_escape_dynamic(value)
[:dynamic, "Temple::Utils.escape_html#{options[:use_html_safe] ? '_safe' : ''}((#{value}))"]
def initialize(opts = {})
super
@escape = false
end

def on_html_staticattrs(*attrs)
[:html, :staticattrs, *attrs.map {|k,v| [k, compile(v)] }]
def on_escape(flag, exp)
old = @escape
@escape = flag
compile(exp)
ensure
@escape = old
end

def on_html_comment(content)
[:html, :comment, compile(content)]
def on_static(value)
[:static, @escape ? (options[:use_html_safe] ? escape_html_safe(value) : escape_html(value)) : value]
end

def on_html_tag(name, attrs, closed, content)
[:html, :tag, name, compile(attrs), closed, compile(content)]
def on_dynamic(value)
[:dynamic, @escape ? "Temple::Utils.escape_html#{options[:use_html_safe] ? '_safe' : ''}((#{value}))" : value]
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions lib/temple/html/dispatcher.rb
@@ -0,0 +1,17 @@
module Temple
module HTML
module Dispatcher
def on_html_staticattrs(*attrs)
[:html, :staticattrs, *attrs.map {|k,v| [k, compile(v)] }]
end

def on_html_comment(content)
[:html, :comment, compile(content)]
end

def on_html_tag(name, attrs, closed, content)
[:html, :tag, name, compile(attrs), closed, compile(content)]
end
end
end
end
24 changes: 16 additions & 8 deletions lib/temple/mixins.rb
Expand Up @@ -103,7 +103,23 @@ def element(args, block)
end
end

module CoreDispatcher
def on_multi(*exps)
[:multi, *exps.map {|exp| compile(exp) }]
end

def on_capture(name, exp)
[:capture, name, compile(exp)]
end

def on_escape(flag, exp)
[:escape, flag, compile(exp)]
end
end

module Dispatcher
include CoreDispatcher

def self.included(base)
base.class_eval { extend ClassMethods }
end
Expand All @@ -121,14 +137,6 @@ def compile(exp)
end
end

def on_multi(*exps)
[:multi, *exps.map {|exp| compile(exp) }]
end

def on_capture(name, exp)
[:capture, name, compile(exp)]
end

module ClassMethods
def temple_dispatch(*bases)
bases.each do |base|
Expand Down
15 changes: 7 additions & 8 deletions test/filters/test_escape_html.rb
Expand Up @@ -12,9 +12,10 @@ def html_safe?
end

it 'should handle escape expressions' do
@filter.call([:multi,
[:escape, :static, "a < b"],
[:escape, :dynamic, "ruby_method"]
@filter.call([:escape, true,
[:multi,
[:static, "a < b"],
[:dynamic, "ruby_method"]]
]).should.equal [:multi,
[:static, "a &lt; b"],
[:dynamic, "Temple::Utils.escape_html((ruby_method))"],
Expand All @@ -38,10 +39,8 @@ def html_safe?

it 'should have use_html_safe option' do
filter = Temple::Filters::EscapeHTML.new(:use_html_safe => true)
filter.call([:multi,
[:escape, :static, HtmlSafeString.new("a < b")],
]).should.equal [:multi,
[:static, "a < b"],
]
filter.call([:escape, true,
[:static, HtmlSafeString.new("a < b")],
]).should.equal [:static, "a < b"],
end
end

0 comments on commit 72936c2

Please sign in to comment.