comparison src/browser/sat_browser/strings.py @ 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 73cc4658f431
children d32b754265a0
comparison
equal deleted inserted replaced
847:330db23d4a44 848:7dafa5ee809a
15 # GNU Affero General Public License for more details. 15 # GNU Affero General Public License for more details.
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 import re
21 from __pyjamas__ import JS 20 from __pyjamas__ import JS
22 21
23 22
24 def getURLParams(url): 23 def getURLParams(url):
25 """This comes from pyjamas.Location.makeUrlDict with a small change 24 """This comes from pyjamas.Location.makeUrlDict with a small change
48 """Check a text for what looks like an URL and make it clickable. 47 """Check a text for what looks like an URL and make it clickable.
49 48
50 @param string (unicode): text to process 49 @param string (unicode): text to process
51 @param new_target (bool): if True, make the link open in a new window 50 @param new_target (bool): if True, make the link open in a new window
52 """ 51 """
53 # XXX: Workaround for a pyjamas bug with regex, base method in sat.frontends.tools.strings 52 # FIXME: Workaround for a pyjamas bug with regex, base method in sat.frontends.tools.strings
54 # In some case, Pyjamas' re module get crazy and freeze browsers (tested with Iceweasel and Chromium). 53 # In some case, Pyjamas' re module get crazy and freeze browsers (tested with Iceweasel and Chromium).
55 # we use javascript as a workaround 54 # we use javascript as a workaround
56 # This method is inspired from https://stackoverflow.com/questions/1500260/detect-urls-in-text-with-javascript 55 # This method is inspired from https://stackoverflow.com/questions/1500260/detect-urls-in-text-with-javascript
57 target = ' target="_blank"' if new_target else ''
58 JS("""var urlRegex = /(https?:\/\/[^\s]+)/g; 56 JS("""var urlRegex = /(https?:\/\/[^\s]+)/g;
57 var target = new_target ? ' target="_blank"' : '';
59 return text.replace(urlRegex, function(url) { 58 return text.replace(urlRegex, function(url) {
60 return '<a href="' + url + '"' + target + ' class="url">' + url + '</a>'; 59 return '<a href="' + url + '"' + target + ' class="url">' + url + '</a>';
61 })""") 60 })""")
62 61
63 62
64 def addURLToImage(string): 63 def addURLToImage(text):
65 """Check a XHTML text for what looks like an imageURL and make it clickable. 64 """Check a XHTML text for what looks like an imageURL and make it clickable.
66 65
67 @param string (unicode): text to process 66 @param text (unicode): text to process
68 """ 67 """
69 # XXX: Workaround for a pyjamas bug with regex, base method in sat.frontends.tools.strings 68 # FIXME: Pyjamas re module is not stable so we use pure JS instead, base method in sat.frontends.tools.strings
70 def repl(match): 69 JS("""var imgRegex = /<img[^>]* src="([^"]+)"[^>]*>/g;
71 url = match.group(1) 70 return text.replace(imgRegex, function(img, src) {
72 return '<a href="%s" target="_blank">%s</a>' % (url, match.group(0)) 71 return '<a href="' + src + '" target="_blank">' + img + '</a>';
73 pattern = r"""<img[^>]* src="([^"]+)"[^>]*>""" 72 })""")
74 return re.sub(pattern, repl, string) 73