annotate browser_side/tools.py @ 234:d4e73d9140af

browser side: rich text: update to follow core implementation
author Goffi <goffi@goffi.org>
date Thu, 31 Oct 2013 17:54:10 +0100
parents f7ec248192de
children d7c41c84d062
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
9763dec220ed dates update
Goffi <goffi@goffi.org>
parents: 131
diff changeset
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
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
24
31
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
25 def html_sanitize(html):
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
26 """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
27 return html.replace('<','&lt;').replace('>','&gt;')
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
28
217
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
29
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
30 def addURLToText(string):
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
31 """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
32 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
33
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
34 def repl(match):
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
35 url = match.group(0)
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
36 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
37 url = "http://" + url
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
38 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
39 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
40 return re.sub(pattern, repl, string)
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
41
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
42
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
43 class DragLabel(DragWidget):
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
44
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
45 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
46 DragWidget.__init__(self)
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
47 self._text = text
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
48 self._type = _type
217
f7ec248192de browser_side: display clickable URLs in chat text
souliane <souliane@mailoo.org>
parents: 196
diff changeset
49
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
50 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
51 dt = event.dataTransfer
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
52 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
53 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
54
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
55 class LiberviaDragWidget(DragLabel):
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
56 """ 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
57 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
58
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
59 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
60 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
61 self.widget = widget
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
62
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
63 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
64 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
65 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
66
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
67 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
68 LiberviaDragWidget.current = None