fix for mixed content model representation in ElementTree

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1507 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2006-11-21 19:53:19 +00:00
parent b49cc08b36
commit 113924fb11
2 changed files with 17 additions and 7 deletions

View file

@ -28,7 +28,8 @@ The elements generator lets you easily create snippets of XML or XHTML:
</body> </body>
</html> </html>
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() >>> doc.renderTree()
'<html><head><title>Page Title</title></head><body><div class="top">The top bar</div><div class="body">The body stuff</div></body></html>' '<html><head><title>Page Title</title></head><body><div class="top">The top bar</div><div class="body">The body stuff</div></body></html>'
@ -92,3 +93,11 @@ just by accessing an element's attributes:
>>> print doc.render() >>> print doc.render()
<html>...<p...>...</p>... <html>...<p...>...</p>...
>>> x = p('Some more text').b('bold text')
>>> x = p('and normal again')
>>> print p.render()
<p...>...home...Some more text
<b>...bold text...</b>
...and normal again
</p>

View file

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