[uilib] `uilib.js` helper now honors explicit JSString (closes #4959538)
authorAlain Leufroy <alain@leufroy.fr>
Fri, 27 Mar 2015 15:29:34 +0100
changeset 10332 da1cb2b12fe1
parent 10329 f92823a66f18
child 10343 359d68bc1260
[uilib] `uilib.js` helper now honors explicit JSString (closes #4959538) Previously, if a JSString was inside a nested container (i.e. list or dict), it was handled as a string, not a verbatim js chunk (see docstring and test). So the following code:: >>> str(js.cw.pouet(1, {'callback': JSString('cw.mycallback')}) resulted in:: "cw.pouet(1, {'callback': 'cw.mycallback')})" while we expect (note the removed quotes):: "cw.pouet(1, {'callback': cw.mycallback)})" We use ``cubiweb.utils.js_dumps`` instead of ``cubicweb.utils.json_dumps`` which is aware of JSString.
test/unittest_uilib.py
uilib.py
web/test/unittest_web.py
--- a/test/unittest_uilib.py	Tue Apr 07 17:08:53 2015 +0200
+++ b/test/unittest_uilib.py	Fri Mar 27 15:29:34 2015 +0100
@@ -30,7 +30,7 @@
 
 from logilab.common.testlib import DocTest, TestCase, unittest_main
 
-from cubicweb import uilib
+from cubicweb import uilib, utils as cwutils
 
 lxml_version = pkg_resources.get_distribution('lxml').version.split('.')
 
@@ -171,6 +171,11 @@
                           'cw.pouet(1,"2")')
         self.assertEqual(str(uilib.js.cw.pouet(1, "2").pouet(None)),
                           'cw.pouet(1,"2").pouet(null)')
+        self.assertEqual(str(uilib.js.cw.pouet(1, cwutils.JSString("$")).pouet(None)),
+                         'cw.pouet(1,$).pouet(null)')
+        self.assertEqual(str(uilib.js.cw.pouet(1, {'callback': cwutils.JSString("cw.cb")}).pouet(None)),
+                         'cw.pouet(1,{callback: cw.cb}).pouet(null)')
+
 
     def test_embedded_css(self):
         incoming = u"""voir le ticket <style type="text/css">@font-face { font-family: "Cambria"; }p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 10pt; font-size: 12pt; font-family: "Times New Roman"; }a:link, span.MsoHyperlink { color: blue; text-decoration: underline; }a:visited, span.MsoHyperlinkFollowed { color: purple; text-decoration: underline; }div.Section1 { page: Section1; }</style></p><p class="MsoNormal">text</p>"""
--- a/uilib.py	Tue Apr 07 17:08:53 2015 +0200
+++ b/uilib.py	Fri Mar 27 15:29:34 2015 +0100
@@ -32,7 +32,7 @@
 from logilab.common.date import ustrftime
 from logilab.common.deprecation import deprecated
 
-from cubicweb.utils import JSString, json_dumps
+from cubicweb.utils import js_dumps
 
 
 def rql_for_eid(eid):
@@ -353,10 +353,7 @@
     def __unicode__(self):
         args = []
         for arg in self.args:
-            if isinstance(arg, JSString):
-                args.append(arg)
-            else:
-                args.append(json_dumps(arg))
+            args.append(js_dumps(arg))
         if self.parent:
             return u'%s(%s)' % (self.parent, ','.join(args))
         return ','.join(args)
@@ -378,6 +375,8 @@
 'cw.pouet(1,"2").pouet(null)'
 >>> str(js.cw.pouet(1, JSString("$")).pouet(None))
 'cw.pouet(1,$).pouet(null)'
+>>> str(js.cw.pouet(1, {'callback': JSString("cw.cb")}).pouet(None))
+'cw.pouet(1,{callback: cw.cb}).pouet(null)'
 """
 
 def domid(string):
--- a/web/test/unittest_web.py	Tue Apr 07 17:08:53 2015 +0200
+++ b/web/test/unittest_web.py	Fri Mar 27 15:29:34 2015 +0100
@@ -50,7 +50,7 @@
         cbname = url.split()[1][:-2]
         self.assertMultiLineEqual(
             'function %s() { $("#foo").loadxhtml("http://testing.fr/cubicweb/ajax?%s",'
-            '{"pageid": "%s"},"get","replace"); }' %
+            '{pageid: "%s"},"get","replace"); }' %
             (cbname, qs, req.pageid),
             req.html_headers.post_inlined_scripts[0])