Permalink
Browse files

Merge pull request #21 from toscawidgets/feature/multiline-js

Feature/multiline js
  • Loading branch information...
ralphbean committed May 10, 2012
2 parents 3268c26 + b6bbf92 commit c9e0ada6f2bb8955c2320dc873abb0adae35f186
Showing with 56 additions and 8 deletions.
  1. +30 −3 tests/test_js.py
  2. +1 −1 tests/test_resources.py
  3. +25 −4 tw2/core/js.py
@@ -13,7 +13,7 @@ def setUp(self):
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")
@@ -49,7 +49,34 @@ def test_encoding_widget_id(self):
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)
@@ -295,7 +295,7 @@ def testEncoderDefault(self):
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)
@@ -7,6 +7,7 @@
as a "bridge" or interface between Python and JavaScript so JS function
**calls** can be generated programatically.
"""
import re
import sys
import logging
@@ -49,6 +50,7 @@ def __init__(self, *args, **kw):
# 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)
@@ -57,20 +59,39 @@ def __init__(self, *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.

0 comments on commit c9e0ada

Please sign in to comment.