Class: GMaps::JS
- Inherits:
-
Object
- Object
- GMaps::JS
- Defined in:
- lib/gmaps/js.rb
Constant Summary collapse
- JS_ESCAPE_MAP =
escape_javascript Copied from ActionView::Helpers::JavaScriptHelper
{ '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }
Class Method Summary collapse
Instance Method Summary collapse
-
#escape_javascript(javascript) ⇒ Object
Escapes carriage returns and single and double quotes for JavaScript segments.
-
#initialize(str, *args) ⇒ JS
constructor
A new instance of JS.
- #inspect ⇒ Object (also: #to_s)
Constructor Details
#initialize(str, *args) ⇒ JS
Returns a new instance of JS.
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/gmaps/js.rb', line 11 def initialize(str, *args) @str = if args.empty? str elsif args.length == 1 && args.first.is_a?(Hash) escaped_args = {} args.first.each { |k, v| escaped_args[k] = v.is_a?(String) ? escape_javascript(v) : v} str % (escaped_args) else args.map!{ |arg| arg.is_a?(String) ? escape_javascript(arg) : arg} str % args end end |
Class Method Details
.[](*args) ⇒ Object
6 7 8 |
# File 'lib/gmaps/js.rb', line 6 def [](*args) self.new(*args) end |
Instance Method Details
#escape_javascript(javascript) ⇒ Object
Escapes carriage returns and single and double quotes for JavaScript segments.
Also available through the alias j(). This is particularly helpful in JavaScript responses, like:
$('some_element').replaceWith('<%=j render 'some/element_template' %>');
52 53 54 55 56 57 58 59 |
# File 'lib/gmaps/js.rb', line 52 def escape_javascript(javascript) if javascript result = javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) {|match| JS_ESCAPE_MAP[match] } javascript.html_safe? ? result.html_safe : result else '' end end |
#inspect ⇒ Object Also known as: to_s
24 25 26 |
# File 'lib/gmaps/js.rb', line 24 def inspect @str end |