comparison src/browser/sat_browser/strings.py @ 849:d32b754265a0

browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
author souliane <souliane@mailoo.org>
date Fri, 15 Jan 2016 16:34:37 +0100
parents 7dafa5ee809a
children 2aaac0605ae2
comparison
equal deleted inserted replaced
848:7dafa5ee809a 849:d32b754265a0
69 JS("""var imgRegex = /<img[^>]* src="([^"]+)"[^>]*>/g; 69 JS("""var imgRegex = /<img[^>]* src="([^"]+)"[^>]*>/g;
70 return text.replace(imgRegex, function(img, src) { 70 return text.replace(imgRegex, function(img, src) {
71 return '<a href="' + src + '" target="_blank">' + img + '</a>'; 71 return '<a href="' + src + '" target="_blank">' + img + '</a>';
72 })""") 72 })""")
73 73
74 def fixXHTMLLinks(xhtml):
75 """Add http:// if the scheme is missing and force opening in a new window.
76
77 @param string (unicode): XHTML Content
78 """
79 # FIXME: Pyjamas re module is not stable so we use pure JS instead, base method in sat.frontends.tools.strings
80 JS("""var subs = [];
81 var tag_re = /<a(?: \w+="[^"]*")* ?\/?>/g;
82 var result;
83 while ((result = tag_re.exec(xhtml)) !== null) {
84 tag = result[0];
85 var link_result = /href="([^"]*)"/.exec(tag);
86 if (link_result) { // found a link
87 var link = link_result[0];
88 var url = link_result[1];
89 if (! /target="([^"]*)"/.test(tag)) { // no target
90 subs.push([tag, '<a target="_blank"' + tag.slice(2, tag.length)]);
91 }
92 if (! /^\w+:\/\//.test(url)) { // no scheme
93 subs.push([link, 'href="http://' + url + '"']);
94 }
95 }
96 }
97 for (i in subs) {
98 xhtml = xhtml.replace(subs[i][0], subs[i][1]);
99 }
100 """)
101 return xhtml