comparison sat/tools/xml_tools.py @ 3393:2b6f69f6df8c

tools(xml_tools): fixed `<div>` unwrapping + added `parse` instance: `<div>` unwrapping could fail when a text node was a sibling of the top element (could easily happen ith a `\n` line feed added by an editor). This is fixed by filtering on IElement with `elements()`. A `parse` instance has been added as it is not necessary to create a new `ElementParser` each time that we want to parse something.
author Goffi <goffi@goffi.org>
date Thu, 12 Nov 2020 14:53:15 +0100
parents 8770397f8f82
children d6a482a78bda
comparison
equal deleted inserted replaced
3392:0957ea9137b8 3393:2b6f69f6df8c
1699 """Check if a data_form.Field is an XHTML one""" 1699 """Check if a data_form.Field is an XHTML one"""
1700 return (field.fieldType is None and field.ext_type == "xml" and 1700 return (field.fieldType is None and field.ext_type == "xml" and
1701 field.value.uri == C.NS_XHTML) 1701 field.value.uri == C.NS_XHTML)
1702 1702
1703 1703
1704 class ElementParser(object): 1704 class ElementParser:
1705 """callable class to parse XML string into Element""" 1705 """Callable class to parse XML string into Element"""
1706 1706
1707 # XXX: Found at http://stackoverflow.com/questions/2093400/how-to-create-twisted-words-xish-domish-element-entirely-from-raw-xml/2095942#2095942 1707 # XXX: Found at http://stackoverflow.com/questions/2093400/how-to-create-twisted-words-xish-domish-element-entirely-from-raw-xml/2095942#2095942
1708 1708
1709 def _escapeHTML(self, matchobj): 1709 def _escapeHTML(self, matchobj):
1710 entity = matchobj.group(1) 1710 entity = matchobj.group(1)
1756 raw_xml = raw_xml.replace("\n", " ").replace("\t", " ") 1756 raw_xml = raw_xml.replace("\n", " ").replace("\t", " ")
1757 tmp.addRawXml(raw_xml) 1757 tmp.addRawXml(raw_xml)
1758 parser.parse(tmp.toXml().encode("utf-8")) 1758 parser.parse(tmp.toXml().encode("utf-8"))
1759 top_elt = self.result.firstChildElement() 1759 top_elt = self.result.firstChildElement()
1760 # we now can check if there was a unique element on the top 1760 # we now can check if there was a unique element on the top
1761 # and remove our wrapping <div/> is this was the case 1761 # and remove our wrapping <div/> is this is the case
1762 if len(top_elt.children) == 1 and domish.IElement.providedBy(top_elt.children[0]): 1762 top_elt_children = list(top_elt.elements())
1763 top_elt = top_elt.firstChildElement() 1763 if len(top_elt_children) == 1:
1764 top_elt = top_elt_children[0]
1764 return top_elt 1765 return top_elt
1766
1767
1768 parse = ElementParser()
1765 1769
1766 1770
1767 # FIXME: this method is duplicated from frontends.tools.xmlui.getText 1771 # FIXME: this method is duplicated from frontends.tools.xmlui.getText
1768 def getText(node): 1772 def getText(node):
1769 """Get child text nodes of a domish.Element. 1773 """Get child text nodes of a domish.Element.