[Checkins] SVN: zope3org/trunk/src/zorg/live/ fixed problems with javascript eval and use JSON.parse instead

Uwe Oestermeier uwe_oestermeier at iwm-kmrc.de
Sat Apr 8 11:40:37 EDT 2006


Log message for revision 66684:
  fixed problems with javascript eval and use JSON.parse instead

Changed:
  U   zope3org/trunk/src/zorg/live/demo/comment/templates/comments.pt
  U   zope3org/trunk/src/zorg/live/demo/upload/progress.pt
  U   zope3org/trunk/src/zorg/live/demo/upload/upload.pt
  U   zope3org/trunk/src/zorg/live/page/client.js
  U   zope3org/trunk/src/zorg/live/page/configure.zcml
  A   zope3org/trunk/src/zorg/live/page/json.js
  U   zope3org/trunk/src/zorg/live/server.py

-=-
Modified: zope3org/trunk/src/zorg/live/demo/comment/templates/comments.pt
===================================================================
--- zope3org/trunk/src/zorg/live/demo/comment/templates/comments.pt	2006-04-08 15:27:01 UTC (rev 66683)
+++ zope3org/trunk/src/zorg/live/demo/comment/templates/comments.pt	2006-04-08 15:40:36 UTC (rev 66684)
@@ -23,6 +23,9 @@
     <script type="text/javascript" src="prototype.js" 
        tal:attributes="src string:${context/++resource++zorgajax/prototype.js}">
     </script>
+    <script type="text/javascript" src="json.js" 
+       tal:attributes="src string:${context/++resource++json.js}">
+    </script>
     <script type="text/javascript" src="livepage.js" 
        tal:attributes="src string:${context/++resource++livepage.js}">
     </script>

Modified: zope3org/trunk/src/zorg/live/demo/upload/progress.pt
===================================================================
--- zope3org/trunk/src/zorg/live/demo/upload/progress.pt	2006-04-08 15:27:01 UTC (rev 66683)
+++ zope3org/trunk/src/zorg/live/demo/upload/progress.pt	2006-04-08 15:40:36 UTC (rev 66684)
@@ -2,6 +2,9 @@
     <script type="text/javascript" src="prototype.js" 
        tal:attributes="src string:${context/++resource++zorgajax/prototype.js}">
     </script>
+    <script type="text/javascript" src="json.js" 
+       tal:attributes="src string:${context/++resource++json.js}">
+    </script>
     <script type="text/javascript" src="livepage.js" 
        tal:attributes="src string:${context/++resource++livepage.js}">
     </script>

Modified: zope3org/trunk/src/zorg/live/demo/upload/upload.pt
===================================================================
--- zope3org/trunk/src/zorg/live/demo/upload/upload.pt	2006-04-08 15:27:01 UTC (rev 66683)
+++ zope3org/trunk/src/zorg/live/demo/upload/upload.pt	2006-04-08 15:40:36 UTC (rev 66684)
@@ -23,6 +23,9 @@
     <script type="text/javascript" src="prototype.js" 
        tal:attributes="src string:${context/++resource++zorgajax/prototype.js}">
     </script>
+    <script type="text/javascript" src="json.js" 
+       tal:attributes="src string:${context/++resource++json.js}">
+    </script>
     <script type="text/javascript" src="livepage.js" 
        tal:attributes="src string:${context/++resource++livepage.js}">
     </script>

