Skip to content

Commit

Permalink
debuggin rails integration
Browse files Browse the repository at this point in the history
  • Loading branch information
igrigorik committed Nov 2, 2011
1 parent 4dcc912 commit bd3d61c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
@@ -1,6 +1,6 @@
# Runtime Contextual Autoescaper

A JRuby wrapper for [Mike Samuel's contextual HTML autoescaper](https://github.com/mikesamuel/html-contextual-autoescaper-java).
A JRuby wrapper for [Mike Samuel's contextual HTML autoescaper](https://github.com/mikesamuel/html-contextual-autoescaper-java).

## Example

Expand Down
68 changes: 61 additions & 7 deletions lib/contextual/rails_erubis.rb
@@ -1,14 +1,59 @@
module ActionView
class Template
module Handlers
class Erubis < ::Erubis::Eruby

# class Erubis < ::Erubis::Eruby
# def add_preamble(src)
# src << "@output_buffer = output_buffer || ActionView::OutputBuffer.new;"
# end
#
# def add_text(src, text)
# return if text.empty?
# p [:add_text, :safe_concat, text]
# src << "@output_buffer.safe_concat('" << escape_text(text) << "');"
# end
#
# BLOCK_EXPR = /\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/
#
# def add_expr_literal(src, code)
# if code =~ BLOCK_EXPR
# p [:add_expr_literal, :block_append=, code]
#
# src << '@output_buffer.append= ' << code
# else
# p [:add_expr_literal, :append=, code]
#
# src << '@output_buffer.append= (' << code << ');'
# end
# end
#
# def add_expr_escaped(src, code)
# if code =~ BLOCK_EXPR
# p [:add_expr_escaped, :safe_append=, code]
#
# src << "@output_buffer.safe_append= " << code
# else
# p [:add_expr_escaped, :safe_concat, code]
# src << "@output_buffer.safe_concat((" << code << ").to_s);"
# end
# end
#
# def add_postamble(src)
# src << '@output_buffer.to_s'
# end
# end

class SafeErubis < ::Erubis::Eruby
BLOCK_EXPR = /\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/

def add_preamble(src)
src << "@output_buffer = output_buffer || Erubis::ContextualBuffer.new; "
p [:safe_add_preamble, src]
end

def add_text(src, text)
if !text.empty?

p [:safe_add_text, text]
src << "@output_buffer.writeSafe '" << text.to_s.gsub("'", "\\\\'") << "';"
end
end
Expand All @@ -19,31 +64,40 @@ def add_stmt(src, code)
end

def add_expr_escaped(src, code)
p [:safe_add_expr_escaped, code]
src << " @output_buffer.write((" << code << ').to_s);'
end

def add_expr_literal(src, code)
if code =~ BLOCK_EXPR
src << '@output_buffer.writeSafe ' << code
p [:safe_add_expr_literal, :block, code]
src << '@output_buffer.write ' << code
else
src << '@output_buffer.writeSafe (' << code << ');'
p [:safe_add_expr_literal, :no_block, code]
src << '@output_buffer.write(' << code << ');'
end
end

def add_expr_escaped(src, code)
if code =~ BLOCK_EXPR
p [:safe_add_expr_escaped, :block, code]
src << "@output_buffer.write " << code
else
src << "@output_buffer.write (" << code << ");"
p [:safe_add_expr_escaped, :no_block, code]
src << "@output_buffer.write(" << code << ");"
end
end

def add_postamble(src)
p [:add_postamble, src]
src << "@output_buffer.close \n"
# src << "p [:CONTEXTUAL @output_buffer.to_s]\n"
src << "@output_buffer.to_s"
src << "p [:CONTEXTUAL, @output_buffer.to_s]\n"
src << "@output_buffer.to_s.html_safe"
end
end

ERB.erb_implementation = SafeErubis

end
end
end
1 change: 0 additions & 1 deletion spec/contextual_spec.rb
Expand Up @@ -56,7 +56,6 @@
end

it "should render contextual template" do

object = {"world" => "<Cincinnati>", "color" => "blue"}
template = Erubis::ContextualEruby.new(TEMPLATE)
res = template.result(binding())
Expand Down

0 comments on commit bd3d61c

Please sign in to comment.