Sha256: d821d43503b7258e00a9aefaa61d4e2aba5a0117971b963eba1809068921b867

Contents?: true

Size: 962 Bytes

Versions: 3

Compression:

Stored size: 962 Bytes

Contents

module WebConsole
  class View < Window

    def do_javascript_function(function, arguments = nil)
      javascript = self.class.javascript_function(function, arguments)
      do_javascript(javascript)
    end

    def self.javascript_function(function, arguments = nil)
      function = function.dup
      function << '('

      if arguments
        arguments.each { |argument|
          if argument
            function << argument.javascript_argument          
          else
            function << "null"
          end
          function << ', '
        }
        function = function[0...-2]
      end

      function << ');'
    end

    private

    class ::String
      def javascript_argument
        "'#{self.javascript_escape}'"
      end

      def javascript_escape
        self.gsub('\\', "\\\\\\\\").gsub("\n", "\\\\n").gsub("'", "\\\\'")
      end

      def javascript_escape!
        replace(self.javascript_escape)
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
webconsole-0.2.1 lib/webconsole/lib/view/javascript.rb
webconsole-0.2.0 lib/webconsole/lib/view/javascript.rb
webconsole-0.1.19 lib/webconsole/lib/view/javascript.rb