diff --git a/xml/README.txt b/xml/README.txt index 0eab647..7b807bb 100644 --- a/xml/README.txt +++ b/xml/README.txt @@ -28,7 +28,8 @@ The elements generator lets you easily create snippets of XML or XHTML: -An XML element thus created may be converted to an ElementTree: +An XML element thus created may be converted to an ElementTree (the standard +implementation in fact uses an lxml.etree structure as its basis): >>> doc.renderTree() 'Page Title
The top bar
The body stuff
' @@ -92,3 +93,11 @@ just by accessing an element's attributes: >>> print doc.render() ......

... + >>> x = p('Some more text').b('bold text') + >>> x = p('and normal again') + >>> print p.render() + ...home...Some more text + ...bold text... + ...and normal again +

+ diff --git a/xml/element.py b/xml/element.py index a6165d5..74ba38a 100644 --- a/xml/element.py +++ b/xml/element.py @@ -36,8 +36,8 @@ class Element(object): result.append(base.text) for c in base.getchildren(): result.append(Element(c)) - if base.tail: - result.append(base.tail) + if c.tail: # the children's tails belong to this element + result.append(c.tail) return result @property @@ -59,10 +59,11 @@ class Element(object): for c in children: if isinstance(c, Element): base.append(c.baseElement) - elif not base: - base.text = base.text and '\n'.join(base.text, c) or c - else: - base.tail = base.tail and '\n'.join(base.tail, c) or c + elif not base: # no children yet, so it's the first text node + base.text = base.text and ' '.join((base.text, c)) or c + else: # if there are children, append text to the last child's tail + lastChild = base.getchildren()[-1] + lastChild.tail = lastChild.tail and ' '.join((lastChild.tail, c)) or c for a in attributes: if a.endswith('_'): attr = a[:-1]