changeset 837:eff944ff3e13

plugin XEP_0277: check for XHTML namespace and decapsulate the content from the main div container
author souliane <souliane@mailoo.org>
date Wed, 12 Feb 2014 15:23:15 +0100 (2014-02-12)
parents 2cc0201b4613
children 8829a6c0b070
files src/plugins/plugin_xep_0277.py
diffstat 1 files changed, 12 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0277.py	Wed Feb 12 15:21:00 2014 +0100
+++ b/src/plugins/plugin_xep_0277.py	Wed Feb 12 15:23:15 2014 +0100
@@ -121,8 +121,8 @@
                 except KeyError:
                     content_type = 'text'
                 if content_type == 'xhtml':
-                    # TODO: proper check of body namespace
-                    microblog_data['%s_xhtml' % key] = yield self.host.plugins["TEXT-SYNTAXES"].clean_xhtml(attr.text)
+                    text = self.__decapsulateExtraNS(attr.text)
+                    microblog_data['%s_xhtml' % key] = yield self.host.plugins["TEXT-SYNTAXES"].clean_xhtml(text)
                 else:
                     microblog_data[key] = attr.text
             if key not in microblog_data and ('%s_xhtml' % key) in microblog_data:
@@ -173,6 +173,16 @@
 
         defer.returnValue(microblog_data)
 
+    def __decapsulateExtraNS(self, text):
+        """Check for XHTML namespace and decapsulate the content so the user
+        who wants to modify an entry will see the text that he entered. Also
+        this avoids successive encapsulation with a new <div>...</div> at
+        each modification (encapsulation is done in self.data2entry)"""
+        elt = ElementParser()(text)
+        if elt.uri != NS_XHTML:
+            raise exceptions.DataError(_('Content of type XHTML must declare its namespace!'))
+        return elt.firstChildElement().toXml()
+
     def microblogCB(self, itemsEvent, profile):
         d = defer.Deferred()