Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dev.gajim.org/gajim/python-nbxmpp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Hörist <philipp@hoerist.com>2019-11-03 16:41:50 +0300
committerPhilipp Hörist <philipp@hoerist.com>2019-11-04 00:08:31 +0300
commit1a844d5042f90e42f577dc57fa161cd801ca145f (patch)
tree28352c6762acad2d5d1ac4623cf417c99ede94d5
parenta335c9ec46651e029703a97a3d5ec2d46f68a077 (diff)
Refactor XHTML code
- setXHTML expects now a body Node - getXHTML returns the html Node - Add XHTMLData struct
-rw-r--r--nbxmpp/modules/message.py14
-rw-r--r--nbxmpp/modules/misc.py14
-rw-r--r--nbxmpp/protocol.py52
-rw-r--r--nbxmpp/structs.py24
4 files changed, 67 insertions, 37 deletions
diff --git a/nbxmpp/modules/message.py b/nbxmpp/modules/message.py
index 27ef286..8a708eb 100644
--- a/nbxmpp/modules/message.py
+++ b/nbxmpp/modules/message.py
@@ -19,8 +19,10 @@ 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
@@ -79,7 +81,17 @@ class BaseMessage:
forms = stanza.getTags('x', namespace=NS_DATA)
if forms:
properties.forms = forms
- properties.xhtml = stanza.getXHTML()
+
+ 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)
@staticmethod
def _parse_type(stanza):
diff --git a/nbxmpp/modules/misc.py b/nbxmpp/modules/misc.py
index 4fdbf4e..02efa8c 100644
--- a/nbxmpp/modules/misc.py
+++ b/nbxmpp/modules/misc.py
@@ -22,6 +22,7 @@ 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
@@ -118,3 +119,16 @@ 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
diff --git a/nbxmpp/protocol.py b/nbxmpp/protocol.py
index 54bdd1d..aa4fe13 100644
--- a/nbxmpp/protocol.py
+++ b/nbxmpp/protocol.py
@@ -26,7 +26,6 @@ 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()
@@ -1232,7 +1231,7 @@ class Message(Protocol):
payload=payload, timestamp=timestamp, xmlns=xmlns, node=node)
if body:
self.setBody(body)
- if xhtml:
+ if xhtml is not None:
self.setXHTML(xhtml)
if subject is not None:
self.setSubject(subject)
@@ -1243,22 +1242,8 @@ class Message(Protocol):
"""
return self.getTagData('body')
- 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 getXHTML(self):
+ return self.getTag('html', namespace=NS_XHTML_IM)
def getSubject(self):
"""
@@ -1293,25 +1278,20 @@ class Message(Protocol):
Set the text of the message"""
self.setTagData('body', val)
- 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()
+ 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)
else:
- 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)
+ 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)
def setSubject(self, val):
"""
diff --git a/nbxmpp/structs.py b/nbxmpp/structs.py
index 77bf605..4512704 100644
--- a/nbxmpp/structs.py
+++ b/nbxmpp/structs.py
@@ -26,6 +26,7 @@ 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
@@ -831,3 +832,26 @@ 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])