Commits

Ralph Bean committed a7e1d46 Merge

Merge branch 'develop' into feature/template-sys

Comments (0)

Files changed (3)

 
     def test_js_function(self):
         json = self.encode({"onLoad": js_function("do_something")("param")})
-        eq_(json, '{"onLoad": do_something(\\"param\\")}')
+        eq_(json, '{"onLoad": do_something("param")}')
 
     def test_js_function_composition(self):
         f = js_function("f")
         args = {'onLoad': f}
 
         json = self.encode(args)
-        eq_(json, '{"onLoad": function(){jQuery(\\"foo\\").click(onClick)}}')
+        eq_(json, '{"onLoad": function(){jQuery("foo").click(onClick)}}')
 
         json = self.encode({'args':args})
-        eq_(json, '{"args": {"onLoad": function(){jQuery(\\"foo\\").click(onClick)}}}')
+        eq_(json, '{"args": {"onLoad": function(){jQuery("foo").click(onClick)}}}')
+
+    def test_quotes_no_escape(self):
+        f = twc.js_callback("function() { return 'c'; }")
+        json = self.encode({'onLoad': f})
+        eq_(json, '{"onLoad": function() { return \'c\'; }}')
+
+    def test_quotes_escape(self):
+        f = twc.js_callback("function() { return \"c\"; }")
+        json = self.encode({'onLoad': f})
+        eq_(json, '{"onLoad": function() { return "c"; }}')
+
+    def test_quotes_tripled(self):
+        f = twc.js_callback("""function() { return "c"; }""")
+        json = self.encode({'onLoad': f})
+        eq_(json, '{"onLoad": function() { return "c"; }}')
+
+    def test_multiline_js(self):
+        """ https://github.com/toscawidgets/tw2.core/issues/12 """
+        raw = """
+            $(document).ready(function() {
+                  console.log("This is multiline javascript");
+            });
+        """
+        expected = '{"onLoad": %s}' % raw
+        f = js_symbol(raw)
+        json = self.encode({'onLoad': f})
+        eq_(json, expected)

tests/test_resources.py

         enc = twc.encoder
         enc.encode("")
         res = enc.default(twr.JSSymbol("X"))
-        self.assert_(res.startswith("*#*"))
+        self.assert_(res.startswith("TW2Encoder_unescape_"))
 
         try:
             res = enc.default(None)
 as a "bridge" or interface between Python and JavaScript so JS function
 **calls** can be generated programatically.
 """
+import re
 import sys
 
 import logging
         # TODO -- make json encoding pretty-printing configurable
         #kw['indent'] = '  '
 
+        self.unescape_pattern = re.compile('"TW2Encoder_unescape_([0-9]*)"')
         self.pass_through = (_js_call, js_callback, js_symbol, js_function)
         super(TWEncoder, self).__init__(*args, **kw)
 
 
     def default(self, obj):
         if isinstance(obj, self.pass_through):
-            return self.mark_for_escape(obj)
-        elif hasattr(obj, 'id'):
+            result = self.mark_for_escape(obj)
+            return result
+
+        if hasattr(obj, '__json__'):
+            return obj.__json__()
+
+        if hasattr(obj, 'id'):
             return str(obj.id)
+
         return super(TWEncoder, self).default(obj)
 
     def encode(self, obj):
+        self.unescape_symbols = {}
+        encoded = super(TWEncoder, self).encode(obj)
+        unescaped = self.unescape_marked(encoded)
+        self.unescape_symbols = {}
+        return unescaped
+
         encoded = super(TWEncoder, self).encode(obj)
         return self.unescape_marked(encoded)
 
     def mark_for_escape(self, obj):
-        return '*#*%s*#*' % obj
+        self.unescape_symbols[id(obj)] = obj
+        return 'TW2Encoder_unescape_' + str(id(obj))
 
     def unescape_marked(self, encoded):
-        return encoded.replace('"*#*', '').replace('*#*"', '')
+        def unescape(match):
+            obj_id = int(match.group(1))
+            obj = self.unescape_symbols[obj_id]
+            return str(obj)
+
+        return self.unescape_pattern.sub(unescape, encoded)
+
 
 encoder = None  # This gets reset at the bottom of the file.