Commit 8b148543 authored by eckhart's avatar eckhart
Browse files

- syntaxtree.Node.as_xml: prepare whitespace handling

parent 19c7acea
...@@ -566,23 +566,30 @@ class Node(collections.abc.Sized): ...@@ -566,23 +566,30 @@ class Node(collections.abc.Sized):
column. column.
""" """
inline = False
def opening(node) -> str: def opening(node) -> str:
"""Returns the opening string for the representation of `node`.""" """Returns the opening string for the representation of `node`."""
txt = ['<', node.tag_name] txt = ['<', node.tag_name]
has_reserved_attrs = hasattr(node, '_xml_attr') \ has_reserved_attrs = hasattr(node, '_xml_attr') \
and any (r in node.attributes for r in {'err', 'line', 'col'}) and any (r in node.attributes for r in {'err', 'line', 'col'})
nonlocal inline
inline = False
if hasattr(node, '_xml_attr'): if hasattr(node, '_xml_attr'):
if '_inline' in node.attributes:
inline = True
del node.attributes['_inline']
txt.extend(' %s="%s"' % (k, v) for k, v in node.attributes.items()) txt.extend(' %s="%s"' % (k, v) for k, v in node.attributes.items())
if src and not has_reserved_attrs: if src and not has_reserved_attrs:
txt.append(' line="%i" col="%i"' % line_col(line_breaks, node.pos)) txt.append(' line="%i" col="%i"' % line_col(line_breaks, node.pos))
if showerrors and node.errors and not has_reserved_attrs: if showerrors and node.errors and not has_reserved_attrs:
txt.append(' err="%s"' % ''.join(str(err).replace('"', r'\"') txt.append(' err="%s"' % ''.join(str(err).replace('"', r'\"')
for err in node.errors)) for err in node.errors))
return "".join(txt + [">\n"]) return "".join(txt + [">" if inline else ">\n"])
def closing(node): def closing(node):
"""Returns the closing string for the representation of `node`.""" """Returns the closing string for the representation of `node`."""
return '\n</' + node.tag_name + '>' return ('</' if inline else '\n</') + node.tag_name + '>'
line_breaks = linebreaks(src) if src else [] line_breaks = linebreaks(src) if src else []
return self._tree_repr(' ' * indentation, opening, closing, density=1) return self._tree_repr(' ' * indentation, opening, closing, density=1)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment