annotate browser/sat_browser/html_tools.py @ 1159:4e716967893a

page (u): fixed blog posting
author Goffi <goffi@goffi.org>
date Sat, 23 Feb 2019 21:05:48 +0100
parents 3048bd137aaf
children
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
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
4 # Libervia: a Salut à Toi frontend
1144
2af117bfe6cc dates update
Goffi <goffi@goffi.org>
parents: 1124
diff changeset
5 # Copyright (C) 2011-2019 Jérôme Poisson <goffi@goffi.org>
31
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
6
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
7 # This program is free software: you can redistribute it and/or modify
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
10 # (at your option) any later version.
31
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
11
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
12 # This program is distributed in the hope that it will be useful,
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
15 # GNU Affero General Public License for more details.
31
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
16
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 332
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
31
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
19
449
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 405
diff changeset
20 from sat_frontends.tools import xmltools
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 405
diff changeset
21
467
97c72fe4a5f2 browser_side: import fixes:
Goffi <goffi@goffi.org>
parents: 449
diff changeset
22 import nativedom
848
7dafa5ee809a browser: replace re module usage by pure javascript
souliane <souliane@mailoo.org>
parents: 818
diff changeset
23 from __pyjamas__ import JS
246
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
24
467
97c72fe4a5f2 browser_side: import fixes:
Goffi <goffi@goffi.org>
parents: 449
diff changeset
25 dom = nativedom.NativeDOM()
196
c2639c9f86ea Browser Side: Widgets can now be moved, header (title bar) is draggable:
Goffi <goffi@goffi.org>
parents: 189
diff changeset
26
279
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
27
31
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
28 def html_sanitize(html):
cb07078f8d6f browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff changeset
29 """Naive sanitization of HTML"""
279
2d6bd975a72d browser_side: set your own presence status and display those of others
souliane <souliane@mailoo.org>
parents: 276
diff changeset
30 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
31
349
f488692c4903 browser_side: LightTextEditor inheritates from BaseTextEditor + display URL in the status
souliane <souliane@mailoo.org>
parents: 347
diff changeset
32 def html_strip(html):
f488692c4903 browser_side: LightTextEditor inheritates from BaseTextEditor + display URL in the status
souliane <souliane@mailoo.org>
parents: 347
diff changeset
33 """Strip leading/trailing white spaces, HTML line breaks and &nbsp; sequences."""
848
7dafa5ee809a browser: replace re module usage by pure javascript
souliane <souliane@mailoo.org>
parents: 818
diff changeset
34 JS("""return html.replace(/(^(<br\/?>|&nbsp;|\s)+)|((<br\/?>|&nbsp;|\s)+$)/g, "");""")
684
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
35
246
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
36 def inlineRoot(xhtml):
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
37 """ make root element inline """
d7c41c84d062 browser side: use of inlineRoot to display XHTML chat text
Goffi <goffi@goffi.org>
parents: 217
diff changeset
38 doc = dom.parseString(xhtml)
332
6abd099c7007 browser side: sat_frontends.tools.xml is now called xmltools
Goffi <goffi@goffi.org>
parents: 306
diff changeset
39 return xmltools.inlineRoot(doc)
405
41b8b96f2248 browser_side: display new lines in microblogs (convert '\n' to '<br/>')
souliane <souliane@mailoo.org>
parents: 370
diff changeset
40
684
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
41
405
41b8b96f2248 browser_side: display new lines in microblogs (convert '\n' to '<br/>')
souliane <souliane@mailoo.org>
parents: 370
diff changeset
42 def convertNewLinesToXHTML(text):
803
89a0cd2aa763 browser_side: convertNewLinesToXHTML should only be called on raw text messages:
souliane <souliane@mailoo.org>
parents: 685
diff changeset
43 """Replace all the \n with <br/>"""
405
41b8b96f2248 browser_side: display new lines in microblogs (convert '\n' to '<br/>')
souliane <souliane@mailoo.org>
parents: 370
diff changeset
44 return text.replace('\n', '<br/>')
504
f030491cff75 browser side (XMLUI): fixed bad use of text instead of XHTML in dialogs
Goffi <goffi@goffi.org>
parents: 467
diff changeset
45
684
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
46
504
f030491cff75 browser side (XMLUI): fixed bad use of text instead of XHTML in dialogs
Goffi <goffi@goffi.org>
parents: 467
diff changeset
47 def XHTML2Text(xhtml):
f030491cff75 browser side (XMLUI): fixed bad use of text instead of XHTML in dialogs
Goffi <goffi@goffi.org>
parents: 467
diff changeset
48 """Helper method to apply both html_sanitize and convertNewLinesToXHTML"""
f030491cff75 browser side (XMLUI): fixed bad use of text instead of XHTML in dialogs
Goffi <goffi@goffi.org>
parents: 467
diff changeset
49 return convertNewLinesToXHTML(html_sanitize(xhtml))
684
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
50
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
51
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
52 def buildPresenceStyle(presence, base_style=None):
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
53 """Return the CSS classname to be used for displaying the given presence information.
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
54
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
55 @param presence (unicode): presence is a value in ('', 'chat', 'away', 'dnd', 'xa')
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
56 @param base_style (unicode): base classname
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
57 @return: unicode
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
58 """
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
59 if not base_style:
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
60 base_style = "contactLabel"
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
61 return '%s-%s' % (base_style, presence or 'connected')
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
62
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
63
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
64 def setPresenceStyle(widget, presence, base_style=None):
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
65 """
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
66 Set the CSS style of a contact's element according to its presence.
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
67
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
68 @param widget (Widget): the UI element of the contact
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
69 @param presence (unicode): a value in ("", "chat", "away", "dnd", "xa").
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
70 @param base_style (unicode): the base name of the style to apply
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
71 """
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
72 if not hasattr(widget, 'presence_style'):
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
73 widget.presence_style = None
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
74 style = buildPresenceStyle(presence, base_style)
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
75 if style == widget.presence_style:
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
76 return
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
77 if widget.presence_style is not None:
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
78 widget.removeStyleName(widget.presence_style)
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
79 widget.addStyleName(style)
e876f493dccc browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
souliane <souliane@mailoo.org>
parents: 504
diff changeset
80 widget.presence_style = style