]> git.parisson.com Git - django-jqchat.git/commitdiff
fix wrong html link parsing
authoryomguy <yomguy@parisson.com>
Mon, 4 Feb 2013 13:34:58 +0000 (14:34 +0100)
committeryomguy <yomguy@parisson.com>
Mon, 4 Feb 2013 13:34:58 +0000 (14:34 +0100)
jqchat/static/jqchat/jqchat.js
jqchat/views.py

index bd64a34e09d82cd4210133e50ac9f95c80a88671..e26291b2ac91cd6dfbdc49e595ef8b6b953397ea 100644 (file)
@@ -42,13 +42,9 @@ $(document).ajaxSend(function(event, xhr, settings) {
 
 jQuery.fn.urlize = function( base ) {
     var x = this.html();
-    list = x.match( /\b(http:\/\/|www\.|http:\/\/www\.)[^ ]{2,100}\b/g );
-    if ( list ) {
-        for ( i = 0; i < list.length; i++ ) {
-            x = x.replace( list[i], "<a href='" + base + escape( list[i] ) + "'>"+ list[i] + "</a>" );
-        }
-        this.html(x);
-    }
+    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
+    var text = x.replace(exp,"<a href='$1'>$1</a>");
+    this.html(text);
 };
 
 // Chat client code.
index 4689f2507f1184a42b1a89b7c07788adb9ebb769..b0fb8caabd11dd1871b68e44290d19ab772703ac 100644 (file)
@@ -17,7 +17,7 @@ except:
     DATE_FORMAT = "D-H:i:s"
 
 # How many messages to retrieve at most.
-JQCHAT_DISPLAY_COUNT = getattr(settings, 'JQCHAT_DISPLAY_COUNT', 100) 
+JQCHAT_DISPLAY_COUNT = getattr(settings, 'JQCHAT_DISPLAY_COUNT', 100)
 
 #------------------------------------------------------------------------------
 @login_required
@@ -56,8 +56,8 @@ class Ajax(object):
     On the first call, this should be set to 0, thereafter the server will supply a new system time
     on each call.
     - the room ID number.
-    
-    Requests that include new data for the server (e.g. new messages) should be sent as a POST and 
+
+    Requests that include new data for the server (e.g. new messages) should be sent as a POST and
     contain the following extra args:
     - an action code, a short string describing the type of data sent.
     - message, a string containing the message sent by the user.
@@ -66,7 +66,7 @@ class Ajax(object):
      1: got new data.
      2: no new data, nothing to update.
 
-    This code is written as a class, the idea being that implementations of a chat window will 
+    This code is written as a class, the idea being that implementations of a chat window will
     have extra features, so these will be coded as derived classes.
     Included below is a basic example for updating the room's description field.
 
@@ -78,7 +78,7 @@ class Ajax(object):
 
         if not request.user.is_authenticated():
             return HttpResponseBadRequest('You need to be logged in to access the chat system.')
-    
+
         StatusCode = 0 # Default status code is 0 i.e. no new data.
         self.request = request
         try:
@@ -91,10 +91,10 @@ class Ajax(object):
         if self.request.method == "POST":
             # User has sent new data.
             action = self.request.POST['action']
-    
+
             if action == 'postmsg':
                 msg_text = self.request.POST['message']
-    
+
                 if len(msg_text.strip()) > 0: # Ignore empty strings.
                     Message.objects.create_message(self.request.user, self.ThisRoom, escape(msg_text))
         else:
@@ -107,14 +107,14 @@ class Ajax(object):
             user_tz = self.request.user.account_set.all()[0].timezone
         except:
             user_tz = settings.TIME_ZONE
-    
+
         # Extra JSON string to be spliced into the response.
         CustomPayload = self.ExtraHandling()
         if CustomPayload:
             StatusCode = 1
-    
+
         # Get new messages - do this last in case the ExtraHandling has itself generated
-        # new messages. 
+        # new messages.
         NewMessages = self.ThisRoom.message_set.filter(unix_timestamp__gt=self.request_time)
         if NewMessages:
             StatusCode = 1
@@ -123,7 +123,7 @@ class Ajax(object):
         l = len(NewMessages)
         if l > JQCHAT_DISPLAY_COUNT:
             NewMessages = NewMessages[l-JQCHAT_DISPLAY_COUNT:]
-            
+
         response =  render_to_response('jqchat/chat_payload.json',
                                   {'current_unix_timestamp': time.time(),
                                    'NewMessages': NewMessages,
@@ -141,13 +141,13 @@ class Ajax(object):
     def ExtraHandling(self):
         """We might want to receive/send extra data in the Ajax calls.
         This function is there to be overriden in child classes.
-        
-        Basic usage is to generate the JSON that then gets spliced into the main JSON 
+
+        Basic usage is to generate the JSON that then gets spliced into the main JSON
         response.
-        
+
         """
         return None
-        
+
 
 BasicAjaxHandler = Ajax()
 
@@ -171,7 +171,7 @@ class DescriptionAjax(Ajax):
         # If yes, return an extra field to be tagged on to the JSON returned to the client.
         if self.ThisRoom.description and self.ThisRoom.description_modified > self.request_time:
             return ',\n        "description": "%s"' % self.ThisRoom.description
-        
+
         return None
 
 WindowWithDescriptionAjaxHandler = DescriptionAjax()