Mercurial > libervia-web
annotate browser_side/tools.py @ 252:b77940d8a9bf
browser_side: isolate the basic stuff of ContactList in the new GenericContactList class
author | souliane <souliane@mailoo.org> |
---|---|
date | Fri, 08 Nov 2013 17:07:27 +0100 |
parents | d7c41c84d062 |
children | aebb96bfa8d1 |
rev | line source |
---|---|
31
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 """ |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 Libervia: a Salut à Toi frontend |
165 | 6 Copyright (C) 2011, 2012, 2013 Jérôme Poisson <goffi@goffi.org> |
31
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 This program is free software: you can redistribute it and/or modify |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 it under the terms of the GNU Affero General Public License as published by |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 the Free Software Foundation, either version 3 of the License, or |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 (at your option) any later version. |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 This program is distributed in the hope that it will be useful, |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 GNU Affero General Public License for more details. |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 You should have received a copy of the GNU Affero General Public License |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
196
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
22 from pyjamas.ui.DragWidget import DragWidget |
217
f7ec248192de
browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents:
196
diff
changeset
|
23 import re |
246
d7c41c84d062
browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents:
217
diff
changeset
|
24 from nativedom import NativeDOM |
d7c41c84d062
browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents:
217
diff
changeset
|
25 from sat.tools.frontends import xml |
d7c41c84d062
browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents:
217
diff
changeset
|
26 |
d7c41c84d062
browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents:
217
diff
changeset
|
27 dom = NativeDOM() |
196
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
28 |
31
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 def html_sanitize(html): |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 """Naive sanitization of HTML""" |
189
67365f17069e
browser side: removed "\n" -> <br> conversion in html_sanitize, and use "white-space: pre" CSS property in chat messages instead (thx Link Mauve !).
Goffi <goffi@goffi.org>
parents:
182
diff
changeset
|
31 return html.replace('<','<').replace('>','>') |
196
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
32 |
217
f7ec248192de
browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents:
196
diff
changeset
|
33 |
246
d7c41c84d062
browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents:
217
diff
changeset
|
34 def inlineRoot(xhtml): |
d7c41c84d062
browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents:
217
diff
changeset
|
35 """ make root element inline """ |
d7c41c84d062
browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents:
217
diff
changeset
|
36 doc = dom.parseString(xhtml) |
d7c41c84d062
browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents:
217
diff
changeset
|
37 return xml.inlineRoot(doc) |
d7c41c84d062
browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents:
217
diff
changeset
|
38 |
217
f7ec248192de
browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents:
196
diff
changeset
|
39 def addURLToText(string): |
f7ec248192de
browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents:
196
diff
changeset
|
40 """Check a text for what looks like an URL and make it clickable. Regexp |
f7ec248192de
browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents:
196
diff
changeset
|
41 from http://daringfireball.net/2010/07/improved_regex_for_matching_urls""" |
f7ec248192de
browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents:
196
diff
changeset
|
42 |
f7ec248192de
browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents:
196
diff
changeset
|
43 def repl(match): |
f7ec248192de
browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents:
196
diff
changeset
|
44 url = match.group(0) |
f7ec248192de
browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents:
196
diff
changeset
|
45 if not re.match(r"""[a-z]{3,}://|mailto:|xmpp:""", url): |
f7ec248192de
browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents:
196
diff
changeset
|
46 url = "http://" + url |
f7ec248192de
browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents:
196
diff
changeset
|
47 return '<a href="%s" target="_blank" class="url">%s</a>' % (url, match.group(0)) |
f7ec248192de
browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents:
196
diff
changeset
|
48 pattern = r"""(?i)\b((?:[a-z]{3,}://|(www|ftp)\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/|mailto:|xmpp:)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?]))""" |
f7ec248192de
browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents:
196
diff
changeset
|
49 return re.sub(pattern, repl, string) |
f7ec248192de
browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents:
196
diff
changeset
|
50 |
f7ec248192de
browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents:
196
diff
changeset
|
51 |
196
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
52 class DragLabel(DragWidget): |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
53 |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
54 def __init__(self, text, _type): |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
55 DragWidget.__init__(self) |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
56 self._text = text |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
57 self._type = _type |
217
f7ec248192de
browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents:
196
diff
changeset
|
58 |
196
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
59 def onDragStart(self, event): |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
60 dt = event.dataTransfer |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
61 dt.setData('text/plain', "%s\n%s" % (self._text, self._type)) |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
62 dt.setDragImage(self.getElement(), 15, 15) |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
63 |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
64 class LiberviaDragWidget(DragLabel): |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
65 """ A DragLabel which keep the widget being dragged as class value """ |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
66 current = None # widget currently dragged |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
67 |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
68 def __init__(self, text, _type, widget): |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
69 DragLabel.__init__(self, text, _type) |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
70 self.widget = widget |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
71 |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
72 def onDragStart(self, event): |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
73 LiberviaDragWidget.current = self.widget |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
74 DragLabel.onDragStart(self, event) |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
75 |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
76 def onDragEnd(self, event): |
c2639c9f86ea
Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents:
189
diff
changeset
|
77 LiberviaDragWidget.current = None |