# HG changeset patch # User souliane # Date 1452871988 -3600 # Node ID 7dafa5ee809a584da5a0d78d7ac0cf6090ba9ea2 # Parent 330db23d4a444be54f1b660d9bf64e1fdf00cdc3 browser: replace re module usage by pure javascript diff -r 330db23d4a44 -r 7dafa5ee809a src/browser/sat_browser/html_tools.py --- 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   sequences.""" - cleaned = re.sub(r"^(
| |\s)+", "", html) - cleaned = re.sub(r"(
| |\s)+$", "", cleaned) - return cleaned - + JS("""return html.replace(/(^(| |\s)+)|((| |\s)+$)/g, "");""") def inlineRoot(xhtml): """ make root element inline """ diff -r 330db23d4a44 -r 7dafa5ee809a src/browser/sat_browser/register.py --- 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
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
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
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): diff -r 330db23d4a44 -r 7dafa5ee809a src/browser/sat_browser/strings.py --- 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 . -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 '' + url + ''; })""") -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 '%s' % (url, match.group(0)) - pattern = r"""]* 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 = /]* src="([^"]+)"[^>]*>/g; + return text.replace(imgRegex, function(img, src) { + return '' + img + ''; + })""") +