Commit a335c9ec authored by Philipp Hörist's avatar Philipp Hörist

Revert "Refactor XHTML event code"

This reverts commit 05e74605
parent 05e74605
Pipeline #4440 passed with stages
in 33 seconds
......@@ -19,10 +19,8 @@ import logging
from nbxmpp.protocol import NodeProcessed
from nbxmpp.protocol import NS_DATA
from nbxmpp.protocol import NS_XHTML
from nbxmpp.structs import StanzaHandler
from nbxmpp.structs import StanzaIDData
from nbxmpp.structs import XHTMLData
from nbxmpp.util import error_factory
from nbxmpp.const import MessageType
......@@ -81,17 +79,7 @@ class BaseMessage:
forms = stanza.getTags('x', namespace=NS_DATA)
if forms:
properties.forms = forms
xhtml = stanza.getXHTML()
if xhtml is None:
return
if xhtml.getTag('body', namespace=NS_XHTML) is None:
log.warning('xhtml without body found')
log.warning(stanza)
return
properties.xhtml = XHTMLData(xhtml)
properties.xhtml = stanza.getXHTML()
@staticmethod
def _parse_type(stanza):
......
......@@ -22,7 +22,6 @@ from nbxmpp.protocol import NS_FORWARD
from nbxmpp.protocol import NS_MUC_USER
from nbxmpp.protocol import NS_MAM_1
from nbxmpp.protocol import NS_MAM_2
from nbxmpp.protocol import NS_XHTML
from nbxmpp.protocol import NodeProcessed
from nbxmpp.protocol import InvalidFrom
from nbxmpp.protocol import InvalidStanza
......@@ -119,16 +118,3 @@ def unwrap_mam(stanza, own_jid):
archive=stanza.getFrom(),
namespace=result.getNamespace(),
timestamp=delay_timestamp)
def build_xhtml_body(xhtml, xmllang=None):
try:
if xmllang is not None:
body = '<body xmlns="%s" xml:lang="%s">%s</body>' % (
NS_XHTML, xmllang, xhtml)
else:
body = '<body xmlns="%s">%s</body>' % (NS_XHTML, xhtml)
except Exception as error:
log.error('Error while building xhtml node: %s', error)
return None
return body
......@@ -26,6 +26,7 @@ from base64 import b64encode
from precis_i18n import get_profile
from nbxmpp.stringprepare import nameprep
from nbxmpp.simplexml import Node
from nbxmpp.simplexml import NodeBuilder
def ascii_upper(s):
return s.upper()
......@@ -1231,7 +1232,7 @@ class Message(Protocol):
payload=payload, timestamp=timestamp, xmlns=xmlns, node=node)
if body:
self.setBody(body)
if xhtml is not None:
if xhtml:
self.setXHTML(xhtml)
if subject is not None:
self.setSubject(subject)
......@@ -1242,8 +1243,22 @@ class Message(Protocol):
"""
return self.getTagData('body')
def getXHTML(self):
return self.getTag('html', namespace=NS_XHTML_IM)
def getXHTML(self, xmllang=None):
"""
Return serialized xhtml-im element text of the message
TODO: Returning a DOM could make rendering faster.
"""
xhtml = self.getTag('html', namespace=NS_XHTML_IM)
if xhtml:
if xmllang:
body = xhtml.getTag('body',
namespace=NS_XHTML,
attrs={'xml:lang': xmllang})
else:
body = xhtml.getTag('body', namespace=NS_XHTML)
return str(body)
return None
def getSubject(self):
"""
......@@ -1278,20 +1293,25 @@ class Message(Protocol):
Set the text of the message"""
self.setTagData('body', val)
def setXHTML(self, body, add=False):
if isinstance(body, str):
body = Node(node=body)
if add:
xhtml = self.getTag('html', namespace=NS_XHTML_IM)
if xhtml is not None:
xhtml.addChild(node=body)
def setXHTML(self, val, xmllang=None):
"""
Sets the xhtml text of the message (XEP-0071). The parameter is the
"inner html" to the body.
"""
try:
if xmllang:
dom = NodeBuilder('<body xmlns="%s" xml:lang="%s">%s</body>' \
% (NS_XHTML, xmllang, val)).getDom()
else:
self.addChild('html', namespace=NS_XHTML_IM, payload=body)
else:
xhtml_nodes = self.getTags('html', namespace=NS_XHTML_IM)
for xhtml in xhtml_nodes:
self.delChild(xhtml)
self.addChild('html', namespace=NS_XHTML_IM, payload=body)
dom = NodeBuilder('<body xmlns="%s">%s</body>' % (NS_XHTML,
val), 0).getDom()
if self.getTag('html'):
self.getTag('html').addChild(node=dom)
else:
self.setTag('html', namespace=NS_XHTML_IM).addChild(node=dom)
except Exception as e:
print("Error" + str(e))
# FIXME: log. we could not set xhtml (parse error, whatever)
def setSubject(self, val):
"""
......
......@@ -26,7 +26,6 @@ from nbxmpp.protocol import NS_MAM_2
from nbxmpp.protocol import NS_MUC
from nbxmpp.protocol import NS_MUC_INFO
from nbxmpp.protocol import NS_CLIENT
from nbxmpp.protocol import NS_XHTML
from nbxmpp.protocol import Protocol
from nbxmpp.const import MessageType
from nbxmpp.const import AvatarState
......@@ -832,26 +831,3 @@ class PresenceProperties:
return self.muc_user.role
except Exception:
return None
class XHTMLData:
def __init__(self, xhtml):
self._bodys = {}
for body in xhtml.getTags('body', namespace=NS_XHTML):
lang = body.getXmlLang()
self._bodys[lang] = body
def get_body(self, pref_lang=None):
if pref_lang is not None:
body = self._bodys.get(pref_lang)
if body is not None:
return str(body)
body = self._bodys.get('en')
if body is not None:
return str(body)
body = self._bodys.get(None)
if body is not None:
return str(body)
return str(self._bodys.popitem()[1])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment