# HG changeset patch # User Goffi # Date 1605189195 -3600 # Node ID 2b6f69f6df8c6299bbedec5b167f33abc9159da3 # Parent 0957ea9137b87b4ffc268340a18f2e4fc8d4a04d tools(xml_tools): fixed `
` unwrapping + added `parse` instance: `
` 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. diff -r 0957ea9137b8 -r 2b6f69f6df8c sat/tools/xml_tools.py --- a/sat/tools/xml_tools.py Thu Nov 12 14:53:15 2020 +0100 +++ b/sat/tools/xml_tools.py Thu Nov 12 14:53:15 2020 +0100 @@ -1701,8 +1701,8 @@ field.value.uri == C.NS_XHTML) -class ElementParser(object): - """callable class to parse XML string into Element""" +class ElementParser: + """Callable class to parse XML string into Element""" # XXX: Found at http://stackoverflow.com/questions/2093400/how-to-create-twisted-words-xish-domish-element-entirely-from-raw-xml/2095942#2095942 @@ -1758,12 +1758,16 @@ parser.parse(tmp.toXml().encode("utf-8")) 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 and domish.IElement.providedBy(top_elt.children[0]): - top_elt = top_elt.firstChildElement() + # and remove our wrapping
is this is the case + top_elt_children = list(top_elt.elements()) + if len(top_elt_children) == 1: + top_elt = top_elt_children[0] return top_elt +parse = ElementParser() + + # FIXME: this method is duplicated from frontends.tools.xmlui.getText def getText(node): """Get child text nodes of a domish.Element.