From 4a44b417935e8127948e4ffd64beb0b1f865e58d Mon Sep 17 00:00:00 2001 From: Goffi Date: Fri, 22 Jan 2016 20:24:17 +0100 Subject: [PATCH] core (xml_tools), plugin XEP-0277: ElementParser element now manage automatically the wrapping with
element when needed + fixed content_xhtml/title_xhtml in XEP-0277 --- src/plugins/plugin_xep_0277.py | 20 +++++++++----------- src/tools/xml_tools.py | 19 ++++++++++++++++--- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/plugins/plugin_xep_0277.py b/src/plugins/plugin_xep_0277.py index dc9ca57c..e16bfb82 100755 --- a/src/plugins/plugin_xep_0277.py +++ b/src/plugins/plugin_xep_0277.py @@ -361,20 +361,18 @@ class XEP_0277(object): elem = entry_elt.addElement(elem_name) if type_: if type_ == '_rich': # convert input from current syntax to XHTML - converted = yield synt.convert(data[attr], synt.getCurrentSyntax(profile), "XHTML") + xml_content = yield synt.convert(data[attr], synt.getCurrentSyntax(profile), "XHTML") if '{}_xhtml'.format(elem_name) in data: raise failure.Failure(exceptions.DataError(_("Can't have xhtml and rich content at the same time"))) + else: + xml_content = data[attr] - xml_content = u'
{converted}
'.format( - ns=NS_XHTML, - converted=converted) - div_elt = xml_tools.ElementParser()(xml_content) - if len(div_elt.children) == 1: - # if we have two
wrapping the content, - # we remove the one we have just added - child = div_elt.children[0] - if child.name == 'div' and not child.attributes and child.uri == div_elt.uri: - div_elt = child + div_elt = xml_tools.ElementParser()(xml_content, namespace=NS_XHTML) + if div_elt.name != 'div' or div_elt.uri != NS_XHTML or div_elt.attributes: + # we need a wrapping
at the top with XHTML namespace + wrap_div_elt = domish.Element((NS_XHTML, 'div')) + wrap_div_elt.addChild(div_elt) + div_elt = wrap_div_elt elem.addChild(div_elt) elem['type'] = 'xhtml' if elem_name not in data: diff --git a/src/tools/xml_tools.py b/src/tools/xml_tools.py index da1c48d5..c3ec4680 100644 --- a/src/tools/xml_tools.py +++ b/src/tools/xml_tools.py @@ -1326,11 +1326,19 @@ class ElementParser(object): (c) Karl Anderson """ - def __call__(self, raw_xml, force_spaces=False): + def __call__(self, raw_xml, force_spaces=False, namespace=None): """ @param raw_xml(unicode): the raw XML - @param force_spaces: if True, replace occurrences of '\n' and '\t' with ' '. + @param force_spaces (bool): if True, replace occurrences of '\n' and '\t' with ' '. + @param namespace(unicode, None): if set, use this namespace for the wrapping element """ + # we need to wrap element in case + # there is not a unique one on the top + if namespace is not None: + raw_xml = u"
{}
".format(namespace, raw_xml) + else: + raw_xml = u"
{}
".format(raw_xml) + self.result = None def onStart(elem): @@ -1351,7 +1359,12 @@ class ElementParser(object): raw_xml = raw_xml.replace('\n', ' ').replace('\t', ' ') tmp.addRawXml(raw_xml) parser.parse(tmp.toXml().encode('utf-8')) - return self.result.firstChildElement() + top_elt = self.result.firstChildElement() + # we now can check if there was a unique element on the top + # and remove our wrapping
is this was the case + if len(top_elt.children) == 1: + top_elt = top_elt.firstChildElement() + return top_elt # FIXME: this method is duplicated from frontends.tools.xmlui.getText