comparison src/tools/xml_tools.py @ 1817:7ef0f5f90862

core (xml_tools), plugin XEP-0277: ElementParser element now manage automatically the wrapping with <div/> element when needed + fixed content_xhtml/title_xhtml in XEP-0277
author Goffi <goffi@goffi.org>
date Fri, 22 Jan 2016 20:24:17 +0100
parents 17c0364607be
children 7e6342de71fb
comparison
equal deleted inserted replaced
1816:2a030a830ebd 1817:7ef0f5f90862
1324 1324
1325 Found at http://stackoverflow.com/questions/2093400/how-to-create-twisted-words-xish-domish-element-entirely-from-raw-xml/2095942#2095942 1325 Found at http://stackoverflow.com/questions/2093400/how-to-create-twisted-words-xish-domish-element-entirely-from-raw-xml/2095942#2095942
1326 (c) Karl Anderson 1326 (c) Karl Anderson
1327 """ 1327 """
1328 1328
1329 def __call__(self, raw_xml, force_spaces=False): 1329 def __call__(self, raw_xml, force_spaces=False, namespace=None):
1330 """ 1330 """
1331 @param raw_xml(unicode): the raw XML 1331 @param raw_xml(unicode): the raw XML
1332 @param force_spaces: if True, replace occurrences of '\n' and '\t' with ' '. 1332 @param force_spaces (bool): if True, replace occurrences of '\n' and '\t' with ' '.
1333 """ 1333 @param namespace(unicode, None): if set, use this namespace for the wrapping element
1334 """
1335 # we need to wrap element in case
1336 # there is not a unique one on the top
1337 if namespace is not None:
1338 raw_xml = u"<div xmlns='{}'>{}</div>".format(namespace, raw_xml)
1339 else:
1340 raw_xml = u"<div>{}</div>".format(raw_xml)
1341
1334 self.result = None 1342 self.result = None
1335 1343
1336 def onStart(elem): 1344 def onStart(elem):
1337 self.result = elem 1345 self.result = elem
1338 1346
1349 tmp = domish.Element((None, "s")) 1357 tmp = domish.Element((None, "s"))
1350 if force_spaces: 1358 if force_spaces:
1351 raw_xml = raw_xml.replace('\n', ' ').replace('\t', ' ') 1359 raw_xml = raw_xml.replace('\n', ' ').replace('\t', ' ')
1352 tmp.addRawXml(raw_xml) 1360 tmp.addRawXml(raw_xml)
1353 parser.parse(tmp.toXml().encode('utf-8')) 1361 parser.parse(tmp.toXml().encode('utf-8'))
1354 return self.result.firstChildElement() 1362 top_elt = self.result.firstChildElement()
1363 # we now can check if there was a unique element on the top
1364 # and remove our wrapping <div/> is this was the case
1365 if len(top_elt.children) == 1:
1366 top_elt = top_elt.firstChildElement()
1367 return top_elt
1355 1368
1356 1369
1357 # FIXME: this method is duplicated from frontends.tools.xmlui.getText 1370 # FIXME: this method is duplicated from frontends.tools.xmlui.getText
1358 def getText(node): 1371 def getText(node):
1359 """Get child text nodes of a domish.Element. 1372 """Get child text nodes of a domish.Element.