changeset 848:7dafa5ee809a

browser: replace re module usage by pure javascript
author souliane <souliane@mailoo.org>
date Fri, 15 Jan 2016 16:33:08 +0100
parents 330db23d4a44
children d32b754265a0
files src/browser/sat_browser/html_tools.py src/browser/sat_browser/register.py src/browser/sat_browser/strings.py
diffstat 3 files changed, 43 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/browser/sat_browser/html_tools.py	Fri Jan 15 12:30:21 2016 +0100
+++ b/src/browser/sat_browser/html_tools.py	Fri Jan 15 16:33:08 2016 +0100
@@ -20,7 +20,7 @@
 from sat_frontends.tools import xmltools
 
 import nativedom
-import re
+from __pyjamas__ import JS
 
 dom = nativedom.NativeDOM()
 
@@ -32,10 +32,7 @@
 
 def html_strip(html):
     """Strip leading/trailing white spaces, HTML line breaks and &nbsp; sequences."""
-    cleaned = re.sub(r"^(<br/?>|&nbsp;|\s)+", "", html)
-    cleaned = re.sub(r"(<br/?>|&nbsp;|\s)+$", "", cleaned)
-    return cleaned
-
+    JS("""return html.replace(/(^(<br\/?>|&nbsp;|\s)+)|((<br\/?>|&nbsp;|\s)+$)/g, "");""")
 
 def inlineRoot(xhtml):
     """ make root element inline """
--- a/src/browser/sat_browser/register.py	Fri Jan 15 12:30:21 2016 +0100
+++ b/src/browser/sat_browser/register.py	Fri Jan 15 16:33:08 2016 +0100
@@ -40,7 +40,7 @@
 from pyjamas.ui.KeyboardListener import KEY_ENTER
 from pyjamas.Timer import Timer
 
-import re
+from __pyjamas__ import JS
 
 from constants import Const as C
 
@@ -178,7 +178,7 @@
         pass
 
     def onLogin(self, button):
-        if not re.match(r'^[a-z0-9_-]+(@[a-z0-9_-]+\.[a-z0-9_-]+)?$', self.login_box.getText(), re.IGNORECASE):
+        if not self.checkJID(self.login_box.getText()):
             self.login_warning_msg.setHTML('Invalid login, valid characters<br>are a-z A-Z 0-9 _ - or a bare JID')
         else:
             self.submit_type.setValue('login')
@@ -187,9 +187,9 @@
     def onRegister(self, button):
         # XXX: for now libervia forces the creation to lower case
         self.register_login_box.setText(self.register_login_box.getText().lower())
-        if not re.match(r'^[a-z0-9_-]+$', self.register_login_box.getText(), re.IGNORECASE):
+        if not self.checkLogin(self.register_login_box.getText()):
             self.register_warning_msg.setHTML(_('Invalid login, valid characters<br>are a-z A-Z 0-9 _ -'))
-        elif not re.match(r'^[a-z0-9_-]+@[a-z0-9_-]+\.[a-z0-9_-]+$', self.email_box.getText(), re.IGNORECASE):
+        elif not self.checkEmail(self.email_box.getText()):
             self.register_warning_msg.setHTML(_('Invalid email address'))
         elif len(self.register_pass_box.getText()) < C.PASSWORD_MIN_LENGTH:
             self.register_warning_msg.setHTML(_('Your password must contain<br>at least %d characters.') % C.PASSWORD_MIN_LENGTH)
@@ -235,6 +235,33 @@
         else:
             Window.alert(_("An error occurred and we couldn't process your request. Please report the following error name to the administrators of your network: '%s'" % result))
 
+    def checkLogin(self, text):
+        """Check if the given text is a valid login
+
+        @param text (unicode)
+        @return bool
+        """
+        # FIXME: Pyjamas re module is not stable so we use pure JS instead
+        JS("""return /^(\w|-)+$/.test(text);""")
+
+    def checkEmail(self, text):
+        """Check if the given text is a valid email address.
+
+        @param text (unicode)
+        @return bool
+        """
+        # FIXME: Pyjamas re module is not stable so we use pure JS instead
+        JS("""return /^(\w|-)+@(\w|-)+\.(\w|-)+$/.test(text);""")
+
+    def checkJID(self, text):
+        """Check if the given text is a valid JID.
+
+        @param text (unicode)
+        @return bool
+        """
+        # FIXME: Pyjamas re module is not stable so we use pure JS instead
+        JS("""return /^(\w|-)+(@(\w|-)+\.(\w|-)+)?$/.test(text);""")
+
 
 class RegisterBox(PopupPanel):
 
--- a/src/browser/sat_browser/strings.py	Fri Jan 15 12:30:21 2016 +0100
+++ b/src/browser/sat_browser/strings.py	Fri Jan 15 16:33:08 2016 +0100
@@ -17,7 +17,6 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-import re
 from __pyjamas__ import JS
 
 
@@ -50,25 +49,25 @@
     @param string (unicode): text to process
     @param new_target (bool): if True, make the link open in a new window
     """
-    # XXX: Workaround for a pyjamas bug with regex, base method in sat.frontends.tools.strings
+    # FIXME: Workaround for a pyjamas bug with regex, base method in sat.frontends.tools.strings
     # In some case, Pyjamas' re module get crazy and freeze browsers (tested with Iceweasel and Chromium).
     # we use javascript as a workaround
     # This method is inspired from https://stackoverflow.com/questions/1500260/detect-urls-in-text-with-javascript
-    target = ' target="_blank"' if new_target else ''
     JS("""var urlRegex = /(https?:\/\/[^\s]+)/g;
+    var target = new_target ? ' target="_blank"' : '';
     return text.replace(urlRegex, function(url) {
         return '<a href="' + url + '"' + target + ' class="url">' + url + '</a>';
     })""")
 
 
-def addURLToImage(string):
+def addURLToImage(text):
     """Check a XHTML text for what looks like an imageURL and make it clickable.
 
-    @param string (unicode): text to process
+    @param text (unicode): text to process
     """
-    # XXX: Workaround for a pyjamas bug with regex, base method in sat.frontends.tools.strings
-    def repl(match):
-        url = match.group(1)
-        return '<a href="%s" target="_blank">%s</a>' % (url, match.group(0))
-    pattern = r"""<img[^>]* src="([^"]+)"[^>]*>"""
-    return re.sub(pattern, repl, string)
+    # FIXME: Pyjamas re module is not stable so we use pure JS instead, base method in sat.frontends.tools.strings
+    JS("""var imgRegex = /<img[^>]* src="([^"]+)"[^>]*>/g;
+    return text.replace(imgRegex, function(img, src) {
+        return '<a href="' + src + '" target="_blank">' + img + '</a>';
+    })""")
+