Permalink
Browse files

Escape javascript using ActionView::Helpers::JavaScriptHelper to avoi…

…d code duplication and differences between rails versions. [#24 state:resolved]
  • Loading branch information...
paneq committed Jun 15, 2011
1 parent 29fa99b commit ae1e38f5560d92e2ccdd32f06f68425ec8fbc481
Showing with 4 additions and 12 deletions.
  1. +4 −12 lib/apotomo/javascript_generator.rb
@@ -1,3 +1,5 @@
+require 'action_view/helpers/javascript_helper'
+
module Apotomo
class JavascriptGenerator
def initialize(framework)
@@ -9,21 +11,11 @@ def <<(javascript)
"#{javascript}"
end
- # Copied from ActionView::Helpers::JavascriptHelper.
- JS_ESCAPE_MAP = {
- '\\' => '\\\\',
- '</' => '<\/',
- "\r\n" => '\n',
- "\n" => '\n',
- "\r" => '\n',
- '"' => '\\"',
- "'" => "\\'" }
+ JS_ESCAPER = Object.new.extend(::ActionView::Helpers::JavaScriptHelper)
# Escape carrier returns and single and double quotes for JavaScript segments.
def self.escape(javascript)
- return javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] } if javascript
-
- ''
+ JS_ESCAPER.escape_javascript(javascript)
end
def escape(javascript)

0 comments on commit ae1e38f

Please sign in to comment.