• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

Escape characters between ruby and javascript

Scheduled Pinned Locked Moved Developers' Forum
8 Posts 3 Posters 2.1k Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    Pout
    last edited by 3 Feb 2010, 19:30

    i have a javascript like this:

    settings=x+","+y+","+z;
    query = 'skp;_edit@'+settings;
    window.location.href = query;
    

    in ruby:

    window.add_action_callback("_edit") {|window,p|
    resultarray=p.split(",")
    

    Now the x, y or z value in javascript can contain commas.
    So this will cause problems for the resultarray in ruby.
    What is the best way to escape the comma's (or any problem characters in javascript) so they are comming to ruby in a usable way?

    Off course the other way arround is also a problem:
    ruby

    rubyarray=['blablabla,','boem,boem,boem','world']
    #join the array to send to javascript
    rubyarray=rubyarray.compact.join(",")
    command = "_test('#{rubyarray}')"
    window.execute_script(command)
    

    In javascriptI split up the string by comma

    function _test(x)
    p = x.split(',');
    

    This again gives me to much elements. How do i escape the unneccesary comma's in the ruby string?

    Thx!

    1 Reply Last reply Reply Quote 0
    • T Offline
      tbleicher
      last edited by 3 Feb 2010, 20:07

      @pout said:

      i have a javascript like this:

      settings=x+","+y+","+z;
      > query = 'skp;_edit@'+settings;
      > window.location.href = query;
      

      in ruby:

      window.add_action_callback("_edit") {|window,p|
      > resultarray=p.split(",")
      

      Now the x, y or z value in javascript can contain commas.
      So this will cause problems for the resultarray in ruby.

      What is the best way to escape the comma's (or any problem characters in javascript) so they are comming to ruby in a usable way?

      If you know that you're going to have commas in the variable values then a comma as a separation character is probably not a good choice. That said, you could first use javascript's escape() on all your variables which will turn every "," in a "%2c" and then join them with your separating ",".

      On the Ruby side you can find a "URL-decode" functions in the CGI module:

      
      def urlDecode(string)
          ## URL-decode from Ruby;;CGI
          string.tr('+', ' ').gsub(/((?;%[0-9a-fA-F]{2})+)/n) do
              [$1.delete('%')].pack('H*')
          end
      end
      

      @pout said:

      Off course the other way arround is also a problem:
      ruby

      rubyarray=['blablabla,','boem,boem,boem','world']
      > #join the array to send to javascript
      > rubyarray=rubyarray.compact.join(",")
      > command = "_test('#{rubyarray}')"
      > window.execute_script(command)
      

      In javascriptI split up the string by comma

      function _test(x)
      > p = x.split(',');
      

      Same procedure only the other way around. These days I prefer to build a JSON string in Ruby and eval() that on the javascript side for anything that's a bit more complex. In your case that would take car of the the encoding because your rubyarray also works as javascript array.

      Cheers,
      Thomas

      1 Reply Last reply Reply Quote 0
      • T Offline
        todd burch
        last edited by 3 Feb 2010, 22:18

        Or, you could make 3 callbacks instead, one for x, another for y and a third for z.

        1 Reply Last reply Reply Quote 0
        • T Offline
          todd burch
          last edited by 3 Feb 2010, 22:29

          Another option would be prefix the length of each "section" in front of the section in javascript, like this: (adding a colon to separate the length from the value)

          
          var settings = '' ; 
          var x = 'abc' ; 
          var y = 'd,e,f' ; 
          var z = '1,2345' ; 
          settings = settings + x.length + ';' + x ; 
          settings = settings + y.length + ';' + y ; 		
          settings = settings + z.length + ';' + z ; 
          
          

          This yields:

          
          3;abc5;d,e,f6;1,2345
          
          
          1 Reply Last reply Reply Quote 0
          • P Offline
            Pout
            last edited by 4 Feb 2010, 08:16

            Thx Thomas and Tod.

            Tod,
            What would be the most correct way then to split the settings string again in ruby?

            I had another idea, dunno if it is a descent one:

            If i replace every possible problem character with a code eg.
            ,--><c0mm4>
            '--><qu0t3>
            "--><2qu0t3>

            And this at both the javascript as the ruby side (and re-encode them if necessary).
            Chances of having that exact value in a string are close to nil I think.

            Would this be a good and solid solution, or just a workarround for dummies?

            1 Reply Last reply Reply Quote 0
            • P Offline
              Pout
              last edited by 4 Feb 2010, 13:34

              Thomas,

              When I use your suggestion it does not seem to work
              escape in javascript indeed give me %c2
              but the p value in the callback still contains , in the strings

              example

              x="abc";
              y="1,2,3";
              z="1,23";
              x=escape(x);
              y=escape(y);
              z=escape(z);
              settings=x+","+y+","+z;
              alert (settings); -->abc,1%c22%c23,1%c223
              query = 'skp;_edit@'+settings;
              window.location.href = query;
              
              window.add_action_callback("_edit") {|window,p|
              puts p -->abc,1,2,3,1,223
              
              1 Reply Last reply Reply Quote 0
              • T Offline
                todd burch
                last edited by 4 Feb 2010, 14:38

                Here's a link that shows how to convert from ascii to hex in javascript. If you convert the whole string to hex, you can use the pack() method in Ruby to get it back to ascii. http://www.ajaxblender.com/howto-convert-ascii-hex-binary-decimal.html .

                1 Reply Last reply Reply Quote 0
                • P Offline
                  Pout
                  last edited by 8 Feb 2010, 10:02

                  Thx all. I managed to get it working

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  1 / 1
                  • First post
                    4/8
                    Last post
                  Buy SketchPlus
                  Buy SUbD
                  Buy WrapR
                  Buy eBook
                  Buy Modelur
                  Buy Vertex Tools
                  Buy SketchCuisine
                  Buy FormFonts

                  Advertisement