Modified: zope3org/trunk/src/zorg/live/page/client.js
===================================================================
--- zope3org/trunk/src/zorg/live/page/client.js	2006-04-08 15:27:01 UTC (rev 66683)
+++ zope3org/trunk/src/zorg/live/page/client.js	2006-04-08 15:40:36 UTC (rev 66684)
@@ -1,3 +1,5 @@
+
+
 var LivePage = {
     uuid: "",
     baseURL: "",
@@ -7,7 +9,7 @@
     alreadyEvaluated : {},
 
     ajaxHandlers : {
-        onError: function(request, transport, json) {
+        onError: function(request, transport) {
             LivePage.numErrors += 1;
 			if (LivePage.numErrors > 3) {
                 alert("Too much errors. The page will be reloaded.");
@@ -28,10 +30,14 @@
 		},
 
         onComplete: function(request, transport, json) {
-			var event = eval(transport.responseText);
-            var name = event['name'];
-            name = "on" + name[0].toUpperCase() + name.slice(1);
-            LivePage.Responders.dispatch(name, event);
+            
+            if (transport.responseText == "json") {
+                var event = JSON.parse(request.header('X-JSON'));
+                var name = event['name'];
+                name = "on" + name[0].toUpperCase() + name.slice(1);
+                
+                LivePage.Responders.dispatch(name, event);
+                }
 			setTimeout("LivePage.nextEvent()", 500);
 			return true;
 			}
@@ -87,11 +93,12 @@
         if (act) {
             switch(act) {
                 case 'scroll' : {
-                    scrollToLast(id);
+                    LivePage.scrollToLast(id);
+                    LivePage.highlightElement(id);
                     return;
                     }
                 case 'sound' : {
-                    playFlash("ping");
+                    LivePage.playFlash("ping");
                     return;
                     }
                 }
@@ -106,6 +113,11 @@
             area.scrollTop = area.scrollHeight;
             }
         }
+        
+    highlightElement : function (id) {
+        
+        }
+
 }
 
 LivePage.Responders = {
@@ -183,3 +195,4 @@
             
     }
             
+

Modified: zope3org/trunk/src/zorg/live/page/configure.zcml
===================================================================
--- zope3org/trunk/src/zorg/live/page/configure.zcml	2006-04-08 15:27:01 UTC (rev 66683)
+++ zope3org/trunk/src/zorg/live/page/configure.zcml	2006-04-08 15:40:36 UTC (rev 66684)
@@ -8,6 +8,9 @@
   <resource 
         name="livepage.js"
         file="client.js"/>
+  <resource 
+        name="json.js"
+        file="json.js"/>
 	
   <page
         name="input"

Added: zope3org/trunk/src/zorg/live/page/json.js
===================================================================
--- zope3org/trunk/src/zorg/live/page/json.js	2006-04-08 15:27:01 UTC (rev 66683)
+++ zope3org/trunk/src/zorg/live/page/json.js	2006-04-08 15:40:36 UTC (rev 66684)
@@ -0,0 +1,141 @@
+/*
+Copyright (c) 2005 JSON.org
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The Software shall be used for Good, not Evil.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+/*
+    The global object JSON contains two methods.
+
+    JSON.stringify(value) takes a JavaScript value and produces a JSON text.
+    The value must not be cyclical.
+
+    JSON.parse(text) takes a JSON text and produces a JavaScript value. It will
+    return false if there is an error.
+*/
+var JSON = function () {
+    var m = {
+            '\b': '\\b',
+            '\t': '\\t',
+            '\n': '\\n',
+            '\f': '\\f',
+            '\r': '\\r',
+            '"' : '\\"',
+            '\\': '\\\\'
+        },
+        s = {
+            'boolean': function (x) {
+                return String(x);
+            },
+            number: function (x) {
+                return isFinite(x) ? String(x) : 'null';
+            },
+            string: function (x) {
+                if (/["\\\x00-\x1f]/.test(x)) {
+                    x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
+                        var c = m[b];
+                        if (c) {
+                            return c;
+                        }
+                        c = b.charCodeAt();
+                        return '\\u00' +
+                            Math.floor(c / 16).toString(16) +
+                            (c % 16).toString(16);
+                    });
+                }
+                return '"' + x + '"';
+            },
+            object: function (x) {
+                if (x) {
+                    var a = [], b, f, i, l, v;
+                    if (x instanceof Array) {
+                        a[0] = '[';
+                        l = x.length;
+                        for (i = 0; i < l; i += 1) {
+                            v = x[i];
+                            f = s[typeof v];
+                            if (f) {
+                                v = f(v);
+                                if (typeof v == 'string') {
+                                    if (b) {
+                                        a[a.length] = ',';
+                                    }
+                                    a[a.length] = v;
+                                    b = true;
+                                }
+                            }
+                        }
+                        a[a.length] = ']';
+                    } else if (typeof x.hasOwnProperty === 'function') {
+                        a[0] = '{';
+                        for (i in x) {
+                            if (x.hasOwnProperty(i)) {
+                                v = x[i];
+                                f = s[typeof v];
+                                if (f) {
+                                    v = f(v);
+                                    if (typeof v == 'string') {
+                                        if (b) {
+                                            a[a.length] = ',';
+                                        }
+                                        a.push(s.string(i), ':', v);
+                                        b = true;
+                                    }
+                                }
+                            }
+                        }
+                        a[a.length] = '}';
+                    } else {
+                        return;
+                    }
+                    return a.join('');
+                }
+                return 'null';
+            }
+        };
+    return {
+        copyright: '(c)2005 JSON.org',
+        license: 'http://www.JSON.org/license.html',
+/*
+    Stringify a JavaScript value, producing a JSON text.
+*/
+        stringify: function (v) {
+            var f = s[typeof v];
+            if (f) {
+                v = f(v);
+                if (typeof v == 'string') {
+                    return v;
+                }
+            }
+            return null;
+        },
+/*
+    Parse a JSON text, producing a JavaScript value.
+    It returns false if there is a syntax error.
+*/
+        parse: function (text) {
+            try {
+                return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
+                        text.replace(/"(\\.|[^"\\])*"/g, ''))) &&
+                    eval('(' + text + ')');
+            } catch (e) {
+                return false;
+            }
+        }
+    };
+}();
\ No newline at end of file

Modified: zope3org/trunk/src/zorg/live/server.py
===================================================================
--- zope3org/trunk/src/zorg/live/server.py	2006-04-08 15:27:01 UTC (rev 66683)
+++ zope3org/trunk/src/zorg/live/server.py	2006-04-08 15:40:36 UTC (rev 66684)
@@ -166,7 +166,7 @@
         reactor.callLater(self.idleInterval, self.onIdle)
 
  
-    def returnResult(self, output, headers=None) :
+    def returnResult(self, output, headers=None, json=None) :
         """ Writes the result to the deferred response and closes
             the output stream.
         """
@@ -175,7 +175,10 @@
                         ("Connection", "close"),
                         ('content-type', 'text/html;charset=utf-8'), 
                         ('content-length', len(output))]
-                                        
+        
+        if json :
+            headers.append(('X-JSON', json))    
+            
         print "***LivePage result", self.num, len(output), "bytes", self.uuid, headers
         print "Output", output
         
@@ -203,7 +206,7 @@
         client = self.manager.get(self.uuid, None)
         if client is None : # Uups, the client has gone in the meanwhile
             error = ErrorEvent(description="Unexpected timeout")
-            return self.returnResult(error.toJSON())        
+            return self.returnResult("json", json=error.toJSON())        
             
         r = self.result
         if r :
@@ -214,10 +217,10 @@
             
         event = client.nextEvent()
         if event is not None :
-            return self.returnResult(event.toJSON())
+            return self.returnResult("json", json=event.toJSON())
         
         if time.time() > self.expires :
-            return self.returnResult(IdleEvent().toJSON())
+            return self.returnResult("json", json=IdleEvent().toJSON())
         
         reactor.callLater(self.idleInterval, self.onIdle)
  



More information about the Checkins mailing list