Updated XHTML-IM stanza with documentation and PEP8 style.

This commit is contained in:
Lance Stout 2010-08-03 17:58:18 -04:00
parent 18683d2b75
commit 183a3f1b87
2 changed files with 71 additions and 26 deletions

View File

@ -5,31 +5,76 @@
See the file LICENSE for copying permission. See the file LICENSE for copying permission.
""" """
from .. xmlstream.stanzabase import registerStanzaPlugin, ElementBase, ET
from sleekxmpp.stanza import Message
from sleekxmpp.xmlstream.stanzabase import registerStanzaPlugin
from sleekxmpp.xmlstream.stanzabase import ElementBase, ET
class HTMLIM(ElementBase): class HTMLIM(ElementBase):
namespace = 'http://jabber.org/protocol/xhtml-im'
name = 'html'
plugin_attrib = 'html'
interfaces = set(('html',))
plugin_attrib_map = set()
plugin_xml_map = set()
def setHtml(self, html): """
if isinstance(html, str): XEP-0071: XHTML-IM defines a method for embedding XHTML content
html = ET.XML(html) within a <message> stanza so that lightweight markup can be used
if html.tag != '{http://www.w3.org/1999/xhtml}body': to format the message contents and to create links.
body = ET.Element('{http://www.w3.org/1999/xhtml}body')
body.append(html) Only a subset of XHTML is recommended for use with XHTML-IM.
self.xml.append(body) See the full spec at 'http://xmpp.org/extensions/xep-0071.html'
else: for more information.
self.xml.append(html)
Example stanza:
def getHtml(self): <message to="user@example.com">
html = self.xml.find('{http://www.w3.org/1999/xhtml}body') <body>Non-html message content.</body>
if html is None: return '' <html xmlns="http://jabber.org/protocol/xhtml-im">
return html <body xmlns="http://www.w3.org/1999/xhtml">
<p><b>HTML!</b></p>
def delHtml(self): </body>
if self.parent is not None: </html>
self.parent().xml.remove(self.xml) </message>
Stanza Interface:
body -- The contents of the HTML body tag.
Methods:
getBody -- Return the HTML body contents.
setBody -- Set the HTML body contents.
delBody -- Remove the HTML body contents.
"""
namespace = 'http://jabber.org/protocol/xhtml-im'
name = 'html'
interfaces = set(('body',))
plugin_attrib = name
def setBody(self, html):
"""
Set the contents of the HTML body.
Arguments:
html -- Either a string or XML object. If the top level
element is not <body> with a namespace of
'http://www.w3.org/1999/xhtml', it will be wrapped.
"""
if isinstance(html, str):
html = ET.XML(html)
if html.tag != '{http://www.w3.org/1999/xhtml}body':
body = ET.Element('{http://www.w3.org/1999/xhtml}body')
body.append(html)
self.xml.append(body)
else:
self.xml.append(html)
def getBody(self):
"""Return the contents of the HTML body."""
html = self.xml.find('{http://www.w3.org/1999/xhtml}body')
if html is None:
return ''
return html
def delBody(self):
"""Remove the HTML body contents."""
if self.parent is not None:
self.parent().xml.remove(self.xml)
registerStanzaPlugin(Message, HTMLIM)

View File

@ -32,7 +32,7 @@ class TestMessageStanzas(SleekTest):
msg['type'] = 'chat' msg['type'] = 'chat'
p = ET.Element('{http://www.w3.org/1999/xhtml}p') p = ET.Element('{http://www.w3.org/1999/xhtml}p')
p.text = "This is the htmlim message" p.text = "This is the htmlim message"
msg['html']['html'] = p msg['html']['body'] = p
self.checkMessage(msg, """ self.checkMessage(msg, """
<message to="fritzy@netflint.net/sleekxmpp" type="chat"> <message to="fritzy@netflint.net/sleekxmpp" type="chat">
<body>this is the plaintext message</body> <body>this is the plaintext message</body>