comparison src/browser/sat_browser/html_tools.py @ 684:e876f493dccc

browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation: - some MUC handlers are no more needed, the presence handler is enough - move the chat states logic to quick_frontend - display MUC games symbols - remove classes contact_list.ContactsPanel, contact_panel.Occupant and contact_panel.OccupantsList - move buildPresenceStyle and setPresenceStyle to html_tools - fixes games menu callback
author souliane <souliane@mailoo.org>
date Wed, 18 Mar 2015 10:17:04 +0100
parents f030491cff75
children 9877607c719a
comparison
equal deleted inserted replaced
683:801eb94aa869 684:e876f493dccc
27 27
28 def html_sanitize(html): 28 def html_sanitize(html):
29 """Naive sanitization of HTML""" 29 """Naive sanitization of HTML"""
30 return html.replace('<', '&lt;').replace('>', '&gt;') 30 return html.replace('<', '&lt;').replace('>', '&gt;')
31 31
32
32 def html_strip(html): 33 def html_strip(html):
33 """Strip leading/trailing white spaces, HTML line breaks and &nbsp; sequences.""" 34 """Strip leading/trailing white spaces, HTML line breaks and &nbsp; sequences."""
34 cleaned = re.sub(r"^(<br/?>|&nbsp;|\s)+", "", html) 35 cleaned = re.sub(r"^(<br/?>|&nbsp;|\s)+", "", html)
35 cleaned = re.sub(r"(<br/?>|&nbsp;|\s)+$", "", cleaned) 36 cleaned = re.sub(r"(<br/?>|&nbsp;|\s)+$", "", cleaned)
36 return cleaned 37 return cleaned
37 38
39
38 def inlineRoot(xhtml): 40 def inlineRoot(xhtml):
39 """ make root element inline """ 41 """ make root element inline """
40 doc = dom.parseString(xhtml) 42 doc = dom.parseString(xhtml)
41 return xmltools.inlineRoot(doc) 43 return xmltools.inlineRoot(doc)
42 44
45
43 def convertNewLinesToXHTML(text): 46 def convertNewLinesToXHTML(text):
44 return text.replace('\n', '<br/>') 47 return text.replace('\n', '<br/>')
48
45 49
46 def XHTML2Text(xhtml): 50 def XHTML2Text(xhtml):
47 """Helper method to apply both html_sanitize and convertNewLinesToXHTML""" 51 """Helper method to apply both html_sanitize and convertNewLinesToXHTML"""
48 return convertNewLinesToXHTML(html_sanitize(xhtml)) 52 return convertNewLinesToXHTML(html_sanitize(xhtml))
53
54
55 def buildPresenceStyle(presence, base_style=None):
56 """Return the CSS classname to be used for displaying the given presence information.
57
58 @param presence (unicode): presence is a value in ('', 'chat', 'away', 'dnd', 'xa')
59 @param base_style (unicode): base classname
60 @return: unicode
61 """
62 if not base_style:
63 base_style = "contactLabel"
64 return '%s-%s' % (base_style, presence or 'connected')
65
66
67 def setPresenceStyle(widget, presence, base_style=None):
68 """
69 Set the CSS style of a contact's element according to its presence.
70
71 @param widget (Widget): the UI element of the contact
72 @param presence (unicode): a value in ("", "chat", "away", "dnd", "xa").
73 @param base_style (unicode): the base name of the style to apply
74 """
75 if not hasattr(widget, 'presence_style'):
76 widget.presence_style = None
77 style = buildPresenceStyle(presence, base_style)
78 if style == widget.presence_style:
79 return
80 if widget.presence_style is not None:
81 widget.removeStyleName(widget.presence_style)
82 widget.addStyleName(style)
83 widget.presence_style = style